RCNConfigDBManager.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 RCNKeyFetchTime = @"fetch_time";
  26. static NSString *const RCNKeyDigestPerNamespace = @"digest_per_ns";
  27. static NSString *const RCNKeyDeviceContext = @"device_context";
  28. static NSString *const RCNKeyAppContext = @"app_context";
  29. static NSString *const RCNKeySuccessFetchTime = @"success_fetch_time";
  30. static NSString *const RCNKeyFailureFetchTime = @"failure_fetch_time";
  31. static NSString *const RCNKeyLastFetchStatus = @"last_fetch_status";
  32. static NSString *const RCNKeyLastFetchError = @"last_fetch_error";
  33. static NSString *const RCNKeyLastApplyTime = @"last_apply_time";
  34. static NSString *const RCNKeyLastSetDefaultsTime = @"last_set_defaults_time";
  35. /// Persist config data in sqlite database on device. Managing data read/write from/to database.
  36. @interface RCNConfigDBManager : NSObject
  37. /// Shared Singleton Instance
  38. + (instancetype)sharedInstance;
  39. /// Database Operation Completion callback.
  40. /// @param success Decide whether the DB operation succeeds.
  41. /// @param result Return operation result data.
  42. typedef void (^RCNDBCompletion)(BOOL success, NSDictionary *result);
  43. /// Database Load Operation Completion callback.
  44. /// @param success Decide whether the DB operation succeeds.
  45. /// @param fetchedConfig Return fetchedConfig loaded from DB
  46. /// @param activeConfig Return activeConfig loaded from DB
  47. /// @param defaultConfig Return defaultConfig loaded from DB
  48. typedef void (^RCNDBLoadCompletion)(BOOL success,
  49. NSDictionary *fetchedConfig,
  50. NSDictionary *activeConfig,
  51. NSDictionary *defaultConfig);
  52. /// Returns the current version of the Remote Config database.
  53. + (NSString *)remoteConfigPathForDatabase;
  54. /// Load config content from main table to cached memory during app start.
  55. - (void)loadMainWithBundleIdentifier:(NSString *)bundleIdentifier
  56. completionHandler:(RCNDBLoadCompletion)handler;
  57. /// Load config settings from metadata table to cached memory during app start. Config settings
  58. /// include success/failure fetch times, device contenxt, app context, etc.
  59. - (NSDictionary *)loadMetadataWithBundleIdentifier:(NSString *)bundleIdentifier;
  60. /// Load internal metadata from internal metadata table, such as customized HTTP connection/read
  61. /// timeout, throttling time interval and number limit of throttling, etc.
  62. /// This call needs to be blocking to ensure throttling works during apps starts.
  63. - (NSDictionary *)loadInternalMetadataTable;
  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 a record in internal metadata table.
  81. /// @param values Values to be inserted.
  82. - (void)insertInternalMetadataTableWithValues:(NSArray *)values
  83. completionHandler:(RCNDBCompletion)handler;
  84. /// Insert exepriment data in experiment table.
  85. /// @param key The key of experiment data belongs to, which are defined in
  86. /// RCNConfigDefines.h.
  87. /// @param value The value that experiment.
  88. /// @param handler The callback.
  89. - (void)insertExperimentTableWithKey:(NSString *)key
  90. value:(NSData *)value
  91. completionHandler:(RCNDBCompletion)handler;
  92. - (void)updateMetadataWithOption:(RCNUpdateOption)option
  93. values:(NSArray *)values
  94. completionHandler:(RCNDBCompletion)handler;
  95. /// Insert or update the data in Personalization config.
  96. - (BOOL)insertOrUpdatePersonalizationConfig:(NSDictionary *)metadata fromSource:(RCNDBSource)source;
  97. /// Clear the record of given namespace and package name
  98. /// before updating the table.
  99. - (void)deleteRecordFromMainTableWithNamespace:(NSString *)namespace_p
  100. bundleIdentifier:(NSString *)bundleIdentifier
  101. fromSource:(RCNDBSource)source;
  102. /// Remove all the records of given package name from metadata/internal metadata DB before updating
  103. /// new values from response.
  104. - (void)deleteRecordWithBundleIdentifier:(NSString *)bundlerIdentifier
  105. isInternalDB:(BOOL)isInternalDB;
  106. /// Remove all the records from a config content table.
  107. - (void)deleteAllRecordsFromTableWithSource:(RCNDBSource)source;
  108. /// Remove all the records from experiment table with given key.
  109. /// @param key The key of experiment data belongs to, which are defined in RCNConfigDefines.h.
  110. - (void)deleteExperimentTableForKey:(NSString *)key;
  111. /// Returns true if this a new install of the Config database.
  112. - (BOOL)isNewDatabase;
  113. @end