@@ -27,6 +27,10 @@ public class WebRequestManager: WebRequestManaging {
2727
2828 internal var requests : Set < Wrapper > = Set < Wrapper > ( )
2929
30+ private var readyRequests : Set < Wrapper > {
31+ return self . requests. filter { $0. state == . ready || $0. state == . unauthorized }
32+ }
33+
3034 public var timeoutInterval : TimeInterval = 60.0
3135
3236 public var lastRefresh : TimeInterval = 0.0
@@ -126,15 +130,10 @@ private extension WebRequestManager {
126130 func begin( ) {
127131 guard ( !isRefreshing) else { return }
128132 accessQueue. sync {
129- let readyRequests = self . requests
130- . filter { $0. state == . ready || $0. state == . unauthorized }
133+ let readyRequests = self . readyRequests
131134
132135 guard let session = self . sessionProvider? . current else {
133- if let anyRequest = readyRequests. first? . originalRequest {
134- self . fail ( request: anyRequest, withStatus: 401 )
135- }
136- for wrapper in readyRequests { wrapper. state = . cancelled }
137- notificationCenter. post ( UnauthorizedResponseNotification)
136+ self . failUnauthorized ( )
138137 return
139138 }
140139
@@ -160,11 +159,13 @@ private extension WebRequestManager {
160159 return
161160 }
162161
163- try wrapper. originalRequest. completion ? ( actualResult, wrapper. originalRequest)
164-
165162 if wrapper. state == . unauthorized {
166- notificationCenter. post ( UnauthorizedResponseNotification)
163+ DispatchQueue . main. async {
164+ self . notificationCenter. post ( self . UnauthorizedResponseNotification)
165+ }
167166 }
167+
168+ try wrapper. originalRequest. completion ? ( actualResult, wrapper. originalRequest)
168169 }
169170
170171 func shouldRefresh( since timestamp: TimeInterval ) -> Bool {
@@ -173,7 +174,9 @@ private extension WebRequestManager {
173174
174175 func performRefresh( ) {
175176 isRefreshing = true
176- sessionProvider. refresh ( )
177+ execQueue. async {
178+ self . sessionProvider. refresh ( )
179+ }
177180 }
178181
179182 func remove( _ wrapper: Wrapper ) {
@@ -187,6 +190,15 @@ private extension WebRequestManager {
187190 func fail( request: WebRequest , withStatus status: Int ) {
188191 try ? request. completion ? ( WebRequest . Result ( status: status) , request)
189192 }
193+
194+ func failUnauthorized( ) {
195+ let readyRequests = self . readyRequests
196+ if let anyRequest = readyRequests. first {
197+ self . fail ( request: anyRequest. originalRequest, withStatus: 401 )
198+ }
199+ for wrapper in readyRequests { wrapper. state = . cancelled }
200+ notificationCenter. post ( UnauthorizedResponseNotification)
201+ }
190202}
191203
192204extension WebRequestManager : WebRequestSessionProvidingDelegate {
0 commit comments