Skip to content

Commit 2cd0c6e

Browse files
authored
Use completion handler when updating experiments after activation (#6711)
* Use completion handler when updating experiments after activation * Fix Travis * Add changelog * Fix whitespace * Nonnull initializers
1 parent fad62c0 commit 2cd0c6e

File tree

6 files changed

+29
-22
lines changed

6 files changed

+29
-22
lines changed

FirebaseRemoteConfig/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- [fixed] Completion handler for `fetchAndActivateWithCompletionHandler` is now run on the main thread. (#5897)
77
- [fixed] Fixed database creation on tvOS. (#6612)
88
- [changed] Updated public API documentation to no longer reference removed APIs. (#6641)
9+
- [fixed] Updated `activateWithCompletion:` to use completion handler for experiment updates. (#3687)
910

1011
# v4.9.1
1112
- [fixed] Fix an `attempt to insert nil object` crash in `fetchWithExpirationDuration:`. (#6522)

FirebaseRemoteConfig/Sources/FIRRemoteConfig.m

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -284,22 +284,19 @@ - (void)activateWithCompletion:(FIRRemoteConfigActivateChangeCompletion)completi
284284
[strongSelf->_configContent copyFromDictionary:self->_configContent.fetchedConfig
285285
toSource:RCNDBSourceActive
286286
forNamespace:self->_FIRNamespace];
287-
[strongSelf updateExperiments];
288287
strongSelf->_settings.lastApplyTimeInterval = [[NSDate date] timeIntervalSince1970];
289288
FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000069", @"Config activated.");
290-
if (completion) {
291-
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
292-
completion(YES, nil);
293-
});
294-
}
289+
[strongSelf->_configExperiment updateExperimentsWithHandler:^(NSError *_Nullable error) {
290+
if (completion) {
291+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
292+
completion(YES, nil);
293+
});
294+
}
295+
}];
295296
};
296297
dispatch_async(_queue, applyBlock);
297298
}
298299

299-
- (void)updateExperiments {
300-
[self->_configExperiment updateExperiments];
301-
}
302-
303300
#pragma mark - helpers
304301
- (NSString *)fullyQualifiedNamespace:(NSString *)namespace {
305302
// If this is already a fully qualified namespace, return.

FirebaseRemoteConfig/Sources/RCNConfigExperiment.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
@interface RCNConfigExperiment : NSObject
2424

2525
/// Designated initializer;
26-
- (instancetype)initWithDBManager:(RCNConfigDBManager *)DBManager
27-
experimentController:(FIRExperimentController *)controller NS_DESIGNATED_INITIALIZER;
26+
- (nonnull instancetype)initWithDBManager:(RCNConfigDBManager *_Nullable)DBManager
27+
experimentController:(FIRExperimentController *_Nullable)controller
28+
NS_DESIGNATED_INITIALIZER;
2829

2930
/// Use `initWithDBManager:` instead.
30-
- (instancetype)init NS_UNAVAILABLE;
31+
- (nonnull instancetype)init NS_UNAVAILABLE;
3132

3233
/// Update/Persist experiment information from config fetch response.
33-
- (void)updateExperimentsWithResponse:(NSArray<NSDictionary<NSString *, id> *> *)response;
34+
- (void)updateExperimentsWithResponse:(NSArray<NSDictionary<NSString *, id> *> *_Nullable)response;
3435

3536
/// Update experiments to Firebase Analytics when `activateWithCompletion:` happens.
36-
- (void)updateExperiments;
37+
- (void)updateExperimentsWithHandler:(nullable void (^)(NSError *_Nullable error))handler;
3738
@end

FirebaseRemoteConfig/Sources/RCNConfigExperiment.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ - (void)updateExperimentsWithResponse:(NSArray<NSDictionary<NSString *, id> *> *
111111
}
112112
}
113113

