Explorar el Código

Checking for stream close in handleStreamClose

Sebastian Schmidt hace 8 años
padre
commit
d200fe09ef

+ 8 - 5
Firestore/Source/Remote/FSTStream.m

@@ -343,14 +343,13 @@ static const NSTimeInterval kIdleTimeout = 60.0;
 - (void)closeWithFinalState:(FSTStreamState)finalState error:(nullable NSError *)error {
   FSTAssert(finalState == FSTStreamStateError || error == nil,
             @"Can't provide an error when not in an error state.");
-
-  [self.workerDispatchQueue verifyIsCurrentQueue];
-  [self cancelIdleCheck];
-
   FSTAssert(self.delegate,
             @"closeWithFinalState should only be called for a started stream that has an active "
             @"delegate.");
 
+  [self.workerDispatchQueue verifyIsCurrentQueue];
+  [self cancelIdleCheck];
+
   if (finalState != FSTStreamStateError) {
     // If this is an intentional close ensure we don't delay our next connection attempt.
     [self.backoff reset];
@@ -516,7 +515,11 @@ static const NSTimeInterval kIdleTimeout = 60.0;
  */
 - (void)handleStreamClose:(nullable NSError *)error {
   FSTLog(@"%@ %p close: %@", NSStringFromClass([self class]), (__bridge void *)self, error);
-  FSTAssert([self isStarted], @"Can't handle server close in non-started state.");
+
+  if (![self isStarted]) {   // The stream could have already been closed by the idle close timer.
+    FSTLog(@"%@ Ignoring server close for already closed stream.", NSStringFromClass([self class]));
+    return;
+  }
 
   // In theory the stream could close cleanly, however, in our current model we never expect this
   // to happen because if we stop a stream ourselves, this callback will never be called. To

+ 1 - 4
Firestore/Source/Util/FSTDispatchQueue.m

@@ -58,10 +58,7 @@ NS_ASSUME_NONNULL_BEGIN
 
 - (void)dispatchAfterDelay:(NSTimeInterval)delay block:(void (^)(void))block {
   dispatch_time_t delayNs = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC));
-  dispatch_after(delayNs, self.queue, ^() {
-    // Make sure that we prioritize tasks that are already queued for immediate execution.
-    [self dispatchAsyncAllowingSameQueue:block];
-  });
+  dispatch_after(delayNs, self.queue, block);
 }
 
 #pragma mark - Private Methods