@@ -41,18 +41,18 @@ @implementation RCNConfigContent {
41
41
RCNConfigDBManager *_DBManager;
42
42
// / Current bundle identifier;
43
43
NSString *_bundleIdentifier;
44
- // / Dispatch semaphore to block all config reads until we have read from the database. This only
44
+ // / Blocks all config reads until we have read from the database. This only
45
45
// / potentially blocks on the first read. Should be a no-wait for all subsequent reads once we
46
46
// / have data read into memory from the database.
47
- dispatch_semaphore_t _configLoadFromDBSemaphore ;
47
+ dispatch_group_t _dispatch_group ;
48
48
// / Boolean indicating if initial DB load of fetched,active and default config has succeeded.
49
49
BOOL _isConfigLoadFromDBCompleted;
50
50
// / Boolean indicating that the load from database has initiated at least once.
51
51
BOOL _isDatabaseLoadAlreadyInitiated;
52
52
}
53
53
54
54
// / Default timeout when waiting to read data from database.
55
- static const NSTimeInterval kDatabaseLoadTimeoutSecs = 30.0 ;
55
+ const NSTimeInterval kDatabaseLoadTimeoutSecs = 30.0 ;
56
56
57
57
// / Singleton instance of RCNConfigContent.
58
58
+ (instancetype )sharedInstance {
@@ -86,7 +86,8 @@ - (instancetype)initWithDBManager:(RCNConfigDBManager *)DBManager {
86
86
_bundleIdentifier = @" " ;
87
87
}
88
88
_DBManager = DBManager;
89
- _configLoadFromDBSemaphore = dispatch_semaphore_create (0 );
89
+ // Waits for both config and Personalization data to load.
90
+ _dispatch_group = dispatch_group_create ();
90
91
[self loadConfigFromMainTable ];
91
92
}
92
93
return self;
@@ -112,22 +113,25 @@ - (void)loadConfigFromMainTable {
112
113
NSAssert (!_isDatabaseLoadAlreadyInitiated, @" Database load has already been initiated" );
113
114
_isDatabaseLoadAlreadyInitiated = true ;
114
115
116
+ dispatch_group_enter (_dispatch_group);
115
117
[_DBManager
116
118
loadMainWithBundleIdentifier: _bundleIdentifier
117
119
completionHandler: ^(BOOL success, NSDictionary *fetchedConfig,
118
120
NSDictionary *activeConfig, NSDictionary *defaultConfig) {
119
121
self->_fetchedConfig = [fetchedConfig mutableCopy ];
120
122
self->_activeConfig = [activeConfig mutableCopy ];
121
123
self->_defaultConfig = [defaultConfig mutableCopy ];
122
- dispatch_semaphore_signal (self->_configLoadFromDBSemaphore );
124
+ dispatch_group_leave (self->_dispatch_group );
123
125
}];
124
126
125
127
// TODO(karenzeng): Refactor personalization to be returned in loadMainWithBundleIdentifier above
128
+ dispatch_group_enter (_dispatch_group);
126
129
[_DBManager loadPersonalizationWithCompletionHandler: ^(
127
130
BOOL success, NSDictionary *fetchedPersonalization,
128
131
NSDictionary *activePersonalization, NSDictionary *defaultConfig) {
129
132
self->_fetchedPersonalization = [fetchedPersonalization copy ];
130
133
self->_activePersonalization = [activePersonalization copy ];
134
+ dispatch_group_leave (self->_dispatch_group );
131
135
}];
132
136
}
133
137
@@ -359,12 +363,12 @@ - (NSDictionary *)getConfigAndMetadataForNamespace:(NSString *)FIRNamespace {
359
363
// / configs until load is done.
360
364
// / @return Database load completion status.
361
365
- (BOOL )checkAndWaitForInitialDatabaseLoad {
362
- // / Wait on semaphore until done. This should be a no-op for subsequent calls.
366
+ // / Wait until load is done. This should be a no-op for subsequent calls.
363
367
if (!_isConfigLoadFromDBCompleted) {
364
- long result = dispatch_semaphore_wait (
365
- _configLoadFromDBSemaphore ,
368
+ intptr_t isErrorOrTimeout = dispatch_group_wait (
369
+ _dispatch_group ,
366
370
dispatch_time (DISPATCH_TIME_NOW, (int64_t )(kDatabaseLoadTimeoutSecs * NSEC_PER_SEC)));
367
- if (result != 0 ) {
371
+ if (isErrorOrTimeout ) {
368
372
FIRLogError (kFIRLoggerRemoteConfig , @" I-RCN000048" ,
369
373
@" Timed out waiting for fetched config to be loaded from DB" );
370
374
return false ;
0 commit comments