diff options
author | 2017-06-30 14:25:02 -0700 | |
---|---|---|
committer | 2017-06-30 14:25:02 -0700 | |
commit | fb5aa974eedeada8de8ffb30c0378d528ae870f9 (patch) | |
tree | f1b004842280f2f24ab6dd34b7a88ceb04b9ef82 /AuthSamples | |
parent | 8743564d9f6d5a0ea893555ed17bedc786bc9aa9 (diff) |
Moves most of FIRAuth initialization out of the calling thread. (#115)
* Adds an option to time initialization of FIRAuth instance in the sample app.
* Changes deployment target from 10.3 to 8.0 for Auth samples.
* Fixes some Xcode warnings in the Auth sample app.
* Moves most of `FIRAuth` initialization to the work thread.
* Fixes a typo.
* Fixes accessing of public `currentUser` method from internal method.
* Fixes tests for macOS.
* Addresses review comments.
Diffstat (limited to 'AuthSamples')
-rw-r--r-- | AuthSamples/Sample/MainViewController.m | 80 | ||||
-rw-r--r-- | AuthSamples/Sample/StaticContentTableViewManager.h | 2 | ||||
-rw-r--r-- | AuthSamples/Sample/UIViewController+Alerts.h | 2 | ||||
-rw-r--r-- | AuthSamples/Samples.xcodeproj/project.pbxproj | 4 |
4 files changed, 80 insertions, 8 deletions
diff --git a/AuthSamples/Sample/MainViewController.m b/AuthSamples/Sample/MainViewController.m index 20f76fe..9876195 100644 --- a/AuthSamples/Sample/MainViewController.m +++ b/AuthSamples/Sample/MainViewController.m @@ -20,6 +20,7 @@ #import "FIRAdditionalUserInfo.h" #import "FIRApp.h" +#import "FIRAppAssociationRegistration.h" #import "FIROAuthProvider.h" #import "FIRPhoneAuthCredential.h" #import "FIRPhoneAuthProvider.h" @@ -362,6 +363,11 @@ static NSString *const kCreateUserTitle = @"Create User"; */ static NSString *const kDeleteAppTitle = @"Delete App"; +/** @var kTimeAuthInitTitle + @brief The text of the "Time Auth Initialization" button. + */ +static NSString *const kTimeAuthInitTitle = @"Time Auth Initialization"; + /** @var kSectionTitleManualTests @brief The section title for automated manual tests. */ @@ -493,6 +499,30 @@ typedef void (^FIRTokenCallback)(NSString *_Nullable token, NSError *_Nullable e - (void)getTokenForcingRefresh:(BOOL)forceRefresh withCallback:(nonnull FIRTokenCallback)callback; @end +/** @category FIRAppAssociationRegistration(Deregistration) + @brief The category for the deregistration method. + */ +@interface FIRAppAssociationRegistration (Deregistration) +/** @fn deregisteredObjectWithHost:key: + @brief Removes the object that was registered with a particular host and key, if one exists. + @param host The host object. + @param key The key to specify the registered object on the host. + */ ++ (void)deregisterObjectWithHost:(id)host key:(NSString *)key; +@end + +@implementation FIRAppAssociationRegistration (Deregistration) + ++ (void)deregisterObjectWithHost:(id)host key:(NSString *)key { + @synchronized(self) { + SEL dictKey = @selector(registeredObjectWithHost:key:creationBlock:); + NSMutableDictionary<NSString *, id> *objectsByKey = objc_getAssociatedObject(host, dictKey); + [objectsByKey removeObjectForKey:key]; + } +} + +@end + @implementation MainViewController { NSMutableString *_consoleString; @@ -681,7 +711,9 @@ typedef void (^FIRTokenCallback)(NSString *_Nullable token, NSError *_Nullable e [StaticContentTableViewCell cellWithTitle:kTokenGetButtonText action:^{ [weakSelf getAppTokenWithForce:NO]; }], [StaticContentTableViewCell cellWithTitle:kTokenRefreshButtonText - action:^{ [weakSelf getAppTokenWithForce:YES]; }] + action:^{ [weakSelf getAppTokenWithForce:YES]; }], + [StaticContentTableViewCell cellWithTitle:kTimeAuthInitTitle + action:^{ [weakSelf timeAuthInitialization]; }] ]], [StaticContentTableViewSection sectionWithTitle:kSectionTitleListeners cells:@[ [StaticContentTableViewCell cellWithTitle:kAddAuthStateListenerTitle @@ -730,8 +762,8 @@ typedef void (^FIRTokenCallback)(NSString *_Nullable token, NSError *_Nullable e /** @fn signInWithProvider:provider: @brief Perform sign in with credential operataion, for given auth provider. - @provider The auth provider. - @callback The callback to continue the flow which executed this sign-in. + @param provider The auth provider. + @param callback The callback to continue the flow which executed this sign-in. */ - (void)signInWithProvider:(nonnull id<AuthProvider>)provider callback:(void(^)(void))callback { if (!provider) { @@ -2382,6 +2414,48 @@ typedef void (^FIRTokenCallback)(NSString *_Nullable token, NSError *_Nullable e }]; } +/** @fn timeAuthInitialization + @brief Times FIRAuth instance initialization time. + */ +- (void)timeAuthInitialization { + // Temporarily disable auth state listener to avoid interfering with the result. + [[NSNotificationCenter defaultCenter] removeObserver:self + name:FIRAuthStateDidChangeNotification + object:nil]; + [self showSpinner:^() { + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^() { + const int numberOfRuns = 4096; + FIRApp *app = [FIRApp defaultApp]; + NSString *key = NSStringFromClass([FIRAuth class]); + NSDate *startTime = [NSDate date]; + for (int i = 0; i < numberOfRuns; i++) { + @autoreleasepool { + [FIRAppAssociationRegistration deregisterObjectWithHost:app key:key]; + [FIRAuth auth]; + } + } + NSDate *endTime = [NSDate date]; + dispatch_async(dispatch_get_main_queue(), ^() { + // Re-enable auth state listener. + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(authStateChangedForAuth:) + name:FIRAuthStateDidChangeNotification + object:nil]; + [self hideSpinner:^() { + NSTimeInterval averageTime = [endTime timeIntervalSinceDate:startTime] / numberOfRuns; + NSString *message = [NSString stringWithFormat: + @"Each [FIRAuth auth] takes average of %.3f ms for %d runs", + averageTime * 1000, numberOfRuns]; + [self showMessagePromptWithTitle:@"Timing Result" + message:message + showCancelButton:NO + completion:nil]; + }]; + }); + }); + }]; +} + #pragma mark - Helpers /** @fn user diff --git a/AuthSamples/Sample/StaticContentTableViewManager.h b/AuthSamples/Sample/StaticContentTableViewManager.h index cb56391..5a7c123 100644 --- a/AuthSamples/Sample/StaticContentTableViewManager.h +++ b/AuthSamples/Sample/StaticContentTableViewManager.h @@ -242,8 +242,6 @@ typedef void(^StaticContentTableViewCellAction)(void); @param customCell The custom @c UITableViewCell to use for this cell. @param title If no custom cell is being used, this is the text of the @c titleLabel of the @c UITableViewCell. - @param title If no custom cell is being used, this is the text of the @c detailTextLabel of the - @c UITableViewCell. @param action A block which is executed when the cell is selected. @param accessibilityID The accessibility ID to add to the cell. */ diff --git a/AuthSamples/Sample/UIViewController+Alerts.h b/AuthSamples/Sample/UIViewController+Alerts.h index 375a0ed..88686c1 100644 --- a/AuthSamples/Sample/UIViewController+Alerts.h +++ b/AuthSamples/Sample/UIViewController+Alerts.h @@ -23,7 +23,7 @@ NS_ASSUME_NONNULL_BEGIN */ typedef void (^AlertPromptCompletionBlock)(BOOL userPressedOK, NSString *_Nullable userInput); -/*! @class Alerts +/*! @category UIViewController(Alerts) @brief Wrapper for @c UIAlertController and @c UIAlertView for backwards compatability with iOS 6+. */ diff --git a/AuthSamples/Samples.xcodeproj/project.pbxproj b/AuthSamples/Samples.xcodeproj/project.pbxproj index dfe1e17..89d78c3 100644 --- a/AuthSamples/Samples.xcodeproj/project.pbxproj +++ b/AuthSamples/Samples.xcodeproj/project.pbxproj @@ -1399,7 +1399,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 10.3; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; @@ -1442,7 +1442,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 10.3; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; TARGETED_DEVICE_FAMILY = "1,2"; |