FIRRemoteConfig.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseRemoteConfig/Sources/Public/FirebaseRemoteConfig/FIRRemoteConfig.h"
  17. #import "FirebaseABTesting/Sources/Private/FirebaseABTestingInternal.h"
  18. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  19. #import "FirebaseRemoteConfig/Sources/FIRRemoteConfigComponent.h"
  20. #import "FirebaseRemoteConfig/Sources/Private/FIRRemoteConfig_Private.h"
  21. #import "FirebaseRemoteConfig/Sources/Private/RCNConfigFetch.h"
  22. #import "FirebaseRemoteConfig/Sources/Private/RCNConfigSettings.h"
  23. #import "FirebaseRemoteConfig/Sources/RCNConfigConstants.h"
  24. #import "FirebaseRemoteConfig/Sources/RCNConfigContent.h"
  25. #import "FirebaseRemoteConfig/Sources/RCNConfigDBManager.h"
  26. #import "FirebaseRemoteConfig/Sources/RCNConfigExperiment.h"
  27. #import "FirebaseRemoteConfig/Sources/RCNConfigValue_Internal.h"
  28. #import "FirebaseRemoteConfig/Sources/RCNDevice.h"
  29. /// Remote Config Error Domain.
  30. /// TODO: Rename according to obj-c style for constants.
  31. NSString *const FIRRemoteConfigErrorDomain = @"com.google.remoteconfig.ErrorDomain";
  32. /// Remote Config Error Info End Time Seconds;
  33. NSString *const FIRRemoteConfigThrottledEndTimeInSecondsKey = @"error_throttled_end_time_seconds";
  34. /// Remote Config Developer Mode Key
  35. static NSString *const kRemoteConfigDeveloperKey = @"_rcn_developer";
  36. /// Minimum required time interval between fetch requests made to the backend.
  37. static NSString *const kRemoteConfigMinimumFetchIntervalKey = @"_rcn_minimum_fetch_interval";
  38. /// Timeout value for waiting on a fetch response.
  39. static NSString *const kRemoteConfigFetchTimeoutKey = @"_rcn_fetch_timeout";
  40. @interface FIRRemoteConfigSettings () {
  41. BOOL _developerModeEnabled;
  42. }
  43. @end
  44. @implementation FIRRemoteConfigSettings
  45. - (instancetype)init {
  46. self = [super init];
  47. if (self) {
  48. _minimumFetchInterval = RCNDefaultMinimumFetchInterval;
  49. _fetchTimeout = RCNHTTPDefaultConnectionTimeout;
  50. }
  51. return self;
  52. }
  53. @end
  54. @implementation FIRRemoteConfig {
  55. /// All the config content.
  56. RCNConfigContent *_configContent;
  57. RCNConfigDBManager *_DBManager;
  58. RCNConfigSettings *_settings;
  59. RCNConfigFetch *_configFetch;
  60. RCNConfigExperiment *_configExperiment;
  61. dispatch_queue_t _queue;
  62. NSString *_appName;
  63. }
  64. static NSMutableDictionary<NSString *, NSMutableDictionary<NSString *, FIRRemoteConfig *> *>
  65. *RCInstances;
  66. + (nonnull FIRRemoteConfig *)remoteConfigWithApp:(FIRApp *_Nonnull)firebaseApp {
  67. return [FIRRemoteConfig remoteConfigWithFIRNamespace:FIRNamespaceGoogleMobilePlatform
  68. app:firebaseApp];
  69. }
  70. + (nonnull FIRRemoteConfig *)remoteConfigWithFIRNamespace:(NSString *_Nonnull)firebaseNamespace {
  71. if (![FIRApp isDefaultAppConfigured]) {
  72. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000047",
  73. @"FIRApp not configured. Please make sure you have called [FIRApp configure]");
  74. // TODO: Maybe throw an exception here? That'd be a breaking change though, but at this point
  75. // RC can't work as expected.
  76. }
  77. return [FIRRemoteConfig remoteConfigWithFIRNamespace:firebaseNamespace app:[FIRApp defaultApp]];
  78. }
  79. + (nonnull FIRRemoteConfig *)remoteConfigWithFIRNamespace:(NSString *_Nonnull)firebaseNamespace
  80. app:(FIRApp *_Nonnull)firebaseApp {
  81. // Use the provider to generate and return instances of FIRRemoteConfig for this specific app and
  82. // namespace. This will ensure the app is configured before Remote Config can return an instance.
  83. id<FIRRemoteConfigProvider> provider =
  84. FIR_COMPONENT(FIRRemoteConfigProvider, firebaseApp.container);
  85. return [provider remoteConfigForNamespace:firebaseNamespace];
  86. }
  87. + (FIRRemoteConfig *)remoteConfig {
  88. // If the default app is not configured at this point, warn the developer.
  89. if (![FIRApp isDefaultAppConfigured]) {
  90. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000047",
  91. @"FIRApp not configured. Please make sure you have called [FIRApp configure]");
  92. // TODO: Maybe throw an exception here? That'd be a breaking change though, but at this point
  93. // RC can't work as expected.
  94. }
  95. return [FIRRemoteConfig remoteConfigWithFIRNamespace:FIRNamespaceGoogleMobilePlatform
  96. app:[FIRApp defaultApp]];
  97. }
  98. /// Singleton instance of serial queue for queuing all incoming RC calls.
  99. + (dispatch_queue_t)sharedRemoteConfigSerialQueue {
  100. static dispatch_once_t onceToken;
  101. static dispatch_queue_t sharedRemoteConfigQueue;
  102. dispatch_once(&onceToken, ^{
  103. sharedRemoteConfigQueue =
  104. dispatch_queue_create(RCNRemoteConfigQueueLabel, DISPATCH_QUEUE_SERIAL);
  105. });
  106. return sharedRemoteConfigQueue;
  107. }
  108. /// Designated initializer
  109. - (instancetype)initWithAppName:(NSString *)appName
  110. FIROptions:(FIROptions *)options
  111. namespace:(NSString *)FIRNamespace
  112. DBManager:(RCNConfigDBManager *)DBManager
  113. configContent:(RCNConfigContent *)configContent
  114. analytics:(nullable id<FIRAnalyticsInterop>)analytics {
  115. self = [super init];
  116. if (self) {
  117. _appName = appName;
  118. _DBManager = DBManager;
  119. // The fully qualified Firebase namespace is namespace:firappname.
  120. _FIRNamespace = [NSString stringWithFormat:@"%@:%@", FIRNamespace, appName];
  121. // Initialize RCConfigContent if not already.
  122. _configContent = configContent;
  123. _settings = [[RCNConfigSettings alloc] initWithDatabaseManager:_DBManager
  124. namespace:_FIRNamespace
  125. firebaseAppName:appName
  126. googleAppID:options.googleAppID];
  127. FIRExperimentController *experimentController = [FIRExperimentController sharedInstance];
  128. _configExperiment = [[RCNConfigExperiment alloc] initWithDBManager:_DBManager
  129. experimentController:experimentController];
  130. /// Serial queue for read and write lock.
  131. _queue = [FIRRemoteConfig sharedRemoteConfigSerialQueue];
  132. // Initialize with default config settings.
  133. [self setDefaultConfigSettings];
  134. _configFetch = [[RCNConfigFetch alloc] initWithContent:_configContent
  135. DBManager:_DBManager
  136. settings:_settings
  137. analytics:analytics
  138. experiment:_configExperiment
  139. queue:_queue
  140. namespace:_FIRNamespace
  141. options:options];
  142. [_settings loadConfigFromMetadataTable];
  143. }
  144. return self;
  145. }
  146. // Initialize with default config settings.
  147. - (void)setDefaultConfigSettings {
  148. // Set the default config settings.
  149. self->_settings.fetchTimeout = RCNHTTPDefaultConnectionTimeout;
  150. self->_settings.minimumFetchInterval = RCNDefaultMinimumFetchInterval;
  151. }
  152. - (void)ensureInitializedWithCompletionHandler:
  153. (nonnull FIRRemoteConfigInitializationCompletion)completionHandler {
  154. __weak FIRRemoteConfig *weakSelf = self;
  155. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
  156. FIRRemoteConfig *strongSelf = weakSelf;
  157. if (!strongSelf) {
  158. return;
  159. }
  160. BOOL initializationSuccess = [self->_configContent initializationSuccessful];
  161. NSError *error = nil;
  162. if (!initializationSuccess) {
  163. error = [[NSError alloc]
  164. initWithDomain:FIRRemoteConfigErrorDomain
  165. code:FIRRemoteConfigErrorInternalError
  166. userInfo:@{NSLocalizedDescriptionKey : @"Timed out waiting for database load."}];
  167. }
  168. completionHandler(error);
  169. });
  170. }
  171. #pragma mark - fetch
  172. - (void)fetchWithCompletionHandler:(FIRRemoteConfigFetchCompletion)completionHandler {
  173. dispatch_async(_queue, ^{
  174. [self fetchWithExpirationDuration:self->_settings.minimumFetchInterval
  175. completionHandler:completionHandler];
  176. });
  177. }
  178. - (void)fetchWithExpirationDuration:(NSTimeInterval)expirationDuration
  179. completionHandler:(FIRRemoteConfigFetchCompletion)completionHandler {
  180. FIRRemoteConfigFetchCompletion completionHandlerCopy = nil;
  181. if (completionHandler) {
  182. completionHandlerCopy = [completionHandler copy];
  183. }
  184. [_configFetch fetchConfigWithExpirationDuration:expirationDuration
  185. completionHandler:completionHandlerCopy];
  186. }
  187. #pragma mark - fetchAndActivate
  188. - (void)fetchAndActivateWithCompletionHandler:
  189. (FIRRemoteConfigFetchAndActivateCompletion)completionHandler {
  190. __weak FIRRemoteConfig *weakSelf = self;
  191. FIRRemoteConfigFetchCompletion fetchCompletion =
  192. ^(FIRRemoteConfigFetchStatus fetchStatus, NSError *fetchError) {
  193. FIRRemoteConfig *strongSelf = weakSelf;
  194. if (!strongSelf) {
  195. return;
  196. }
  197. // Fetch completed. We are being called on the main queue.
  198. // If fetch is successful, try to activate the fetched config
  199. if (fetchStatus == FIRRemoteConfigFetchStatusSuccess && !fetchError) {
  200. [strongSelf activateWithCompletion:^(BOOL changed, NSError *_Nullable activateError) {
  201. if (completionHandler) {
  202. FIRRemoteConfigFetchAndActivateStatus status =
  203. activateError ? FIRRemoteConfigFetchAndActivateStatusSuccessUsingPreFetchedData
  204. : FIRRemoteConfigFetchAndActivateStatusSuccessFetchedFromRemote;
  205. dispatch_async(dispatch_get_main_queue(), ^{
  206. completionHandler(status, nil);
  207. });
  208. }
  209. }];
  210. } else if (completionHandler) {
  211. FIRRemoteConfigFetchAndActivateStatus status =
  212. fetchStatus == FIRRemoteConfigFetchStatusSuccess
  213. ? FIRRemoteConfigFetchAndActivateStatusSuccessUsingPreFetchedData
  214. : FIRRemoteConfigFetchAndActivateStatusError;
  215. dispatch_async(dispatch_get_main_queue(), ^{
  216. completionHandler(status, fetchError);
  217. });
  218. }
  219. };
  220. [self fetchWithCompletionHandler:fetchCompletion];
  221. }
  222. #pragma mark - apply
  223. typedef void (^FIRRemoteConfigActivateChangeCompletion)(BOOL changed, NSError *_Nullable error);
  224. - (void)activateWithCompletion:(FIRRemoteConfigActivateChangeCompletion)completion {
  225. __weak FIRRemoteConfig *weakSelf = self;
  226. void (^applyBlock)(void) = ^(void) {
  227. FIRRemoteConfig *strongSelf = weakSelf;
  228. if (!strongSelf) {
  229. NSError *error = [NSError errorWithDomain:FIRRemoteConfigErrorDomain
  230. code:FIRRemoteConfigErrorInternalError
  231. userInfo:@{@"ActivationFailureReason" : @"Internal Error."}];
  232. if (completion) {
  233. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  234. completion(NO, error);
  235. });
  236. }
  237. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000068", @"Internal error activating config.");
  238. return;
  239. }
  240. // Check if the last fetched config has already been activated. Fetches with no data change are
  241. // ignored.
  242. if (strongSelf->_settings.lastETagUpdateTime == 0 ||
  243. strongSelf->_settings.lastETagUpdateTime <= strongSelf->_settings.lastApplyTimeInterval) {
  244. FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000069",
  245. @"Most recently fetched config is already activated.");
  246. if (completion) {
  247. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  248. completion(NO, nil);
  249. });
  250. }
  251. return;
  252. }
  253. [strongSelf->_configContent copyFromDictionary:self->_configContent.fetchedConfig
  254. toSource:RCNDBSourceActive
  255. forNamespace:self->_FIRNamespace];
  256. [strongSelf updateExperiments];
  257. strongSelf->_settings.lastApplyTimeInterval = [[NSDate date] timeIntervalSince1970];
  258. FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000069", @"Config activated.");
  259. if (completion) {
  260. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  261. completion(YES, nil);
  262. });
  263. }
  264. };
  265. dispatch_async(_queue, applyBlock);
  266. }
  267. - (void)updateExperiments {
  268. [self->_configExperiment updateExperiments];
  269. }
  270. #pragma mark - helpers
  271. - (NSString *)fullyQualifiedNamespace:(NSString *)namespace {
  272. // If this is already a fully qualified namespace, return.
  273. if ([namespace rangeOfString:@":"].location != NSNotFound) {
  274. return namespace;
  275. }
  276. NSString *fullyQualifiedNamespace = [NSString stringWithFormat:@"%@:%@", namespace, _appName];
  277. return fullyQualifiedNamespace;
  278. }
  279. #pragma mark - Get Config Result
  280. - (FIRRemoteConfigValue *)objectForKeyedSubscript:(NSString *)key {
  281. return [self configValueForKey:key];
  282. }
  283. - (FIRRemoteConfigValue *)configValueForKey:(NSString *)key {
  284. if (!key) {
  285. return [[FIRRemoteConfigValue alloc] initWithData:[NSData data]
  286. source:FIRRemoteConfigSourceStatic];
  287. }
  288. NSString *FQNamespace = [self fullyQualifiedNamespace:_FIRNamespace];
  289. __block FIRRemoteConfigValue *value;
  290. dispatch_sync(_queue, ^{
  291. value = self->_configContent.activeConfig[FQNamespace][key];
  292. if (value) {
  293. if (value.source != FIRRemoteConfigSourceRemote) {
  294. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000001",
  295. @"Key %@ should come from source:%zd instead coming from source: %zd.", key,
  296. (long)FIRRemoteConfigSourceRemote, (long)value.source);
  297. }
  298. return;
  299. }
  300. value = self->_configContent.defaultConfig[FQNamespace][key];
  301. if (value) {
  302. return;
  303. }
  304. value = [[FIRRemoteConfigValue alloc] initWithData:[NSData data]
  305. source:FIRRemoteConfigSourceStatic];
  306. });
  307. return value;
  308. }
  309. - (FIRRemoteConfigValue *)configValueForKey:(NSString *)key source:(FIRRemoteConfigSource)source {
  310. if (!key) {
  311. return [[FIRRemoteConfigValue alloc] initWithData:[NSData data]
  312. source:FIRRemoteConfigSourceStatic];
  313. }
  314. NSString *FQNamespace = [self fullyQualifiedNamespace:_FIRNamespace];
  315. __block FIRRemoteConfigValue *value;
  316. dispatch_sync(_queue, ^{
  317. if (source == FIRRemoteConfigSourceRemote) {
  318. value = self->_configContent.activeConfig[FQNamespace][key];
  319. } else if (source == FIRRemoteConfigSourceDefault) {
  320. value = self->_configContent.defaultConfig[FQNamespace][key];
  321. } else {
  322. value = [[FIRRemoteConfigValue alloc] initWithData:[NSData data]
  323. source:FIRRemoteConfigSourceStatic];
  324. }
  325. });
  326. return value;
  327. }
  328. - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
  329. objects:(id __unsafe_unretained[])stackbuf
  330. count:(NSUInteger)len {
  331. __block NSUInteger localValue;
  332. dispatch_sync(_queue, ^{
  333. localValue =
  334. [self->_configContent.activeConfig[self->_FIRNamespace] countByEnumeratingWithState:state
  335. objects:stackbuf
  336. count:len];
  337. });
  338. return localValue;
  339. }
  340. #pragma mark - Properties
  341. /// Last fetch completion time.
  342. - (NSDate *)lastFetchTime {
  343. __block NSDate *fetchTime;
  344. dispatch_sync(_queue, ^{
  345. NSTimeInterval lastFetchTime = self->_settings.lastFetchTimeInterval;
  346. fetchTime = [NSDate dateWithTimeIntervalSince1970:lastFetchTime];
  347. });
  348. return fetchTime;
  349. }
  350. - (FIRRemoteConfigFetchStatus)lastFetchStatus {
  351. __block FIRRemoteConfigFetchStatus currentStatus;
  352. dispatch_sync(_queue, ^{
  353. currentStatus = self->_settings.lastFetchStatus;
  354. });
  355. return currentStatus;
  356. }
  357. - (NSArray *)allKeysFromSource:(FIRRemoteConfigSource)source {
  358. __block NSArray *keys = [[NSArray alloc] init];
  359. dispatch_sync(_queue, ^{
  360. NSString *FQNamespace = [self fullyQualifiedNamespace:self->_FIRNamespace];
  361. switch (source) {
  362. case FIRRemoteConfigSourceDefault:
  363. if (self->_configContent.defaultConfig[FQNamespace]) {
  364. keys = [[self->_configContent.defaultConfig[FQNamespace] allKeys] copy];
  365. }
  366. break;
  367. case FIRRemoteConfigSourceRemote:
  368. if (self->_configContent.activeConfig[FQNamespace]) {
  369. keys = [[self->_configContent.activeConfig[FQNamespace] allKeys] copy];
  370. }
  371. break;
  372. default:
  373. break;
  374. }
  375. });
  376. return keys;
  377. }
  378. - (nonnull NSSet *)keysWithPrefix:(nullable NSString *)prefix {
  379. __block NSMutableSet *keys = [[NSMutableSet alloc] init];
  380. dispatch_sync(_queue, ^{
  381. NSString *FQNamespace = [self fullyQualifiedNamespace:self->_FIRNamespace];
  382. if (self->_configContent.activeConfig[FQNamespace]) {
  383. NSArray *allKeys = [self->_configContent.activeConfig[FQNamespace] allKeys];
  384. if (!prefix.length) {
  385. keys = [NSMutableSet setWithArray:allKeys];
  386. } else {
  387. for (NSString *key in allKeys) {
  388. if ([key hasPrefix:prefix]) {
  389. [keys addObject:key];
  390. }
  391. }
  392. }
  393. }
  394. });
  395. return [keys copy];
  396. }
  397. #pragma mark - Defaults
  398. - (void)setDefaults:(NSDictionary<NSString *, NSObject *> *)defaultConfig {
  399. NSString *FQNamespace = [self fullyQualifiedNamespace:_FIRNamespace];
  400. NSDictionary *defaultConfigCopy = [[NSDictionary alloc] init];
  401. if (defaultConfig) {
  402. defaultConfigCopy = [defaultConfig copy];
  403. }
  404. void (^setDefaultsBlock)(void) = ^(void) {
  405. NSDictionary *namespaceToDefaults = @{FQNamespace : defaultConfigCopy};
  406. [self->_configContent copyFromDictionary:namespaceToDefaults
  407. toSource:RCNDBSourceDefault
  408. forNamespace:FQNamespace];
  409. self->_settings.lastSetDefaultsTimeInterval = [[NSDate date] timeIntervalSince1970];
  410. };
  411. dispatch_async(_queue, setDefaultsBlock);
  412. }
  413. - (FIRRemoteConfigValue *)defaultValueForKey:(NSString *)key {
  414. NSString *FQNamespace = [self fullyQualifiedNamespace:_FIRNamespace];
  415. __block FIRRemoteConfigValue *value;
  416. dispatch_sync(_queue, ^{
  417. NSDictionary *defaultConfig = self->_configContent.defaultConfig;
  418. value = defaultConfig[FQNamespace][key];
  419. if (value) {
  420. if (value.source != FIRRemoteConfigSourceDefault) {
  421. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000002",
  422. @"Key %@ should come from source:%zd instead coming from source: %zd", key,
  423. (long)FIRRemoteConfigSourceDefault, (long)value.source);
  424. }
  425. }
  426. });
  427. return value;
  428. }
  429. - (void)setDefaultsFromPlistFileName:(nullable NSString *)fileName {
  430. if (!fileName || fileName.length == 0) {
  431. FIRLogWarning(kFIRLoggerRemoteConfig, @"I-RCN000037",
  432. @"The plist file '%@' could not be found by Remote Config.", fileName);
  433. return;
  434. }
  435. NSArray *bundles = @[ [NSBundle mainBundle], [NSBundle bundleForClass:[self class]] ];
  436. for (NSBundle *bundle in bundles) {
  437. NSString *plistFile = [bundle pathForResource:fileName ofType:@"plist"];
  438. // Use the first one we find.
  439. if (plistFile) {
  440. NSDictionary *defaultConfig = [[NSDictionary alloc] initWithContentsOfFile:plistFile];
  441. if (defaultConfig) {
  442. [self setDefaults:defaultConfig];
  443. }
  444. return;
  445. }
  446. }
  447. FIRLogWarning(kFIRLoggerRemoteConfig, @"I-RCN000037",
  448. @"The plist file '%@' could not be found by Remote Config.", fileName);
  449. }
  450. #pragma mark - custom variables
  451. - (FIRRemoteConfigSettings *)configSettings {
  452. __block NSTimeInterval minimumFetchInterval = RCNDefaultMinimumFetchInterval;
  453. __block NSTimeInterval fetchTimeout = RCNHTTPDefaultConnectionTimeout;
  454. dispatch_sync(_queue, ^{
  455. minimumFetchInterval = self->_settings.minimumFetchInterval;
  456. fetchTimeout = self->_settings.fetchTimeout;
  457. });
  458. FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000066",
  459. @"Successfully read configSettings. Minimum Fetch Interval:%f, "
  460. @"Fetch timeout: %f",
  461. minimumFetchInterval, fetchTimeout);
  462. FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
  463. settings.minimumFetchInterval = minimumFetchInterval;
  464. settings.fetchTimeout = fetchTimeout;
  465. /// The NSURLSession needs to be recreated whenever the fetch timeout may be updated.
  466. [_configFetch recreateNetworkSession];
  467. return settings;
  468. }
  469. - (void)setConfigSettings:(FIRRemoteConfigSettings *)configSettings {
  470. void (^setConfigSettingsBlock)(void) = ^(void) {
  471. if (!configSettings) {
  472. return;
  473. }
  474. self->_settings.minimumFetchInterval = configSettings.minimumFetchInterval;
  475. self->_settings.fetchTimeout = configSettings.fetchTimeout;
  476. /// The NSURLSession needs to be recreated whenever the fetch timeout may be updated.
  477. [self->_configFetch recreateNetworkSession];
  478. FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000067",
  479. @"Successfully set configSettings. Minimum Fetch Interval:%f, "
  480. @"Fetch timeout:%f",
  481. configSettings.minimumFetchInterval, configSettings.fetchTimeout);
  482. };
  483. dispatch_async(_queue, setConfigSettingsBlock);
  484. }
  485. @end