Skip to content

Commit f964056

Browse files
dmandarpaulb777
authored andcommitted
Recreate an NSURLSession for updates to the fetch timeout. (#3683)
1 parent 5fd5ed2 commit f964056

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

FirebaseRemoteConfig/Sources/FIRRemoteConfig.m

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ - (instancetype)initWithAppName:(NSString *)appName
168168
/// Serial queue for read and write lock.
169169
_queue = [FIRRemoteConfig sharedRemoteConfigSerialQueue];
170170

171+
// Initialize with default config settings.
172+
[self setDefaultConfigSettings];
171173
_configFetch = [[RCNConfigFetch alloc] initWithContent:_configContent
172174
DBManager:_DBManager
173175
settings:_settings
@@ -178,12 +180,17 @@ - (instancetype)initWithAppName:(NSString *)appName
178180
options:options];
179181

180182
[_settings loadConfigFromMetadataTable];
181-
self->_settings.fetchTimeout = RCNHTTPDefaultConnectionTimeout;
182-
self->_settings.minimumFetchInterval = RCNDefaultMinimumFetchInterval;
183183
}
184184
return self;
185185
}
186186

187+
// Initialize with default config settings.
188+
- (void)setDefaultConfigSettings {
189+
// Set the default config settings.
190+
self->_settings.fetchTimeout = RCNHTTPDefaultConnectionTimeout;
191+
self->_settings.minimumFetchInterval = RCNDefaultMinimumFetchInterval;
192+
}
193+
187194
- (void)ensureInitializedWithCompletionHandler:
188195
(nonnull FIRRemoteConfigInitializationCompletion)completionHandler {
189196
__weak FIRRemoteConfig *weakSelf = self;
@@ -592,6 +599,8 @@ - (FIRRemoteConfigSettings *)configSettings {
592599
[[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:developerModeEnabled];
593600
settings.minimumFetchInterval = minimumFetchInterval;
594601
settings.fetchTimeout = fetchTimeout;
602+
/// The NSURLSession needs to be recreated whenever the fetch timeout may be updated.
603+
[_configFetch recreateNetworkSession];
595604
return settings;
596605
}
597606

@@ -607,6 +616,8 @@ - (void)setConfigSettings:(FIRRemoteConfigSettings *)configSettings {
607616
self->_settings.customVariables = settingsToSave;
608617
self->_settings.minimumFetchInterval = configSettings.minimumFetchInterval;
609618
self->_settings.fetchTimeout = configSettings.fetchTimeout;
619+
/// The NSURLSession needs to be recreated whenever the fetch timeout may be updated.
620+
[self->_configFetch recreateNetworkSession];
610621
FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000067",
611622
@"Successfully set configSettings. Developer Mode: %@, Minimum Fetch Interval:%f, "
612623
@"Fetch timeout:%f",

FirebaseRemoteConfig/Sources/RCNConfigFetch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ typedef void (^RCNConfigFetcherTestBlock)(RCNConfigFetcherCompletion completion)
5353
- (void)fetchAllConfigsWithExpirationDuration:(NSTimeInterval)expirationDuration
5454
completionHandler:(FIRRemoteConfigFetchCompletion)completionHandler;
5555

56+
/// Add the ability to update NSURLSession's timeout after a session has already been created.
57+
- (void)recreateNetworkSession;
58+
5659
/// Sets the test block to mock the fetch response instead of performing the fetch task from server.
5760
+ (void)setGlobalTestBlock:(RCNConfigFetcherTestBlock)block;
5861

FirebaseRemoteConfig/Sources/RCNFetch.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ - (instancetype)initWithContent:(RCNConfigContent *)content
110110
return self;
111111
}
112112

113+
/// Force a new NSURLSession creation for updated config.
114+
- (void)recreateNetworkSession {
115+
if (_fetchSession) {
116+
[_fetchSession invalidateAndCancel];
117+
}
118+
_fetchSession = [self newFetchSession];
119+
}
120+
121+
/// Return the current session. (Tests).
122+
- (NSURLSession *)currentNetworkSession {
123+
return _fetchSession;
124+
}
125+
113126
- (void)dealloc {
114127
[_fetchSession invalidateAndCancel];
115128
}

FirebaseRemoteConfig/Tests/Unit/RCNRemoteConfigTest.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ - (NSURLSessionDataTask *)URLSessionDataTaskWithContent:(NSData *)content
4949
- (void)fetchWithUserProperties:(NSDictionary *)userProperties
5050
completionHandler:(FIRRemoteConfigFetchCompletion)completionHandler;
5151
- (NSString *)constructServerURL;
52+
- (NSURLSession *)currentNetworkSession;
5253
@end
5354

5455
@interface FIRRemoteConfig (ForTest)
@@ -1138,6 +1139,10 @@ - (void)testSetFetchTimeoutConfigSetting {
11381139
settings.fetchTimeout = 1;
11391140
[_configInstances[i] setConfigSettings:settings];
11401141
XCTAssertEqual([_configInstances[i] configSettings].fetchTimeout, 1);
1142+
NSURLSession *networkSession = [_configFetch[i] currentNetworkSession];
1143+
XCTAssertNotNil(networkSession);
1144+
XCTAssertEqual(networkSession.configuration.timeoutIntervalForResource, 1);
1145+
XCTAssertEqual(networkSession.configuration.timeoutIntervalForRequest, 1);
11411146
}
11421147
}
11431148

0 commit comments

Comments
 (0)