Skip to content

Commit 6d91c69

Browse files
feat: fetchAndActivate retry
1 parent de87551 commit 6d91c69

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

packages/firebase_remote_config/firebase_remote_config/ios/Classes/FLTFirebaseRemoteConfigPlugin.m

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,25 @@
77

88
#import "FLTFirebaseRemoteConfigPlugin.h"
99
#import "FLTFirebaseRemoteConfigUtils.h"
10+
#import "Reachability.h"
1011

1112
NSString *const kFirebaseRemoteConfigChannelName = @"plugins.flutter.io/firebase_remote_config";
1213

1314
@interface FLTFirebaseRemoteConfigPlugin ()
1415
@property(nonatomic, retain) FlutterMethodChannel *channel;
16+
@property(nonatomic) Reachability *hostReachability;
1517
@end
1618

1719
@implementation FLTFirebaseRemoteConfigPlugin
1820

21+
BOOL _canConnectToHost;
22+
BOOL _fetchAndActivateRetry;
23+
NSString *remoteHostName = @"firebaseremoteconfig.googleapis.com";
1924
+ (instancetype)sharedInstance {
2025
static dispatch_once_t onceToken;
2126
static FLTFirebaseRemoteConfigPlugin *instance;
27+
_canConnectToHost = true;
28+
_fetchAndActivateRetry = false;
2229

2330
dispatch_once(&onceToken, ^{
2431
instance = [[FLTFirebaseRemoteConfigPlugin alloc] init];
@@ -29,6 +36,13 @@ + (instancetype)sharedInstance {
2936
}
3037

3138
- (instancetype)init {
39+
self.hostReachability = [Reachability reachabilityWithHostName:remoteHostName];
40+
[self.hostReachability startNotifier];
41+
[[NSNotificationCenter defaultCenter] addObserver:self
42+
selector:@selector(reachabilityChanged:)
43+
name:kReachabilityChangedNotification
44+
object:nil];
45+
3246
self = [super init];
3347
return self;
3448
}
@@ -161,7 +175,19 @@ - (void)fetchAndActivate:(id)arguments withMethodCallResult:(FLTFirebaseMethodCa
161175
[remoteConfig fetchAndActivateWithCompletionHandler:^(
162176
FIRRemoteConfigFetchAndActivateStatus status, NSError *error) {
163177
if (error != nil) {
164-
result.error(nil, nil, nil, error);
178+
if (error.code == 999 && _fetchAndActivateRetry == false) {
179+
// Note: see issue for details: https://github.com/firebase/flutterfire/issues/6196
180+
// Only calling once as the issue noted describes how it works on second retry
181+
// Issue appears to indicate the error code is: 999
182+
_fetchAndActivateRetry = true;
183+
NSLog(@"FLTFirebaseRemoteConfigPlugin: Retrying `fetchAndActivate()` due to a cancelled "
184+
@"request with the error code: 999.");
185+
NSLog(@"FLTFirebaseRemoteConfigPlugin: '%@' host connection status: %s", remoteHostName,
186+
_canConnectToHost == true ? "REACHABLE" : "NOT REACHABLE");
187+
[self fetchAndActivate:arguments withMethodCallResult:result];
188+
} else {
189+
result.error(nil, nil, nil, error);
190+
}
165191
} else {
166192
if (status == FIRRemoteConfigFetchAndActivateStatusSuccessFetchedFromRemote) {
167193
result.success(@(YES));
@@ -224,6 +250,24 @@ - (NSString *)mapValueSource:(FIRRemoteConfigSource)source {
224250
}
225251
}
226252

253+
/*!
254+
* Called by Reachability whenever status changes.
255+
*/
256+
- (void)reachabilityChanged:(NSNotification *)note {
257+
// Reachability code inspired by Apple docs on checking host connectivity:
258+
// https://developer.apple.com/library/archive/samplecode/Reachability/Listings/Reachability_APLViewController_m.html#//apple_ref/doc/uid/DTS40007324-Reachability_APLViewController_m-DontLinkElementID_7
259+
Reachability *reachability = [note object];
260+
261+
if (reachability == self.hostReachability) {
262+
NetworkStatus netStatus = [reachability currentReachabilityStatus];
263+
if (netStatus == NotReachable) {
264+
_canConnectToHost = false;
265+
} else {
266+
_canConnectToHost = true;
267+
}
268+
}
269+
}
270+
227271
#pragma mark - FLTFirebasePlugin
228272

229273
- (void)didReinitializeFirebaseCore:(void (^)(void))completion {

0 commit comments

Comments
 (0)