RCNConfigContent.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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/RCNConfigContent.h"
  17. #import "FirebaseRemoteConfig/Sources/Private/FIRRemoteConfig_Private.h"
  18. #import "FirebaseRemoteConfig/Sources/Public/FirebaseRemoteConfig/FIRRemoteConfig.h"
  19. #import "FirebaseRemoteConfig/Sources/RCNConfigConstants.h"
  20. #import "FirebaseRemoteConfig/Sources/RCNConfigDBManager.h"
  21. #import "FirebaseRemoteConfig/Sources/RCNConfigDefines.h"
  22. #import "FirebaseRemoteConfig/Sources/RCNConfigValue_Internal.h"
  23. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  24. @implementation RCNConfigContent {
  25. /// Active config data that is currently used.
  26. NSMutableDictionary *_activeConfig;
  27. /// Pending config (aka Fetched config) data that is latest data from server that might or might
  28. /// not be applied.
  29. NSMutableDictionary *_fetchedConfig;
  30. /// Default config provided by user.
  31. NSMutableDictionary *_defaultConfig;
  32. /// Active Personalization metadata that is currently used.
  33. NSDictionary *_activePersonalization;
  34. /// Pending Personalization metadata that is latest data from server that might or might not be
  35. /// applied.
  36. NSDictionary *_fetchedPersonalization;
  37. /// Active Rollout metadata that is currently used.
  38. NSArray<NSDictionary *> *_activeRolloutMetadata;
  39. /// Pending Rollout metadata that is latest data from server that might or might not be applied.
  40. NSArray<NSDictionary *> *_fetchedRolloutMetadata;
  41. /// DBManager
  42. RCNConfigDBManager *_DBManager;
  43. /// Current bundle identifier;
  44. NSString *_bundleIdentifier;
  45. /// Blocks all config reads until we have read from the database. This only
  46. /// potentially blocks on the first read. Should be a no-wait for all subsequent reads once we
  47. /// have data read into memory from the database.
  48. dispatch_group_t _dispatch_group;
  49. /// Boolean indicating if initial DB load of fetched,active and default config has succeeded.
  50. BOOL _isConfigLoadFromDBCompleted;
  51. /// Boolean indicating that the load from database has initiated at least once.
  52. BOOL _isDatabaseLoadAlreadyInitiated;
  53. }
  54. /// Default timeout when waiting to read data from database.
  55. const NSTimeInterval kDatabaseLoadTimeoutSecs = 30.0;
  56. /// Singleton instance of RCNConfigContent.
  57. + (instancetype)sharedInstance {
  58. static dispatch_once_t onceToken;
  59. static RCNConfigContent *sharedInstance;
  60. dispatch_once(&onceToken, ^{
  61. sharedInstance =
  62. [[RCNConfigContent alloc] initWithDBManager:[RCNConfigDBManager sharedInstance]];
  63. });
  64. return sharedInstance;
  65. }
  66. - (instancetype)init {
  67. NSAssert(NO, @"Invalid initializer.");
  68. return nil;
  69. }
  70. /// Designated initializer
  71. - (instancetype)initWithDBManager:(RCNConfigDBManager *)DBManager {
  72. self = [super init];
  73. if (self) {
  74. _activeConfig = [[NSMutableDictionary alloc] init];
  75. _fetchedConfig = [[NSMutableDictionary alloc] init];
  76. _defaultConfig = [[NSMutableDictionary alloc] init];
  77. _activePersonalization = [[NSDictionary alloc] init];
  78. _fetchedPersonalization = [[NSDictionary alloc] init];
  79. _activeRolloutMetadata = [[NSArray alloc] init];
  80. _fetchedRolloutMetadata = [[NSArray alloc] init];
  81. _bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
  82. if (!_bundleIdentifier) {
  83. FIRLogNotice(kFIRLoggerRemoteConfig, @"I-RCN000038",
  84. @"Main bundle identifier is missing. Remote Config might not work properly.");
  85. _bundleIdentifier = @"";
  86. }
  87. _DBManager = DBManager;
  88. // Waits for both config and Personalization data to load.
  89. _dispatch_group = dispatch_group_create();
  90. [self loadConfigFromMainTable];
  91. }
  92. return self;
  93. }
  94. // Blocking call that returns true/false once database load completes / times out.
  95. // @return Initialization status.
  96. - (BOOL)initializationSuccessful {
  97. RCN_MUST_NOT_BE_MAIN_THREAD();
  98. BOOL isDatabaseLoadSuccessful = [self checkAndWaitForInitialDatabaseLoad];
  99. return isDatabaseLoadSuccessful;
  100. }
  101. #pragma mark - database
  102. /// This method is only meant to be called at init time. The underlying logic will need to be
  103. /// reevaluated if the assumption changes at a later time.
  104. - (void)loadConfigFromMainTable {
  105. if (!_DBManager) {
  106. return;
  107. }
  108. NSAssert(!_isDatabaseLoadAlreadyInitiated, @"Database load has already been initiated");
  109. _isDatabaseLoadAlreadyInitiated = true;
  110. dispatch_group_enter(_dispatch_group);
  111. [_DBManager loadMainWithBundleIdentifier:_bundleIdentifier
  112. completionHandler:^(
  113. BOOL success, NSDictionary *fetchedConfig, NSDictionary *activeConfig,
  114. NSDictionary *defaultConfig, NSDictionary *rolloutMetadata) {
  115. self->_fetchedConfig = [fetchedConfig mutableCopy];
  116. self->_activeConfig = [activeConfig mutableCopy];
  117. self->_defaultConfig = [defaultConfig mutableCopy];
  118. self->_fetchedRolloutMetadata =
  119. [rolloutMetadata[@RCNRolloutTableKeyFetchedMetadata] copy];
  120. self->_activeRolloutMetadata =
  121. [rolloutMetadata[@RCNRolloutTableKeyActiveMetadata] copy];
  122. dispatch_group_leave(self->_dispatch_group);
  123. }];
  124. // TODO(karenzeng): Refactor personalization to be returned in loadMainWithBundleIdentifier above
  125. dispatch_group_enter(_dispatch_group);
  126. [_DBManager
  127. loadPersonalizationWithCompletionHandler:^(
  128. BOOL success, NSDictionary *fetchedPersonalization, NSDictionary *activePersonalization,
  129. NSDictionary *defaultConfig, NSDictionary *rolloutMetadata) {
  130. self->_fetchedPersonalization = [fetchedPersonalization copy];
  131. self->_activePersonalization = [activePersonalization copy];
  132. dispatch_group_leave(self->_dispatch_group);
  133. }];
  134. }
  135. /// Update the current config result to main table.
  136. /// @param values Values in a row to write to the table.
  137. /// @param source The source the config data is coming from. It determines which table to write to.
  138. - (void)updateMainTableWithValues:(NSArray *)values fromSource:(RCNDBSource)source {
  139. [_DBManager insertMainTableWithValues:values fromSource:source completionHandler:nil];
  140. }
  141. #pragma mark - update
  142. /// This function is for copying dictionary when user set up a default config or when user clicks
  143. /// activate. For now the DBSource can only be Active or Default.
  144. - (void)copyFromDictionary:(NSDictionary *)fromDict
  145. toSource:(RCNDBSource)DBSource
  146. forNamespace:(NSString *)FIRNamespace {
  147. // Make sure database load has completed.
  148. [self checkAndWaitForInitialDatabaseLoad];
  149. NSMutableDictionary *toDict;
  150. if (!fromDict) {
  151. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000007",
  152. @"The source dictionary to copy from does not exist.");
  153. return;
  154. }
  155. FIRRemoteConfigSource source = FIRRemoteConfigSourceRemote;
  156. switch (DBSource) {
  157. case RCNDBSourceDefault:
  158. toDict = _defaultConfig;
  159. source = FIRRemoteConfigSourceDefault;
  160. break;
  161. case RCNDBSourceFetched:
  162. FIRLogWarning(kFIRLoggerRemoteConfig, @"I-RCN000008",
  163. @"This shouldn't happen. Destination dictionary should never be pending type.");
  164. return;
  165. case RCNDBSourceActive:
  166. toDict = _activeConfig;
  167. source = FIRRemoteConfigSourceRemote;
  168. [toDict removeObjectForKey:FIRNamespace];
  169. break;
  170. default:
  171. toDict = _activeConfig;
  172. source = FIRRemoteConfigSourceRemote;
  173. [toDict removeObjectForKey:FIRNamespace];
  174. break;
  175. }
  176. // Completely wipe out DB first.
  177. [_DBManager deleteRecordFromMainTableWithNamespace:FIRNamespace
  178. bundleIdentifier:_bundleIdentifier
  179. fromSource:DBSource];
  180. toDict[FIRNamespace] = [[NSMutableDictionary alloc] init];
  181. NSDictionary *config = fromDict[FIRNamespace];
  182. for (NSString *key in config) {
  183. if (DBSource == RCNDBSourceDefault) {
  184. NSObject *value = config[key];
  185. NSData *valueData;
  186. if ([value isKindOfClass:[NSData class]]) {
  187. valueData = (NSData *)value;
  188. } else if ([value isKindOfClass:[NSString class]]) {
  189. valueData = [(NSString *)value dataUsingEncoding:NSUTF8StringEncoding];
  190. } else if ([value isKindOfClass:[NSNumber class]]) {
  191. NSString *strValue = [(NSNumber *)value stringValue];
  192. valueData = [(NSString *)strValue dataUsingEncoding:NSUTF8StringEncoding];
  193. } else if ([value isKindOfClass:[NSDate class]]) {
  194. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  195. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  196. NSString *strValue = [dateFormatter stringFromDate:(NSDate *)value];
  197. valueData = [(NSString *)strValue dataUsingEncoding:NSUTF8StringEncoding];
  198. } else if ([value isKindOfClass:[NSArray class]]) {
  199. NSError *error;
  200. valueData = [NSJSONSerialization dataWithJSONObject:value options:0 error:&error];
  201. if (error) {
  202. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000076", @"Invalid array value for key '%@'",
  203. key);
  204. }
  205. } else if ([value isKindOfClass:[NSDictionary class]]) {
  206. NSError *error;
  207. valueData = [NSJSONSerialization dataWithJSONObject:value options:0 error:&error];
  208. if (error) {
  209. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000077",
  210. @"Invalid dictionary value for key '%@'", key);
  211. }
  212. } else {
  213. continue;
  214. }
  215. toDict[FIRNamespace][key] = [[FIRRemoteConfigValue alloc] initWithData:valueData
  216. source:source];
  217. NSArray *values = @[ _bundleIdentifier, FIRNamespace, key, valueData ];
  218. [self updateMainTableWithValues:values fromSource:DBSource];
  219. } else {
  220. FIRRemoteConfigValue *value = config[key];
  221. toDict[FIRNamespace][key] = [[FIRRemoteConfigValue alloc] initWithData:value.dataValue
  222. source:source];
  223. NSArray *values = @[ _bundleIdentifier, FIRNamespace, key, value.dataValue ];
  224. [self updateMainTableWithValues:values fromSource:DBSource];
  225. }
  226. }
  227. }
  228. - (void)updateConfigContentWithResponse:(NSDictionary *)response
  229. forNamespace:(NSString *)currentNamespace {
  230. // Make sure database load has completed.
  231. [self checkAndWaitForInitialDatabaseLoad];
  232. NSString *state = response[RCNFetchResponseKeyState];
  233. if (!state) {
  234. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000049", @"State field in fetch response is nil.");
  235. return;
  236. }
  237. FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000059",
  238. @"Updating config content from Response for namespace:%@ with state: %@",
  239. currentNamespace, response[RCNFetchResponseKeyState]);
  240. if ([state isEqualToString:RCNFetchResponseKeyStateNoChange]) {
  241. [self handleNoChangeStateForConfigNamespace:currentNamespace];
  242. return;
  243. }
  244. /// Handle empty config state
  245. if ([state isEqualToString:RCNFetchResponseKeyStateEmptyConfig]) {
  246. [self handleEmptyConfigStateForConfigNamespace:currentNamespace];
  247. return;
  248. }
  249. /// Handle no template state.
  250. if ([state isEqualToString:RCNFetchResponseKeyStateNoTemplate]) {
  251. [self handleNoTemplateStateForConfigNamespace:currentNamespace];
  252. return;
  253. }
  254. /// Handle update state
  255. if ([state isEqualToString:RCNFetchResponseKeyStateUpdate]) {
  256. [self handleUpdateStateForConfigNamespace:currentNamespace
  257. withEntries:response[RCNFetchResponseKeyEntries]];
  258. [self handleUpdatePersonalization:response[RCNFetchResponseKeyPersonalizationMetadata]];
  259. [self handleUpdateRolloutFetchedMetadata:response[RCNFetchResponseKeyRolloutMetadata]];
  260. return;
  261. }
  262. }
  263. - (void)activatePersonalization {
  264. _activePersonalization = _fetchedPersonalization;
  265. [_DBManager insertOrUpdatePersonalizationConfig:_activePersonalization
  266. fromSource:RCNDBSourceActive];
  267. }
  268. - (void)activateRolloutMetadata:(void (^)(BOOL success))completionHandler {
  269. _activeRolloutMetadata = _fetchedRolloutMetadata;
  270. [_DBManager insertOrUpdateRolloutTableWithKey:@RCNRolloutTableKeyActiveMetadata
  271. value:_activeRolloutMetadata
  272. completionHandler:^(BOOL success, NSDictionary *result) {
  273. completionHandler(success);
  274. }];
  275. }
  276. #pragma mark State handling
  277. - (void)handleNoChangeStateForConfigNamespace:(NSString *)currentNamespace {
  278. if (!_fetchedConfig[currentNamespace]) {
  279. _fetchedConfig[currentNamespace] = [[NSMutableDictionary alloc] init];
  280. }
  281. }
  282. - (void)handleEmptyConfigStateForConfigNamespace:(NSString *)currentNamespace {
  283. if (_fetchedConfig[currentNamespace]) {
  284. [_fetchedConfig[currentNamespace] removeAllObjects];
  285. } else {
  286. // If namespace has empty status and it doesn't exist in _fetchedConfig, we will
  287. // still add an entry for that namespace. Even if it will not be persisted in database.
  288. // TODO: Add generics for all collection types.
  289. _fetchedConfig[currentNamespace] = [[NSMutableDictionary alloc] init];
  290. }
  291. [_DBManager deleteRecordFromMainTableWithNamespace:currentNamespace
  292. bundleIdentifier:_bundleIdentifier
  293. fromSource:RCNDBSourceFetched];
  294. }
  295. - (void)handleNoTemplateStateForConfigNamespace:(NSString *)currentNamespace {
  296. // Remove the namespace.
  297. [_fetchedConfig removeObjectForKey:currentNamespace];
  298. [_DBManager deleteRecordFromMainTableWithNamespace:currentNamespace
  299. bundleIdentifier:_bundleIdentifier
  300. fromSource:RCNDBSourceFetched];
  301. }
  302. - (void)handleUpdateStateForConfigNamespace:(NSString *)currentNamespace
  303. withEntries:(NSDictionary *)entries {
  304. FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000058", @"Update config in DB for namespace:%@",
  305. currentNamespace);
  306. // Clear before updating
  307. [_DBManager deleteRecordFromMainTableWithNamespace:currentNamespace
  308. bundleIdentifier:_bundleIdentifier
  309. fromSource:RCNDBSourceFetched];
  310. if ([_fetchedConfig objectForKey:currentNamespace]) {
  311. [_fetchedConfig[currentNamespace] removeAllObjects];
  312. } else {
  313. _fetchedConfig[currentNamespace] = [[NSMutableDictionary alloc] init];
  314. }
  315. // Store the fetched config values.
  316. for (NSString *key in entries) {
  317. NSData *valueData = [entries[key] dataUsingEncoding:NSUTF8StringEncoding];
  318. if (!valueData) {
  319. continue;
  320. }
  321. _fetchedConfig[currentNamespace][key] =
  322. [[FIRRemoteConfigValue alloc] initWithData:valueData source:FIRRemoteConfigSourceRemote];
  323. NSArray *values = @[ _bundleIdentifier, currentNamespace, key, valueData ];
  324. [self updateMainTableWithValues:values fromSource:RCNDBSourceFetched];
  325. }
  326. }
  327. - (void)handleUpdatePersonalization:(NSDictionary *)metadata {
  328. if (!metadata) {
  329. return;
  330. }
  331. _fetchedPersonalization = metadata;
  332. [_DBManager insertOrUpdatePersonalizationConfig:metadata fromSource:RCNDBSourceFetched];
  333. }
  334. - (void)handleUpdateRolloutFetchedMetadata:(NSArray<NSDictionary *> *)metadata {
  335. if (!metadata) {
  336. metadata = [[NSArray alloc] init];
  337. }
  338. _fetchedRolloutMetadata = metadata;
  339. [_DBManager insertOrUpdateRolloutTableWithKey:@RCNRolloutTableKeyFetchedMetadata
  340. value:metadata
  341. completionHandler:nil];
  342. }
  343. #pragma mark - getter/setter
  344. - (NSDictionary *)fetchedConfig {
  345. /// If this is the first time reading the fetchedConfig, we might still be reading it from the
  346. /// database.
  347. [self checkAndWaitForInitialDatabaseLoad];
  348. return _fetchedConfig;
  349. }
  350. - (NSDictionary *)activeConfig {
  351. /// If this is the first time reading the activeConfig, we might still be reading it from the
  352. /// database.
  353. [self checkAndWaitForInitialDatabaseLoad];
  354. return _activeConfig;
  355. }
  356. - (NSDictionary *)defaultConfig {
  357. /// If this is the first time reading the fetchedConfig, we might still be reading it from the
  358. /// database.
  359. [self checkAndWaitForInitialDatabaseLoad];
  360. return _defaultConfig;
  361. }
  362. - (NSDictionary *)activePersonalization {
  363. [self checkAndWaitForInitialDatabaseLoad];
  364. return _activePersonalization;
  365. }
  366. - (NSArray<NSDictionary *> *)activeRolloutMetadata {
  367. [self checkAndWaitForInitialDatabaseLoad];
  368. return _activeRolloutMetadata;
  369. }
  370. - (NSDictionary *)getConfigAndMetadataForNamespace:(NSString *)FIRNamespace {
  371. /// If this is the first time reading the active metadata, we might still be reading it from the
  372. /// database.
  373. [self checkAndWaitForInitialDatabaseLoad];
  374. return @{
  375. RCNFetchResponseKeyEntries : _activeConfig[FIRNamespace],
  376. RCNFetchResponseKeyPersonalizationMetadata : _activePersonalization
  377. };
  378. }
  379. /// We load the database async at init time. Block all further calls to active/fetched/default
  380. /// configs until load is done.
  381. /// @return Database load completion status.
  382. - (BOOL)checkAndWaitForInitialDatabaseLoad {
  383. /// Wait until load is done. This should be a no-op for subsequent calls.
  384. if (!_isConfigLoadFromDBCompleted) {
  385. intptr_t isErrorOrTimeout = dispatch_group_wait(
  386. _dispatch_group,
  387. dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kDatabaseLoadTimeoutSecs * NSEC_PER_SEC)));
  388. if (isErrorOrTimeout) {
  389. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000048",
  390. @"Timed out waiting for fetched config to be loaded from DB");
  391. return false;
  392. }
  393. _isConfigLoadFromDBCompleted = true;
  394. }
  395. return true;
  396. }
  397. // Compare fetched config with active config and output what has changed
  398. - (FIRRemoteConfigUpdate *)getConfigUpdateForNamespace:(NSString *)FIRNamespace {
  399. // TODO: handle diff in experiment metadata
  400. FIRRemoteConfigUpdate *configUpdate;
  401. NSMutableSet<NSString *> *updatedKeys = [[NSMutableSet alloc] init];
  402. NSDictionary *fetchedConfig =
  403. _fetchedConfig[FIRNamespace] ? _fetchedConfig[FIRNamespace] : [[NSDictionary alloc] init];
  404. NSDictionary *activeConfig =
  405. _activeConfig[FIRNamespace] ? _activeConfig[FIRNamespace] : [[NSDictionary alloc] init];
  406. NSDictionary *fetchedP13n = _fetchedPersonalization;
  407. NSDictionary *activeP13n = _activePersonalization;
  408. NSArray<NSDictionary *> *fetchedRolloutMetadata = _fetchedRolloutMetadata;
  409. NSArray<NSDictionary *> *activeRolloutMetadata = _activeRolloutMetadata;
  410. // add new/updated params
  411. for (NSString *key in [fetchedConfig allKeys]) {
  412. if (activeConfig[key] == nil ||
  413. ![[activeConfig[key] stringValue] isEqualToString:[fetchedConfig[key] stringValue]]) {
  414. [updatedKeys addObject:key];
  415. }
  416. }
  417. // add deleted params
  418. for (NSString *key in [activeConfig allKeys]) {
  419. if (fetchedConfig[key] == nil) {
  420. [updatedKeys addObject:key];
  421. }
  422. }
  423. // add params with new/updated p13n metadata
  424. for (NSString *key in [fetchedP13n allKeys]) {
  425. if (activeP13n[key] == nil || ![activeP13n[key] isEqualToDictionary:fetchedP13n[key]]) {
  426. [updatedKeys addObject:key];
  427. }
  428. }
  429. // add params with deleted p13n metadata
  430. for (NSString *key in [activeP13n allKeys]) {
  431. if (fetchedP13n[key] == nil) {
  432. [updatedKeys addObject:key];
  433. }
  434. }
  435. NSDictionary<NSString *, NSDictionary *> *fetchedRollouts =
  436. [self getParameterKeyToRolloutMetadata:fetchedRolloutMetadata];
  437. NSDictionary<NSString *, NSDictionary *> *activeRollouts =
  438. [self getParameterKeyToRolloutMetadata:activeRolloutMetadata];
  439. // add params with new/updated rollout metadata
  440. for (NSString *key in [fetchedRollouts allKeys]) {
  441. if (activeRollouts[key] == nil ||
  442. ![activeRollouts[key] isEqualToDictionary:fetchedRollouts[key]]) {
  443. [updatedKeys addObject:key];
  444. }
  445. }
  446. // add params with deleted rollout metadata
  447. for (NSString *key in [activeRollouts allKeys]) {
  448. if (fetchedRollouts[key] == nil) {
  449. [updatedKeys addObject:key];
  450. }
  451. }
  452. configUpdate = [[FIRRemoteConfigUpdate alloc] initWithUpdatedKeys:updatedKeys];
  453. return configUpdate;
  454. }
  455. - (NSDictionary<NSString *, NSDictionary *> *)getParameterKeyToRolloutMetadata:
  456. (NSArray<NSDictionary *> *)rolloutMetadata {
  457. NSMutableDictionary<NSString *, NSMutableDictionary *> *result =
  458. [[NSMutableDictionary alloc] init];
  459. for (NSDictionary *metadata in rolloutMetadata) {
  460. NSString *rolloutId = metadata[RCNFetchResponseKeyRolloutID];
  461. NSString *variantId = metadata[RCNFetchResponseKeyVariantID];
  462. NSArray<NSString *> *affectedKeys = metadata[RCNFetchResponseKeyAffectedParameterKeys];
  463. if (rolloutId && variantId && affectedKeys) {
  464. for (NSString *key in affectedKeys) {
  465. if (result[key]) {
  466. NSMutableDictionary *rolloutIdToVariantId = result[key];
  467. [rolloutIdToVariantId setValue:variantId forKey:rolloutId];
  468. } else {
  469. NSMutableDictionary *rolloutIdToVariantId = [@{rolloutId : variantId} mutableCopy];
  470. [result setValue:rolloutIdToVariantId forKey:key];
  471. }
  472. }
  473. }
  474. }
  475. return [result copy];
  476. }
  477. @end