FIRRemoteConfig.m 22 KB

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