Skip to content

Commit abe7939

Browse files
authored
Realtime rc no change (#10188)
* Save template version in settings * Format files * set last template version to 0 * Store template version in userDefaults instead of deviceContents * return nil for unsuccessful fetches * fix typo * put addEtagToHeader flag to fetch and propagate it to surrounding functions * Add excludeEtagHeaderForRealtime flag to RCNConfigFetch * Update mocks for Swift test * Fix flag * Add commentting
1 parent ccef460 commit abe7939

File tree

7 files changed

+133
-40
lines changed

7 files changed

+133
-40
lines changed

FirebaseRemoteConfig/Sources/FIRRemoteConfig.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ - (void)fetchWithExpirationDuration:(NSTimeInterval)expirationDuration
247247
completionHandlerCopy = [completionHandler copy];
248248
}
249249
[_configFetch fetchConfigWithExpirationDuration:expirationDuration
250-
completionHandler:completionHandlerCopy];
250+
completionHandler:completionHandlerCopy
251+
excludeEtagHeaderForRealtime:false];
251252
}
252253

253254
#pragma mark - fetchAndActivate

FirebaseRemoteConfig/Sources/Private/RCNConfigFetch.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ typedef void (^RCNConfigFetcherCompletion)(NSData *data, NSURLResponse *response
4646

4747
/// Fetches config data keyed by namespace. Completion block will be called on the main queue.
4848
/// @param expirationDuration Expiration duration, in seconds.
49+
/// @param excludeEtagHeaderForRealtime Needed for fetches from Realtime when an UPDATE response is
50+
/// required
4951
/// @param completionHandler Callback handler.
5052
- (void)fetchConfigWithExpirationDuration:(NSTimeInterval)expirationDuration
51-
completionHandler:(FIRRemoteConfigFetchCompletion)completionHandler;
53+
completionHandler:(FIRRemoteConfigFetchCompletion)completionHandler
54+
excludeEtagHeaderForRealtime:(bool)excludeEtagHeaderForRealtime;
5255

5356
/// Add the ability to update NSURLSession's timeout after a session has already been created.
5457
- (void)recreateNetworkSession;

FirebaseRemoteConfig/Sources/RCNConfigFetch.m

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ - (void)dealloc {
127127
#pragma mark - Fetch Config API
128128

129129
- (void)fetchConfigWithExpirationDuration:(NSTimeInterval)expirationDuration
130-
completionHandler:(FIRRemoteConfigFetchCompletion)completionHandler {
130+
completionHandler:(FIRRemoteConfigFetchCompletion)completionHandler
131+
excludeEtagHeaderForRealtime:(bool)excludeEtagHeaderForRealtime {
131132
// Note: We expect the googleAppID to always be available.
132133
BOOL hasDeviceContextChanged =
133134
FIRRemoteConfigHasDeviceContextChanged(_settings.deviceContext, _options.googleAppID);
@@ -191,7 +192,8 @@ - (void)fetchConfigWithExpirationDuration:(NSTimeInterval)expirationDuration
191192
withError:error];
192193
}
193194
strongSelf->_settings.isFetchInProgress = YES;
194-
[strongSelf refreshInstallationsTokenWithCompletionHandler:completionHandler];
195+
[strongSelf refreshInstallationsTokenWithCompletionHandler:completionHandler
196+
excludeEtagHeaderForRealtime:excludeEtagHeaderForRealtime];
195197
});
196198
}
197199

