RCNConfigDBManager.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 <Foundation/Foundation.h>
  17. #import "FirebaseRemoteConfig/Sources/RCNConfigContent.h"
  18. typedef NS_ENUM(NSInteger, RCNUpdateOption) {
  19. RCNUpdateOptionApplyTime,
  20. RCNUpdateOptionDefaultTime,
  21. RCNUpdateOptionFetchStatus,
  22. };
  23. /// Column names in metadata table
  24. static NSString *const RCNKeyBundleIdentifier = @"bundle_identifier";
  25. static NSString *const RCNKeyNamespace = @"namespace";
  26. static NSString *const RCNKeyFetchTime = @"fetch_time";
  27. static NSString *const RCNKeyDigestPerNamespace = @"digest_per_ns";
  28. static NSString *const RCNKeyDeviceContext = @"device_context";
  29. static NSString *const RCNKeyAppContext = @"app_context";
  30. static NSString *const RCNKeySuccessFetchTime = @"success_fetch_time";
  31. static NSString *const RCNKeyFailureFetchTime = @"failure_fetch_time";
  32. static NSString *const RCNKeyLastFetchStatus = @"last_fetch_status";
  33. static NSString *const RCNKeyLastFetchError = @"last_fetch_error";
  34. static NSString *const RCNKeyLastApplyTime = @"last_apply_time";
  35. static NSString *const RCNKeyLastSetDefaultsTime = @"last_set_defaults_time";
  36. /// Persist config data in sqlite database on device. Managing data read/write from/to database.
  37. @interface RCNConfigDBManager : NSObject
  38. /// Shared Singleton Instance
  39. + (instancetype)sharedInstance;
  40. /// Database Operation Completion callback.
  41. /// @param success Decide whether the DB operation succeeds.
  42. /// @param result Return operation result data.
  43. typedef void (^RCNDBCompletion)(BOOL success, NSDictionary *result);
  44. /// Database Load Operation Completion callback.
  45. /// @param success Decide whether the DB operation succeeds.
  46. /// @param fetchedConfig Return fetchedConfig loaded from DB
  47. /// @param activeConfig Return activeConfig loaded from DB
  48. /// @param defaultConfig Return defaultConfig loaded from DB
  49. /// @param rolloutMetadata Return fetched and active RolloutMetadata loaded from DB
  50. typedef void (^RCNDBLoadCompletion)(BOOL success,
  51. NSDictionary *fetchedConfig,
  52. NSDictionary *activeConfig,
  53. NSDictionary *defaultConfig,
  54. NSDictionary *rolloutMetadata);
  55. /// Returns the current version of the Remote Config database.
  56. + (NSString *)remoteConfigPathForDatabase;
  57. /// Load config content from main table to cached memory during app start.
  58. - (void)loadMainWithBundleIdentifier:(NSString *)bundleIdentifier
  59. completionHandler:(RCNDBLoadCompletion)handler;
  60. /// Load config settings for a given namespace from metadata table to cached memory during app
  61. /// start. Config settings include success/failure fetch times, device contenxt, app context, etc.
  62. - (NSDictionary *)loadMetadataWithBundleIdentifier:(NSString *)bundleIdentifier
  63. namespace:(NSString *)namespace;
  64. /// Load experiment from experiment table.
  65. /// @param handler The callback when reading from DB is complete.
  66. - (void)loadExperimentWithCompletionHandler:(RCNDBCompletion)handler;
  67. /// Load Personalization from table.
  68. /// @param handler The callback when reading from DB is complete.
  69. - (void)loadPersonalizationWithCompletionHandler:(RCNDBLoadCompletion)handler;
  70. /// Insert a record in metadata table.
  71. /// @param columnNameToValue The column name and its value to be inserted in metadata table.
  72. /// @param handler The callback.
  73. - (void)insertMetadataTableWithValues:(NSDictionary *)columnNameToValue
  74. completionHandler:(RCNDBCompletion)handler;
  75. /// Insert a record in main table.
  76. /// @param values Values to be inserted.
  77. - (void)insertMainTableWithValues:(NSArray *)values
  78. fromSource:(RCNDBSource)source
  79. completionHandler:(RCNDBCompletion)handler;
  80. /// Insert experiment data in experiment table.
  81. /// @param key The key of experiment data belongs to, which are defined in
  82. /// RCNConfigDefines.h.
  83. /// @param value The value that experiment.
  84. /// @param handler The callback.
  85. - (void)insertExperimentTableWithKey:(NSString *)key
  86. value:(NSData *)value
  87. completionHandler:(RCNDBCompletion)handler;
  88. - (void)updateMetadataWithOption:(RCNUpdateOption)option
  89. namespace:(NSString *)namespace
  90. values:(NSArray *)values
  91. completionHandler:(RCNDBCompletion)handler;
  92. /// Insert or update the data in Personalization config.
  93. - (BOOL)insertOrUpdatePersonalizationConfig:(NSDictionary *)metadata fromSource:(RCNDBSource)source;
  94. /// Insert rollout metadata in rollout table.
  95. /// @param key Key indicating whether rollout metadata is fetched or active and defined in
  96. /// RCNConfigDefines.h.
  97. /// @param metadataList The metadata info for each rollout entry .
  98. /// @param handler The callback.
  99. - (void)insertOrUpdateRolloutTableWithKey:(NSString *)key
  100. value:(NSArray<NSDictionary *> *)metadataList
  101. completionHandler:(RCNDBCompletion)handler;
  102. /// Clear the record of given namespace and package name
  103. /// before updating the table.
  104. - (void)deleteRecordFromMainTableWithNamespace:(NSString *)namespace_p
  105. bundleIdentifier:(NSString *)bundleIdentifier
  106. fromSource:(RCNDBSource)source;
  107. /// Remove all the records of given package name and namespace from metadata DB
  108. /// before updating new values from response.
  109. - (void)deleteRecordWithBundleIdentifier:(NSString *)bundlerIdentifier
  110. namespace:(NSString *)namespace;
  111. /// Remove all the records from a config content table.
  112. - (void)deleteAllRecordsFromTableWithSource:(RCNDBSource)source;
  113. /// Remove all the records from experiment table with given key.
  114. /// @param key The key of experiment data belongs to, which are defined in RCNConfigDefines.h.
  115. - (void)deleteExperimentTableForKey:(NSString *)key;
  116. /// Returns true if this a new install of the Config database.
  117. - (BOOL)isNewDatabase;
  118. @end