RCNConfigSettings.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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/Private/RCNConfigSettings.h"
  17. #import "FirebaseRemoteConfig/Sources/RCNConfigConstants.h"
  18. #import "FirebaseRemoteConfig/Sources/RCNConfigDBManager.h"
  19. #import "FirebaseRemoteConfig/Sources/RCNConfigValue_Internal.h"
  20. #import "FirebaseRemoteConfig/Sources/RCNDevice.h"
  21. #import "FirebaseRemoteConfig/Sources/RCNUserDefaultsManager.h"
  22. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  23. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  24. static NSString *const kRCNGroupPrefix = @"frc.group.";
  25. static NSString *const kRCNUserDefaultsKeyNamelastETag = @"lastETag";
  26. static NSString *const kRCNUserDefaultsKeyNameLastSuccessfulFetchTime = @"lastSuccessfulFetchTime";
  27. static NSString *const kRCNAnalyticsFirstOpenTimePropertyName = @"_fot";
  28. static const int kRCNExponentialBackoffMinimumInterval = 60 * 2; // 2 mins.
  29. static const int kRCNExponentialBackoffMaximumInterval = 60 * 60 * 4; // 4 hours.
  30. @interface RCNConfigSettings () {
  31. /// A list of successful fetch timestamps in seconds.
  32. NSMutableArray *_successFetchTimes;
  33. /// A list of failed fetch timestamps in seconds.
  34. NSMutableArray *_failureFetchTimes;
  35. /// Device conditions since last successful fetch from the backend. Device conditions including
  36. /// app
  37. /// version, iOS version, device localte, language, GMP project ID and Game project ID. Used for
  38. /// determing whether to throttle.
  39. NSMutableDictionary *_deviceContext;
  40. /// Custom variables (aka App context digest). This is the pending custom variables request before
  41. /// fetching.
  42. NSMutableDictionary *_customVariables;
  43. /// Cached internal metadata from internal metadata table. It contains customized information such
  44. /// as HTTP connection timeout, HTTP read timeout, success/failure throttling rate and time
  45. /// interval. Client has the default value of each parameters, they are only saved in
  46. /// internalMetadata if they have been customize by developers.
  47. NSMutableDictionary *_internalMetadata;
  48. /// Last fetch status.
  49. FIRRemoteConfigFetchStatus _lastFetchStatus;
  50. /// Last fetch Error.
  51. FIRRemoteConfigError _lastFetchError;
  52. /// The time of last apply timestamp.
  53. NSTimeInterval _lastApplyTimeInterval;
  54. /// The time of last setDefaults timestamp.
  55. NSTimeInterval _lastSetDefaultsTimeInterval;
  56. /// The database manager.
  57. RCNConfigDBManager *_DBManager;
  58. // The namespace for this instance.
  59. NSString *_FIRNamespace;
  60. // The Google App ID of the configured FIRApp.
  61. NSString *_googleAppID;
  62. /// The user defaults manager scoped to this RC instance of FIRApp and namespace.
  63. RCNUserDefaultsManager *_userDefaultsManager;
  64. /// The timestamp of last eTag update.
  65. NSTimeInterval _lastETagUpdateTime;
  66. }
  67. @end
  68. @implementation RCNConfigSettings
  69. - (instancetype)initWithDatabaseManager:(RCNConfigDBManager *)manager
  70. namespace:(NSString *)FIRNamespace
  71. firebaseAppName:(NSString *)appName
  72. googleAppID:(NSString *)googleAppID {
  73. self = [super init];
  74. if (self) {
  75. _FIRNamespace = FIRNamespace;
  76. _googleAppID = googleAppID;
  77. _bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
  78. if (!_bundleIdentifier) {
  79. FIRLogNotice(kFIRLoggerRemoteConfig, @"I-RCN000038",
  80. @"Main bundle identifier is missing. Remote Config might not work properly.");
  81. _bundleIdentifier = @"";
  82. }
  83. _minimumFetchInterval = RCNDefaultMinimumFetchInterval;
  84. _deviceContext = [[NSMutableDictionary alloc] init];
  85. _customVariables = [[NSMutableDictionary alloc] init];
  86. _successFetchTimes = [[NSMutableArray alloc] init];
  87. _failureFetchTimes = [[NSMutableArray alloc] init];
  88. _DBManager = manager;
  89. _internalMetadata = [[_DBManager loadInternalMetadataTable] mutableCopy];
  90. if (!_internalMetadata) {
  91. _internalMetadata = [[NSMutableDictionary alloc] init];
  92. }
  93. _userDefaultsManager = [[RCNUserDefaultsManager alloc] initWithAppName:appName
  94. bundleID:_bundleIdentifier
  95. namespace:_FIRNamespace];
  96. // Check if the config database is new. If so, clear the configs saved in userDefaults.
  97. if ([_DBManager isNewDatabase]) {
  98. FIRLogNotice(kFIRLoggerRemoteConfig, @"I-RCN000072",
  99. @"New config database created. Resetting user defaults.");
  100. [_userDefaultsManager resetUserDefaults];
  101. }
  102. _isFetchInProgress = NO;
  103. _templateVersion = @"1";
  104. }
  105. return self;
  106. }
  107. #pragma mark - read from / update userDefaults
  108. - (NSString *)lastETag {
  109. return [_userDefaultsManager lastETag];
  110. }
  111. - (void)setLastETag:(NSString *)lastETag {
  112. [self setLastETagUpdateTime:[[NSDate date] timeIntervalSince1970]];
  113. [_userDefaultsManager setLastETag:lastETag];
  114. }
  115. - (void)setLastETagUpdateTime:(NSTimeInterval)lastETagUpdateTime {
  116. [_userDefaultsManager setLastETagUpdateTime:lastETagUpdateTime];
  117. }
  118. - (NSTimeInterval)lastFetchTimeInterval {
  119. return _userDefaultsManager.lastFetchTime;
  120. }
  121. - (NSTimeInterval)lastETagUpdateTime {
  122. return _userDefaultsManager.lastETagUpdateTime;
  123. }
  124. // TODO: Update logic for app extensions as required.
  125. - (void)updateLastFetchTimeInterval:(NSTimeInterval)lastFetchTimeInterval {
  126. _userDefaultsManager.lastFetchTime = lastFetchTimeInterval;
  127. }
  128. #pragma mark - load from DB
  129. - (NSDictionary *)loadConfigFromMetadataTable {
  130. NSDictionary *metadata = [[_DBManager loadMetadataWithBundleIdentifier:_bundleIdentifier
  131. namespace:_FIRNamespace] copy];
  132. if (metadata) {
  133. // TODO: Remove (all metadata in general) once ready to
  134. // migrate to user defaults completely.
  135. if (metadata[RCNKeyDeviceContext]) {
  136. self->_deviceContext = [metadata[RCNKeyDeviceContext] mutableCopy];
  137. if ([self->_deviceContext valueForKey:RCNFetchResponseKeyTemplateVersion]) {
  138. _templateVersion = [self->_deviceContext valueForKey:RCNFetchResponseKeyTemplateVersion];
  139. }
  140. }
  141. if (metadata[RCNKeyAppContext]) {
  142. self->_customVariables = [metadata[RCNKeyAppContext] mutableCopy];
  143. }
  144. if (metadata[RCNKeySuccessFetchTime]) {
  145. self->_successFetchTimes = [metadata[RCNKeySuccessFetchTime] mutableCopy];
  146. }
  147. if (metadata[RCNKeyFailureFetchTime]) {
  148. self->_failureFetchTimes = [metadata[RCNKeyFailureFetchTime] mutableCopy];
  149. }
  150. if (metadata[RCNKeyLastFetchStatus]) {
  151. self->_lastFetchStatus =
  152. (FIRRemoteConfigFetchStatus)[metadata[RCNKeyLastFetchStatus] intValue];
  153. }
  154. if (metadata[RCNKeyLastFetchError]) {
  155. self->_lastFetchError = (FIRRemoteConfigError)[metadata[RCNKeyLastFetchError] intValue];
  156. }
  157. if (metadata[RCNKeyLastApplyTime]) {
  158. self->_lastApplyTimeInterval = [metadata[RCNKeyLastApplyTime] doubleValue];
  159. }
  160. if (metadata[RCNKeyLastFetchStatus]) {
  161. self->_lastSetDefaultsTimeInterval = [metadata[RCNKeyLastSetDefaultsTime] doubleValue];
  162. }
  163. }
  164. return metadata;
  165. }
  166. #pragma mark - update DB/cached
  167. // Update internal metadata content to cache and DB.
  168. - (void)updateInternalContentWithResponse:(NSDictionary *)response {
  169. // Remove all the keys with current pakcage name.
  170. [_DBManager deleteRecordWithBundleIdentifier:_bundleIdentifier
  171. namespace:_FIRNamespace
  172. isInternalDB:YES];
  173. for (NSString *key in _internalMetadata.allKeys) {
  174. if ([key hasPrefix:_bundleIdentifier]) {
  175. [_internalMetadata removeObjectForKey:key];
  176. }
  177. }
  178. for (NSString *entry in response) {
  179. NSData *val = [response[entry] dataUsingEncoding:NSUTF8StringEncoding];
  180. NSArray *values = @[ entry, val ];
  181. _internalMetadata[entry] = response[entry];
  182. [self updateInternalMetadataTableWithValues:values];
  183. }
  184. }
  185. - (void)updateInternalMetadataTableWithValues:(NSArray *)values {
  186. [_DBManager insertInternalMetadataTableWithValues:values completionHandler:nil];
  187. }
  188. /// If the last fetch was not successful, update the (exponential backoff) period that we wait until
  189. /// fetching again. Any subsequent fetch requests will be checked and allowed only if past this
  190. /// throttle end time.
  191. - (void)updateExponentialBackoffTime {
  192. // If not in exponential backoff mode, reset the retry interval.
  193. if (_lastFetchStatus == FIRRemoteConfigFetchStatusSuccess) {
  194. FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000057",
  195. @"Throttling: Entering exponential backoff mode.");
  196. _exponentialBackoffRetryInterval = kRCNExponentialBackoffMinimumInterval;
  197. } else {
  198. FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000057",
  199. @"Throttling: Updating throttling interval.");
  200. // Double the retry interval until we hit the truncated exponential backoff. More info here:
  201. // https://cloud.google.com/storage/docs/exponential-backoff
  202. _exponentialBackoffRetryInterval =
  203. ((_exponentialBackoffRetryInterval * 2) < kRCNExponentialBackoffMaximumInterval)
  204. ? _exponentialBackoffRetryInterval * 2
  205. : _exponentialBackoffRetryInterval;
  206. }
  207. // Randomize the next retry interval.
  208. int randomPlusMinusInterval = ((arc4random() % 2) == 0) ? -1 : 1;
  209. NSTimeInterval randomizedRetryInterval =
  210. _exponentialBackoffRetryInterval +
  211. (0.5 * _exponentialBackoffRetryInterval * randomPlusMinusInterval);
  212. _exponentialBackoffThrottleEndTime =
  213. [[NSDate date] timeIntervalSince1970] + randomizedRetryInterval;
  214. }
  215. - (void)updateMetadataWithFetchSuccessStatus:(BOOL)fetchSuccess
  216. templateVersion:(NSString *)templateVersion {
  217. FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000056", @"Updating metadata with fetch result.");
  218. [self updateFetchTimeWithSuccessFetch:fetchSuccess];
  219. _lastFetchStatus =
  220. fetchSuccess ? FIRRemoteConfigFetchStatusSuccess : FIRRemoteConfigFetchStatusFailure;
  221. _lastFetchError = fetchSuccess ? FIRRemoteConfigErrorUnknown : FIRRemoteConfigErrorInternalError;
  222. if (fetchSuccess) {
  223. [self updateLastFetchTimeInterval:[[NSDate date] timeIntervalSince1970]];
  224. // Note: We expect the googleAppID to always be available.
  225. _deviceContext = FIRRemoteConfigDeviceContextWithProjectIdentifier(_googleAppID);
  226. [_deviceContext setValue:templateVersion forKey:RCNFetchResponseKeyTemplateVersion];
  227. }
  228. [self updateMetadataTable];
  229. }
  230. - (void)updateFetchTimeWithSuccessFetch:(BOOL)isSuccessfulFetch {
  231. NSTimeInterval epochTimeInterval = [[NSDate date] timeIntervalSince1970];
  232. if (isSuccessfulFetch) {
  233. [_successFetchTimes addObject:@(epochTimeInterval)];
  234. } else {
  235. [_failureFetchTimes addObject:@(epochTimeInterval)];
  236. }
  237. }
  238. - (void)updateMetadataTable {
  239. [_DBManager deleteRecordWithBundleIdentifier:_bundleIdentifier
  240. namespace:_FIRNamespace
  241. isInternalDB:NO];
  242. NSError *error;
  243. // Objects to be serialized cannot be invalid.
  244. if (!_bundleIdentifier) {
  245. return;
  246. }
  247. if (![NSJSONSerialization isValidJSONObject:_customVariables]) {
  248. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000028",
  249. @"Invalid custom variables to be serialized.");
  250. return;
  251. }
  252. if (![NSJSONSerialization isValidJSONObject:_deviceContext]) {
  253. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000029",
  254. @"Invalid device context to be serialized.");
  255. return;
  256. }
  257. if (![NSJSONSerialization isValidJSONObject:_successFetchTimes]) {
  258. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000031",
  259. @"Invalid success fetch times to be serialized.");
  260. return;
  261. }
  262. if (![NSJSONSerialization isValidJSONObject:_failureFetchTimes]) {
  263. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000032",
  264. @"Invalid failure fetch times to be serialized.");
  265. return;
  266. }
  267. NSData *serializedAppContext = [NSJSONSerialization dataWithJSONObject:_customVariables
  268. options:NSJSONWritingPrettyPrinted
  269. error:&error];
  270. NSData *serializedDeviceContext =
  271. [NSJSONSerialization dataWithJSONObject:_deviceContext
  272. options:NSJSONWritingPrettyPrinted
  273. error:&error];
  274. // The digestPerNamespace is not used and only meant for backwards DB compatibility.
  275. NSData *serializedDigestPerNamespace =
  276. [NSJSONSerialization dataWithJSONObject:@{} options:NSJSONWritingPrettyPrinted error:&error];
  277. NSData *serializedSuccessTime = [NSJSONSerialization dataWithJSONObject:_successFetchTimes
  278. options:NSJSONWritingPrettyPrinted
  279. error:&error];
  280. NSData *serializedFailureTime = [NSJSONSerialization dataWithJSONObject:_failureFetchTimes
  281. options:NSJSONWritingPrettyPrinted
  282. error:&error];
  283. if (!serializedDigestPerNamespace || !serializedDeviceContext || !serializedAppContext ||
  284. !serializedSuccessTime || !serializedFailureTime) {
  285. return;
  286. }
  287. NSDictionary *columnNameToValue = @{
  288. RCNKeyBundleIdentifier : _bundleIdentifier,
  289. RCNKeyNamespace : _FIRNamespace,
  290. RCNKeyFetchTime : @(self.lastFetchTimeInterval),
  291. RCNKeyDigestPerNamespace : serializedDigestPerNamespace,
  292. RCNKeyDeviceContext : serializedDeviceContext,
  293. RCNKeyAppContext : serializedAppContext,
  294. RCNKeySuccessFetchTime : serializedSuccessTime,
  295. RCNKeyFailureFetchTime : serializedFailureTime,
  296. RCNKeyLastFetchStatus : [NSString stringWithFormat:@"%ld", (long)_lastFetchStatus],
  297. RCNKeyLastFetchError : [NSString stringWithFormat:@"%ld", (long)_lastFetchError],
  298. RCNKeyLastApplyTime : @(_lastApplyTimeInterval),
  299. RCNKeyLastSetDefaultsTime : @(_lastSetDefaultsTimeInterval)
  300. };
  301. [_DBManager insertMetadataTableWithValues:columnNameToValue completionHandler:nil];
  302. }
  303. #pragma mark - fetch request
  304. /// Returns a fetch request with the latest device and config change.
  305. /// Whenever user issues a fetch api call, collect the latest request.
  306. - (NSString *)nextRequestWithUserProperties:(NSDictionary *)userProperties {
  307. // Note: We only set user properties as mentioned in the new REST API Design doc
  308. NSString *ret = [NSString stringWithFormat:@"{"];
  309. ret = [ret stringByAppendingString:[NSString stringWithFormat:@"app_instance_id:'%@'",
  310. _configInstallationsIdentifier]];
  311. ret = [ret stringByAppendingString:[NSString stringWithFormat:@", app_instance_id_token:'%@'",
  312. _configInstallationsToken]];
  313. ret = [ret stringByAppendingString:[NSString stringWithFormat:@", app_id:'%@'", _googleAppID]];
  314. ret = [ret stringByAppendingString:[NSString stringWithFormat:@", country_code:'%@'",
  315. FIRRemoteConfigDeviceCountry()]];
  316. ret = [ret stringByAppendingString:[NSString stringWithFormat:@", language_code:'%@'",
  317. FIRRemoteConfigDeviceLocale()]];
  318. ret = [ret
  319. stringByAppendingString:[NSString stringWithFormat:@", platform_version:'%@'",
  320. [GULAppEnvironmentUtil systemVersion]]];
  321. ret = [ret stringByAppendingString:[NSString stringWithFormat:@", time_zone:'%@'",
  322. FIRRemoteConfigTimezone()]];
  323. ret = [ret stringByAppendingString:[NSString stringWithFormat:@", package_name:'%@'",
  324. _bundleIdentifier]];
  325. ret = [ret stringByAppendingString:[NSString stringWithFormat:@", app_version:'%@'",
  326. FIRRemoteConfigAppVersion()]];
  327. ret = [ret stringByAppendingString:[NSString stringWithFormat:@", app_build:'%@'",
  328. FIRRemoteConfigAppBuildVersion()]];
  329. ret = [ret stringByAppendingString:[NSString stringWithFormat:@", sdk_version:'%@'",
  330. FIRRemoteConfigPodVersion()]];
  331. if (userProperties && userProperties.count > 0) {
  332. NSError *error;
  333. // Extract first open time from user properties and send as a separate field
  334. NSNumber *firstOpenTime = userProperties[kRCNAnalyticsFirstOpenTimePropertyName];
  335. NSMutableDictionary *remainingUserProperties = [userProperties mutableCopy];
  336. if (firstOpenTime != nil) {
  337. NSDate *date = [NSDate dateWithTimeIntervalSince1970:([firstOpenTime longValue] / 1000)];
  338. NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
  339. NSString *firstOpenTimeISOString = [formatter stringFromDate:date];
  340. ret = [ret stringByAppendingString:[NSString stringWithFormat:@", first_open_time:'%@'",
  341. firstOpenTimeISOString]];
  342. [remainingUserProperties removeObjectForKey:kRCNAnalyticsFirstOpenTimePropertyName];
  343. }
  344. if (remainingUserProperties.count > 0) {
  345. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:remainingUserProperties
  346. options:0
  347. error:&error];
  348. if (!error) {
  349. ret = [ret
  350. stringByAppendingString:[NSString
  351. stringWithFormat:@", analytics_user_properties:%@",
  352. [[NSString alloc]
  353. initWithData:jsonData
  354. encoding:NSUTF8StringEncoding]]];
  355. }
  356. }
  357. }
  358. ret = [ret stringByAppendingString:@"}"];
  359. return ret;
  360. }
  361. #pragma mark - getter/setter
  362. - (void)setLastFetchError:(FIRRemoteConfigError)lastFetchError {
  363. if (_lastFetchError != lastFetchError) {
  364. _lastFetchError = lastFetchError;
  365. [_DBManager updateMetadataWithOption:RCNUpdateOptionFetchStatus
  366. namespace:_FIRNamespace
  367. values:@[ @(_lastFetchStatus), @(_lastFetchError) ]
  368. completionHandler:nil];
  369. }
  370. }
  371. - (NSArray *)successFetchTimes {
  372. return [_successFetchTimes copy];
  373. }
  374. - (NSArray *)failureFetchTimes {
  375. return [_failureFetchTimes copy];
  376. }
  377. - (NSDictionary *)customVariables {
  378. return [_customVariables copy];
  379. }
  380. - (NSDictionary *)internalMetadata {
  381. return [_internalMetadata copy];
  382. }
  383. - (NSDictionary *)deviceContext {
  384. return [_deviceContext copy];
  385. }
  386. - (void)setCustomVariables:(NSDictionary *)customVariables {
  387. _customVariables = [[NSMutableDictionary alloc] initWithDictionary:customVariables];
  388. [self updateMetadataTable];
  389. }
  390. - (void)setMinimumFetchInterval:(NSTimeInterval)minimumFetchInterval {
  391. if (minimumFetchInterval < 0) {
  392. _minimumFetchInterval = 0;
  393. } else {
  394. _minimumFetchInterval = minimumFetchInterval;
  395. }
  396. }
  397. - (void)setFetchTimeout:(NSTimeInterval)fetchTimeout {
  398. if (fetchTimeout <= 0) {
  399. _fetchTimeout = RCNHTTPDefaultConnectionTimeout;
  400. } else {
  401. _fetchTimeout = fetchTimeout;
  402. }
  403. }
  404. - (void)setLastApplyTimeInterval:(NSTimeInterval)lastApplyTimestamp {
  405. _lastApplyTimeInterval = lastApplyTimestamp;
  406. [_DBManager updateMetadataWithOption:RCNUpdateOptionApplyTime
  407. namespace:_FIRNamespace
  408. values:@[ @(lastApplyTimestamp) ]
  409. completionHandler:nil];
  410. }
  411. - (void)setLastSetDefaultsTimeInterval:(NSTimeInterval)lastSetDefaultsTimestamp {
  412. _lastSetDefaultsTimeInterval = lastSetDefaultsTimestamp;
  413. [_DBManager updateMetadataWithOption:RCNUpdateOptionDefaultTime
  414. namespace:_FIRNamespace
  415. values:@[ @(lastSetDefaultsTimestamp) ]
  416. completionHandler:nil];
  417. }
  418. #pragma mark Throttling
  419. - (BOOL)hasMinimumFetchIntervalElapsed:(NSTimeInterval)minimumFetchInterval {
  420. if (self.lastFetchTimeInterval == 0) return YES;
  421. // Check if last config fetch is within minimum fetch interval in seconds.
  422. NSTimeInterval diffInSeconds = [[NSDate date] timeIntervalSince1970] - self.lastFetchTimeInterval;
  423. return diffInSeconds > minimumFetchInterval;
  424. }
  425. - (BOOL)shouldThrottle {
  426. NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
  427. return ((self.lastFetchTimeInterval > 0) &&
  428. (_lastFetchStatus != FIRRemoteConfigFetchStatusSuccess) &&
  429. (_exponentialBackoffThrottleEndTime - now > 0));
  430. }
  431. @end