@@ -203,7 +205,8 @@ - (NSString *)FIRAppNameFromFullyQualifiedNamespace {
203205
/// Refresh installation ID token before fetching config. installation ID is now mandatory for fetch
204206
/// requests to work.(b/14751422).
205207
- (void)refreshInstallationsTokenWithCompletionHandler:
206-
(FIRRemoteConfigFetchCompletion)completionHandler {
208+
(FIRRemoteConfigFetchCompletion)completionHandler
209+
excludeEtagHeaderForRealtime:(bool)excludeEtagHeaderForRealtime {
207210
FIRInstallations *installations = [FIRInstallations
208211
installationsWithApp:[FIRApp appNamed:[self FIRAppNameFromFullyQualifiedNamespace]]];
209212
if (!installations || !_options.GCMSenderID) {
@@ -283,7 +286,8 @@ - (void)refreshInstallationsTokenWithCompletionHandler:
283286

284287
FIRLogInfo(kFIRLoggerRemoteConfig, @"I-RCN000022", @"Success to get iid : %@.",
285288
strongSelfQueue->_settings.configInstallationsIdentifier);
286-
[strongSelf doFetchCall:completionHandler];
289+
[strongSelf doFetchCall:completionHandler
290+
excludeEtagHeaderForRealtime:excludeEtagHeaderForRealtime];
287291
});
288292
}];
289293
};
@@ -292,10 +296,13 @@ - (void)refreshInstallationsTokenWithCompletionHandler:
292296
[installations authTokenWithCompletion:installationsTokenHandler];
293297
}
294298

295-
- (void)doFetchCall:(FIRRemoteConfigFetchCompletion)completionHandler {
299+
- (void)doFetchCall:(FIRRemoteConfigFetchCompletion)completionHandler
300+
excludeEtagHeaderForRealtime:(bool)excludeEtagHeaderForRealtime {
296301
[self getAnalyticsUserPropertiesWithCompletionHandler:^(NSDictionary *userProperties) {
297302
dispatch_async(self->_lockQueue, ^{
298-
[self fetchWithUserProperties:userProperties completionHandler:completionHandler];
303+
[self fetchWithUserProperties:userProperties
304+
completionHandler:completionHandler
305+
excludeEtagHeaderForRealtime:excludeEtagHeaderForRealtime];
299306
});
300307
}];
301308
}
@@ -322,7 +329,8 @@ - (void)reportCompletionOnHandler:(FIRRemoteConfigFetchCompletion)completionHand
322329
}
323330

324331
- (void)fetchWithUserProperties:(NSDictionary *)userProperties
325-
completionHandler:(FIRRemoteConfigFetchCompletion)completionHandler {
332+
completionHandler:(FIRRemoteConfigFetchCompletion)completionHandler
333+
excludeEtagHeaderForRealtime:(bool)excludeEtagHeaderForRealtime {
326334
FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000061", @"Fetch with user properties initiated.");
327335

328336
NSString *postRequestString = [_settings nextRequestWithUserProperties:userProperties];
@@ -515,8 +523,10 @@ - (void)fetchWithUserProperties:(NSDictionary *)userProperties
515523

516524
FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000061", @"Making remote config fetch.");
517525

518-
NSURLSessionDataTask *dataTask = [self URLSessionDataTaskWithContent:compressedContent
519-
completionHandler:fetcherCompletion];
526+
NSURLSessionDataTask *dataTask =
527+
[self URLSessionDataTaskWithContent:compressedContent
528+
completionHandler:fetcherCompletion
529+
excludeEtagHeaderForRealtime:excludeEtagHeaderForRealtime];
520530
[dataTask resume];
521531
}
522532

@@ -556,7 +566,8 @@ - (NSURLSession *)newFetchSession {
556566

557567
- (NSURLSessionDataTask *)URLSessionDataTaskWithContent:(NSData *)content
558568
completionHandler:
559-
(RCNConfigFetcherCompletion)fetcherCompletion {
569+
(RCNConfigFetcherCompletion)fetcherCompletion
570+
excludeEtagHeaderForRealtime:(bool)excludeEtagHeaderForRealtime {
560571
NSURL *URL = [NSURL URLWithString:[self constructServerURL]];
561572
FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000046", @"%@",
562573
[NSString stringWithFormat:@"Making config request: %@", [URL absoluteString]]);
@@ -575,7 +586,7 @@ - (NSURLSessionDataTask *)URLSessionDataTaskWithContent:(NSData *)content
575586
[URLRequest setValue:@"gzip" forHTTPHeaderField:kContentEncodingHeaderName];
576587
[URLRequest setValue:@"gzip" forHTTPHeaderField:kAcceptEncodingHeaderName];
577588
// Set the eTag from the last successful fetch, if available.
578-
if (_settings.lastETag) {
589+
if (_settings.lastETag && !excludeEtagHeaderForRealtime) {
579590
[URLRequest setValue:_settings.lastETag forHTTPHeaderField:kIfNoneMatchETagHeaderName];
580591
}
581592
[URLRequest setHTTPBody:content];

FirebaseRemoteConfig/Sources/RCNConfigRealtime.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,10 @@ - (void)fetchLatestConfig:(NSInteger)remainingAttempts targetVersion:(NSInteger)
420420
[strongSelf autoFetch:remainingAttempts - 1 targetVersion:targetVersion];
421421
}
422422
} else {
423+
/// If the template version is it's default value, 0, then we need to exclude the Etag from
424+
/// the fetch to get a response with the current template version.
425+
bool excludeEtagHeaderForRealtime =
426+
[[strongSelf->_configFetch templateVersionNumber] integerValue] == 0;
423427
[strongSelf->_configFetch
424428
fetchConfigWithExpirationDuration:0
425429
completionHandler:^(FIRRemoteConfigFetchStatus status, NSError *error) {
@@ -451,7 +455,8 @@ - (void)fetchLatestConfig:(NSInteger)remainingAttempts targetVersion:(NSInteger)
451455
targetVersion:targetVersion];
452456
}
453457
}
454-
}];
458+
}
459+
excludeEtagHeaderForRealtime:excludeEtagHeaderForRealtime];
455460
}
456461
});
457462
}

