Ver código fonte

Call public callback blocks asynchronously on the main thread.

Peter Andrews 4 anos atrás
pai
commit
f26e72c4d4

+ 6 - 2
GoogleSignIn/Sources/GIDAuthentication.m

@@ -209,7 +209,9 @@ static NSString *const kNewIOSSystemName = @"iOS";
 - (void)doWithFreshTokens:(GIDAuthenticationAction)action {
   if (!([self.accessTokenExpirationDate timeIntervalSinceNow] < kMinimalTimeToExpire ||
       (self.idToken && [self.idTokenExpirationDate timeIntervalSinceNow] < kMinimalTimeToExpire))) {
-    action(self, nil);
+    dispatch_async(dispatch_get_main_queue(), ^{
+      action(self, nil);
+    });
     return;
   }
   @synchronized (_authenticationHandlerQueue) {
@@ -252,7 +254,9 @@ static NSString *const kNewIOSSystemName = @"iOS";
         [_authenticationHandlerQueue removeAllObjects];
       }
       for (GIDAuthenticationAction action in authenticationHandlerQueue) {
-        action(error ? nil : self, error);
+        dispatch_async(dispatch_get_main_queue(), ^{
+          action(error ? nil : self, error);
+        });
       }
     }];
   }];

+ 18 - 6
GoogleSignIn/Sources/GIDSignIn.m

@@ -210,7 +210,9 @@ static const NSTimeInterval kMinimumRestoredAccessTokenTimeToExpire = 600.0;
                                          code:kGIDSignInErrorCodeNoCurrentUser
                                      userInfo:nil];
     if (callback) {
-      callback(nil, error);
+      dispatch_async(dispatch_get_main_queue(), ^{
+        callback(nil, error);
+      });
     }
     return;
   }
@@ -237,7 +239,9 @@ static const NSTimeInterval kMinimumRestoredAccessTokenTimeToExpire = 600.0;
                                          code:kGIDSignInErrorCodeScopesAlreadyGranted
                                      userInfo:nil];
     if (callback) {
-      callback(nil, error);
+      dispatch_async(dispatch_get_main_queue(), ^{
+        callback(nil, error);
+      });
     }
     return;
   }
@@ -278,7 +282,9 @@ static const NSTimeInterval kMinimumRestoredAccessTokenTimeToExpire = 600.0;
     [self signOut];
     // Nothing to do here, consider the operation successful.
     if (callback) {
-      callback(nil);
+      dispatch_async(dispatch_get_main_queue(), ^{
+        callback(nil);
+      });
     }
     return;
   }
@@ -299,7 +305,9 @@ static const NSTimeInterval kMinimumRestoredAccessTokenTimeToExpire = 600.0;
       [self signOut];
     }
     if (callback) {
-      callback(error);
+      dispatch_async(dispatch_get_main_queue(), ^{
+        callback(error);
+      });
     }
   }];
 }
@@ -503,7 +511,9 @@ static const NSTimeInterval kMinimumRestoredAccessTokenTimeToExpire = 600.0;
                                          code:kGIDSignInErrorCodeHasNoAuthInKeychain
                                      userInfo:nil];
     if (options.callback) {
-      options.callback(nil, error);
+      dispatch_async(dispatch_get_main_queue(), ^{
+        options.callback(nil, error);
+      });
     }
     return;
   }
@@ -668,7 +678,9 @@ static const NSTimeInterval kMinimumRestoredAccessTokenTimeToExpire = 600.0;
   [authFlow addCallback:^() {
     GIDAuthFlow *handlerAuthFlow = weakAuthFlow;
     if (_currentOptions.callback) {
-      _currentOptions.callback(_currentUser, handlerAuthFlow.error);
+      dispatch_async(dispatch_get_main_queue(), ^{
+        _currentOptions.callback(_currentUser, handlerAuthFlow.error);
+      });
     }
   }];
 }

+ 2 - 2
GoogleSignIn/Sources/Public/GoogleSignIn/GIDAuthentication.h

@@ -21,7 +21,7 @@
 
 NS_ASSUME_NONNULL_BEGIN
 
-/// The callback block that takes a `GIDAuthentication` or an error if the attempt to refresh tokens
+/// A callback block that takes a `GIDAuthentication` or an error if the attempt to refresh tokens
 /// was unsuccessful.
 typedef void (^GIDAuthenticationAction)(GIDAuthentication *_Nullable authentication,
                                         NSError *_Nullable error);
@@ -58,7 +58,7 @@ typedef void (^GIDAuthenticationAction)(GIDAuthentication *_Nullable authenticat
 /// about to expire.
 ///
 /// @param action A callback block that takes a `GIDAuthentication` or an error if the attempt to
-///               refresh tokens was unsuccessful.
+///     refresh tokens was unsuccessful.  The block will be called asynchronously on the main queue.
 - (void)doWithFreshTokens:(GIDAuthenticationAction)action;
 
 @end

+ 10 - 5
GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h

@@ -83,7 +83,8 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error);
 
 /// Attempts to restore a previously authenticated user without interaction.
 ///
-/// @param callback The `GIDSignInCallback` block that is called on completion.
+/// @param callback The `GIDSignInCallback` block that is called on completion.  This block will be
+///     called asynchronously on the main queue.
 - (void)restorePreviousSignInWithCallback:(nullable GIDSignInCallback)callback;
 
 /// Starts an interactive sign-in flow using the provided configuration.
@@ -96,7 +97,8 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error);
 /// @param configuration The configuration properties to be used for this flow.
 /// @param presentingViewController The view controller used to present `SFSafariViewContoller` on
 ///     iOS 9 and 10.
-/// @param callback The `GIDSignInCallback` block that is called on completion.
+/// @param callback The `GIDSignInCallback` block that is called on completion.  This block will be
+///     called asynchronously on the main queue.
 - (void)signInWithConfiguration:(GIDConfiguration *)configuration
        presentingViewController:(UIViewController *)presentingViewController
                        callback:(nullable GIDSignInCallback)callback;
@@ -113,7 +115,8 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error);
 ///     iOS 9 and 10.
 /// @param hint An optional hint for the authorization server, for example the user's ID or email
 ///     address, to be prefilled if possible.
-/// @param callback The `GIDSignInCallback` block that is called on completion.
+/// @param callback The `GIDSignInCallback` block that is called on completion.  This block will be
+///     called asynchronously on the main queue.
 - (void)signInWithConfiguration:(GIDConfiguration *)configuration
        presentingViewController:(UIViewController *)presentingViewController
                            hint:(nullable NSString *)hint
@@ -124,7 +127,8 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error);
 /// @param scopes The scopes to ask the user to consent to.
 /// @param presentingViewController The view controller used to present `SFSafariViewContoller` on
 ///     iOS 9 and 10.
-/// @param callback The `GIDSignInCallback` block that is called on completion.
+/// @param callback The `GIDSignInCallback` block that is called on completion.  This block will be
+///     called asynchronously on the main queue.
 - (void)addScopes:(NSArray<NSString *> *)scopes
     presentingViewController:(UIViewController *)presentingViewController
                     callback:(nullable GIDSignInCallback)callback;
@@ -135,7 +139,8 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error);
 /// Disconnects the current user from the app and revokes previous authentication. If the operation
 /// succeeds, the OAuth 2.0 token is also removed from keychain.
 ///
-/// @param callback The optional `GIDDisconnectCallback` block that is called on completion.
+/// @param callback The optional `GIDDisconnectCallback` block that is called on completion.  This
+///     block will be called asynchronously on the main queue.
 - (void)disconnectWithCallback:(nullable GIDDisconnectCallback)callback;
 
 @end