114-
- (void)updateExperiments {
114+
- (void)updateExperimentsWithHandler:(void (^)(NSError *_Nullable))handler {
115115
FIRLifecycleEvents *lifecycleEvent = [[FIRLifecycleEvents alloc] init];
116116

117117
// Get the last experiment start time prior to the latest payload.
@@ -126,7 +126,7 @@ - (void)updateExperiments {
126126
policy:ABTExperimentPayloadExperimentOverflowPolicyDiscardOldest
127127
lastStartTime:lastStartTime
128128
payloads:_experimentPayloads
129-
completionHandler:nil];
129+
completionHandler:handler];
130130
}
131131

132132
- (void)updateExperimentStartTime {

FirebaseRemoteConfig/Tests/Unit/RCNConfigExperimentTest.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,11 @@ - (void)testUpdateExperiments {
227227

228228
experiment.experimentPayloads = [@[ payloadData ] mutableCopy];
229229

230-
[experiment updateExperiments];
231-
XCTAssertEqualObjects(experiment.experimentMetadata[@"last_experiment_start_time"], @(12345678));
230+
[experiment updateExperimentsWithHandler:^(NSError *_Nullable error) {
231+
XCTAssertNil(error);
232+
XCTAssertEqualObjects(experiment.experimentMetadata[@"last_experiment_start_time"],
233+
@(12345678));
234+
}];
232235
}
233236

234237
#pragma mark Helpers.

FirebaseRemoteConfig/Tests/Unit/RCNRemoteConfigTest.m

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#import "FirebaseRemoteConfig/Sources/Public/FirebaseRemoteConfig/FIRRemoteConfig.h"
2424
#import "FirebaseRemoteConfig/Sources/RCNConfigConstants.h"
2525
#import "FirebaseRemoteConfig/Sources/RCNConfigDBManager.h"
26+
#import "FirebaseRemoteConfig/Sources/RCNConfigExperiment.h"
2627
#import "FirebaseRemoteConfig/Sources/RCNUserDefaultsManager.h"
2728

2829
#import "FirebaseRemoteConfig/Tests/Unit/RCNTestUtilities.h"
@@ -99,6 +100,7 @@ @interface RCNRemoteConfigTest : XCTestCase {
99100
NSString *_userDefaultsSuiteName;
100101
NSString *_DBPath;
101102
id _DBManagerMock;
103+
id _experimentMock;
102104
id _userDefaultsMock;
103105
}
104106
@end
@@ -123,6 +125,10 @@ - (void)setUp {
123125
OCMStub([_userDefaultsMock sharedUserDefaultsForBundleIdentifier:[OCMArg any]])
124126
.andReturn(_userDefaults);
125127

128+
_experimentMock = OCMClassMock([RCNConfigExperiment class]);
129+
OCMStub([_experimentMock
130+
updateExperimentsWithHandler:([OCMArg invokeBlockWithArgs:[NSNull null], nil])]);
131+
126132
RCNConfigContent *configContent = [[RCNConfigContent alloc] initWithDBManager:_DBManager];
127133
_configInstances = [[NSMutableArray alloc] initWithCapacity:3];
128134
_entries = [[NSMutableArray alloc] initWithCapacity:3];
@@ -172,7 +178,6 @@ - (void)setUp {
172178
DBManager:_DBManager
173179
configContent:configContent
174180
analytics:nil]);
175-
176181
_configInstances[i] = config;
177182
RCNConfigSettings *settings =
178183
[[RCNConfigSettings alloc] initWithDatabaseManager:_DBManager
@@ -186,7 +191,7 @@ - (void)setUp {
186191
DBManager:_DBManager
187192
settings:settings
188193
analytics:nil
189-
experiment:nil
194+
experiment:_experimentMock
190195
queue:queue
191196
namespace:fullyQualifiedNamespace
192197
options:currentOptions]);
@@ -221,7 +226,7 @@ __unsafe_unretained void (^handler)(FIRRemoteConfigFetchStatus status,
221226
[_configInstances[i] updateWithNewInstancesForConfigFetch:_configFetch[i]
222227
configContent:configContent
223228
configSettings:settings
224-
configExperiment:nil];
229+
configExperiment:_experimentMock];
225230
}
226231
}
227232

0 commit comments

Comments
 (0)