FirebaseRemoteConfig/Tests/Unit/RCNPersonalizationTest.m

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
@interface RCNConfigFetch (ForTest)
3131
- (NSURLSessionDataTask *)URLSessionDataTaskWithContent:(NSData *)content
3232
completionHandler:
33-
(RCNConfigFetcherCompletion)fetcherCompletion;
33+
(RCNConfigFetcherCompletion)fetcherCompletion
34+
excludeEtagHeaderForRealtime:(bool)excludeEtagHeaderForRealtime;
3435

3536
- (void)fetchWithUserProperties:(NSDictionary *)userProperties
36-
completionHandler:(FIRRemoteConfigFetchCompletion)completionHandler;
37+
completionHandler:(FIRRemoteConfigFetchCompletion)completionHandler
38+
excludeEtagHeaderForRealtime:(bool)excludeEtagHeaderForRealtime;
3739
@end
3840

3941
@interface RCNPersonalizationTest : XCTestCase {
@@ -238,16 +240,20 @@ - (void)testRemoteConfigIntegration {
238240

239241
+ (id)mockFetchRequest {
240242
id configFetch = OCMClassMock([RCNConfigFetch class]);
241-
OCMStub([configFetch fetchConfigWithExpirationDuration:0 completionHandler:OCMOCK_ANY])
243+
OCMStub([configFetch fetchConfigWithExpirationDuration:0
244+
completionHandler:OCMOCK_ANY
245+
excludeEtagHeaderForRealtime:false])
242246
.ignoringNonObjectArgs()
243247
.andDo(^(NSInvocation *invocation) {
244248
__unsafe_unretained FIRRemoteConfigFetchCompletion handler;
245249
[invocation getArgument:&handler atIndex:3];
246-
[configFetch fetchWithUserProperties:[[NSDictionary alloc] init] completionHandler:handler];
250+
[configFetch fetchWithUserProperties:[[NSDictionary alloc] init]
251+
completionHandler:handler
252+
excludeEtagHeaderForRealtime:false];
247253
});
248-
OCMExpect([configFetch
249-
URLSessionDataTaskWithContent:[OCMArg any]
250-
completionHandler:[RCNPersonalizationTest mockResponseHandler]])
254+
OCMExpect([configFetch URLSessionDataTaskWithContent:[OCMArg any]
255+
completionHandler:[RCNPersonalizationTest mockResponseHandler]
256+
excludeEtagHeaderForRealtime:false])
251257
.andReturn(nil);
252258
return configFetch;
253259
}

0 commit comments

Comments
 (0)