From fa041f1daa3c50617c3566b572bf1dce12fdd0b6 Mon Sep 17 00:00:00 2001 From: Mandar Date: Thu, 3 Oct 2019 14:46:47 -0700 Subject: [PATCH 1/2] The instanceID sync API is deprecated. Implement the async instanceID API. --- FirebaseRemoteConfig/Sources/RCNFetch.m | 25 ++++++++++--- .../RemoteConfigSampleApp/ViewController.m | 37 ++++++++++--------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/FirebaseRemoteConfig/Sources/RCNFetch.m b/FirebaseRemoteConfig/Sources/RCNFetch.m index 7969529eedf..0b53677dc51 100644 --- a/FirebaseRemoteConfig/Sources/RCNFetch.m +++ b/FirebaseRemoteConfig/Sources/RCNFetch.m @@ -215,14 +215,27 @@ - (void)refreshInstanceIDTokenAndFetchCheckInInfoWithCompletionHandler: @"Failed to register InstanceID with error : %@.", error); } if (token) { - NSError *IIDError; strongSelf->_settings.configInstanceIDToken = [token copy]; - strongSelf->_settings.configInstanceID = [instanceID appInstanceID:&IIDError]; - FIRLogInfo(kFIRLoggerRemoteConfig, @"I-RCN000022", @"Success to get iid : %@.", - strongSelf->_settings.configInstanceID); + __weak RCNConfigFetch *instanceIDSelf = strongSelf; + [instanceID getIDWithHandler:^(NSString *_Nullable identity, NSError *_Nullable error) { + RCNConfigFetch *instanceIDStrongSelf = instanceIDSelf; + instanceIDStrongSelf->_settings.configInstanceID = identity; + + if (identity && !error) { + FIRLogInfo(kFIRLoggerRemoteConfig, @"I-RCN000022", @"Success to get iid : %@.", + instanceIDStrongSelf->_settings.configInstanceID); + } else { + FIRLogWarning(kFIRLoggerRemoteConfig, @"I-RCN000055", @"Error getting iid : %@.", + error); + } + // Continue the fetch regardless of whether fetch of instance ID succeeded. + [instanceIDStrongSelf fetchCheckinInfoWithCompletionHandler:completionHandler]; + }]; + + } else { + // Continue the fetch regardless of whether fetch of instance ID succeeded. + [strongSelf fetchCheckinInfoWithCompletionHandler:completionHandler]; } - // Continue the fetch regardless of whether fetch of instance ID succeeded. - [strongSelf fetchCheckinInfoWithCompletionHandler:completionHandler]; }); }; FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000039", @"Starting requesting token."); diff --git a/FirebaseRemoteConfig/Tests/Sample/RemoteConfigSampleApp/ViewController.m b/FirebaseRemoteConfig/Tests/Sample/RemoteConfigSampleApp/ViewController.m index f63da9f9442..b3fe0800425 100644 --- a/FirebaseRemoteConfig/Tests/Sample/RemoteConfigSampleApp/ViewController.m +++ b/FirebaseRemoteConfig/Tests/Sample/RemoteConfigSampleApp/ViewController.m @@ -353,21 +353,21 @@ - (void)printResult:(NSMutableString *)output { [self statusString:currentRCInstance.lastFetchStatus]]]; FIRInstanceID *instanceID = [FIRInstanceID instanceID]; - NSError *error; - NSString *instanceIDString = [instanceID appInstanceID:&error]; - [output appendString:@"\n-----------Instance ID------------------\n"]; - [output appendString:[NSString stringWithFormat:@"%@\n", instanceIDString]]; + [instanceID getIDWithHandler:^(NSString *_Nullable identity, NSError *_Nullable error) { + [output appendString:@"\n-----------Instance ID------------------\n"]; + [output appendString:[NSString stringWithFormat:@"%@\n", identity]]; - [output appendString:@"\n-----------Instance ID token------------\n"]; - [output - appendString:[NSString - stringWithFormat:@"%@\n", currentRCInstance.settings.configInstanceIDToken]]; + [output appendString:@"\n-----------Instance ID token------------\n"]; + [output + appendString:[NSString stringWithFormat:@"%@\n", + currentRCInstance.settings.configInstanceIDToken]]; - [output appendString:@"\n-----------Android ID------------\n"]; - [output - appendString:[NSString stringWithFormat:@"%@\n", currentRCInstance.settings.deviceAuthID]]; + [output appendString:@"\n-----------Android ID------------\n"]; + [output + appendString:[NSString stringWithFormat:@"%@\n", currentRCInstance.settings.deviceAuthID]]; - [[FRCLog sharedInstance] logToConsole:output]; + [[FRCLog sharedInstance] logToConsole:output]; + }]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { @@ -414,12 +414,13 @@ - (IBAction)fetchIIDButtonClicked:(id)sender { if (token) { ((FIRRemoteConfig *)self.RCInstances[self.currentNamespace][self.FIRAppName]) .settings.configInstanceIDToken = token; - NSString *iid = [instanceID appInstanceID:&error]; - [[FRCLog sharedInstance] - logToConsole: - [NSString - stringWithFormat:@"Successfully getting InstanceID : \n\n%@\n\nToken : \n\n%@\n", - iid, token]]; + [instanceID getIDWithHandler:^(NSString *_Nullable identity, NSError *_Nullable error) { + [[FRCLog sharedInstance] + logToConsole:[NSString + stringWithFormat: + @"Successfully getting InstanceID : \n\n%@\n\nToken : \n\n%@\n", + identity, token]]; + }]; } }; [instanceID tokenWithAuthorizedEntity:[FIRApp appNamed:self.FIRAppName].options.GCMSenderID From be26ef715c9c537e56adb1e344de18fd5c324c51 Mon Sep 17 00:00:00 2001 From: Mandar Date: Tue, 8 Oct 2019 17:40:05 -0700 Subject: [PATCH 2/2] Fix for review comments --- FirebaseRemoteConfig/Sources/RCNFetch.m | 50 ++++++++++++++----------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/FirebaseRemoteConfig/Sources/RCNFetch.m b/FirebaseRemoteConfig/Sources/RCNFetch.m index 0b53677dc51..4895bf00e65 100644 --- a/FirebaseRemoteConfig/Sources/RCNFetch.m +++ b/FirebaseRemoteConfig/Sources/RCNFetch.m @@ -205,38 +205,46 @@ - (void)refreshInstanceIDTokenAndFetchCheckInInfoWithCompletionHandler: [self fetchCheckinInfoWithCompletionHandler:completionHandler]; return; } - __weak RCNConfigFetch *weakSelf = self; FIRInstanceIDTokenHandler instanceIDHandler = ^(NSString *token, NSError *error) { - RCNConfigFetch *instanceIDHandlerSelf = weakSelf; - dispatch_async(instanceIDHandlerSelf->_lockQueue, ^{ - RCNConfigFetch *strongSelf = instanceIDHandlerSelf; - if (error) { - FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000020", - @"Failed to register InstanceID with error : %@.", error); - } - if (token) { - strongSelf->_settings.configInstanceIDToken = [token copy]; - __weak RCNConfigFetch *instanceIDSelf = strongSelf; - [instanceID getIDWithHandler:^(NSString *_Nullable identity, NSError *_Nullable error) { - RCNConfigFetch *instanceIDStrongSelf = instanceIDSelf; - instanceIDStrongSelf->_settings.configInstanceID = identity; + if (error) { + FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000020", + @"Failed to register InstanceID with error : %@.", error); + } + + // If the token is available, try to get the instanceID. + __weak RCNConfigFetch *weakSelf = self; + if (token) { + [instanceID getIDWithHandler:^(NSString *_Nullable identity, NSError *_Nullable error) { + RCNConfigFetch *strongSelf = weakSelf; + + // Dispatch to the RC serial queue to update settings on the queue. + dispatch_async(strongSelf->_lockQueue, ^{ + RCNConfigFetch *strongSelfQueue = weakSelf; + + // Update config settings with the IID and token. + strongSelfQueue->_settings.configInstanceIDToken = [token copy]; + strongSelfQueue->_settings.configInstanceID = identity; if (identity && !error) { FIRLogInfo(kFIRLoggerRemoteConfig, @"I-RCN000022", @"Success to get iid : %@.", - instanceIDStrongSelf->_settings.configInstanceID); + strongSelfQueue->_settings.configInstanceID); } else { FIRLogWarning(kFIRLoggerRemoteConfig, @"I-RCN000055", @"Error getting iid : %@.", error); } + // Continue the fetch regardless of whether fetch of instance ID succeeded. - [instanceIDStrongSelf fetchCheckinInfoWithCompletionHandler:completionHandler]; - }]; + [strongSelfQueue fetchCheckinInfoWithCompletionHandler:completionHandler]; + }); + }]; - } else { + } else { + dispatch_async(self->_lockQueue, ^{ + RCNConfigFetch *strongSelfQueue = weakSelf; // Continue the fetch regardless of whether fetch of instance ID succeeded. - [strongSelf fetchCheckinInfoWithCompletionHandler:completionHandler]; - } - }); + [strongSelfQueue fetchCheckinInfoWithCompletionHandler:completionHandler]; + }); + } }; FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000039", @"Starting requesting token."); // Note: We expect the GCMSenderID to always be available by the time this request is made.