@@ -24,6 +24,7 @@ typedef NS_ENUM(NSUInteger, OPTLYDataStoreDataType)
24
24
OPTLYDataStoreDataTypeUserProfile,
25
25
};
26
26
27
+ // if adding event type, update: removeAllUserData!!
27
28
typedef NS_ENUM (NSUInteger , OPTLYDataStoreEventType)
28
29
{
29
30
OPTLYDataStoreEventTypeImpression,
@@ -33,8 +34,11 @@ typedef NS_ENUM(NSUInteger, OPTLYDataStoreEventType)
33
34
@class OPTLYFileManager;
34
35
35
36
/*
36
- * This class handles all things related to persistence and serves as a wrapper class for
37
- * OPTLYFileManager and OPTLYDatabase.
37
+ * This class handles all Optimizely-related data persistence and caching.
38
+ * Data is persisted for the following purposes:
39
+ * - NSFileManager for datafile
40
+ * - SQLite table (or in-memory queue) for events
41
+ * - NSUserDefault for user data (e.g., bucketing info).
38
42
*/
39
43
40
44
@interface OPTLYDataStore : NSObject
@@ -43,11 +47,16 @@ typedef NS_ENUM(NSUInteger, OPTLYDataStoreEventType)
43
47
@property (nonatomic , strong , readonly , nonnull ) NSString *baseDirectory;
44
48
45
49
/* *
46
- * Removes all data persisted by Optimizely
50
+ * Wipes all Optimizely data (saved and cached).
51
+ *
47
52
**/
48
53
- (void )removeAll ;
49
54
50
- // ---- NSFileManager ----
55
+ // -------- File Storage --------
56
+ // Persists data in a file format using NSFileManager.
57
+ // iOS documents are written in the Library directory.
58
+ // tvOS documents are written in the Temporary directory.
59
+
51
60
/* *
52
61
* Saves a file.
53
62
* If a file of the same name type exists already, then that file will be overwritten.
@@ -131,134 +140,159 @@ typedef NS_ENUM(NSUInteger, OPTLYDataStoreEventType)
131
140
error : (NSError * _Nullable * _Nullable)error ;
132
141
133
142
134
- // ---- SQLite Table ----
135
- // Used for event storage
136
- #if TARGET_OS_IOS
143
+ // -------- Events Storage --------
144
+ // If data is cached, then the events are stored in a runtime in-memory queue.
145
+ // If data is not cached, the events are stored in an SQLite table.
146
+ // Events are only persisted in the SQLite table for iOS platforms.
147
+ // tvOS events are only cached to save on very limited memory usage.
148
+ // A future enhancement for tvOS is to store data in iCloud.
149
+
137
150
/* *
138
- * Creates a new table .
151
+ * Saves an event .
139
152
*
140
- * @param eventType The event type of the data to be stored in the new table.
153
+ * @param data The data to be saved.
154
+ * @param eventType The event type of the data that needs to be saved.
155
+ * @param cacheData Specify if the data should be cached (default is true for tvOS).
141
156
* @param error An error object is returned if an error occurs.
142
- **/
143
- - (void )createTable : (OPTLYDataStoreEventType)eventType
144
- error : (NSError * _Nullable * _Nullable)error ;
157
+ */
158
+ - (void )saveData : (nonnull NSDictionary *)data
159
+ eventType : (OPTLYDataStoreEventType)eventType
160
+ cacheData : (bool )cacheData
161
+ error : (NSError * _Nullable * _Nullable)error ;
145
162
146
163
/* *
147
- * Inserts data into a database table .
164
+ * Gets the oldest event .
148
165
*
149
- * @param data The data to be written into the table .
150
- * @param eventType The event type of the data that needs to be saved .
166
+ * @param eventType The event type of the data that needs to be retrieved .
167
+ * @param cacheData Specify if the data should be retrieved from cache (this is by default always true for tvOS) .
151
168
* @param error An error object is returned if an error occurs.
152
169
*/
153
- - (void ) insertData : (nonnull NSDictionary *)data
154
- eventType : (OPTLYDataStoreEventType) eventType
155
- error : (NSError * _Nullable * _Nullable)error ;
170
+ - (nullable NSDictionary *)getOldestEvent : (OPTLYDataStoreEventType) eventType
171
+ cacheData : ( bool ) cacheData
172
+ error : (NSError * _Nullable * _Nullable)error ;
156
173
157
174
/* *
158
- * Deletes data from a database table .
175
+ * Gets the first N entries (i.e., the N oldest events) .
159
176
*
160
- * @param entityId The entity id to remove from the table .
177
+ * @param numberOfEvents The number of events to retrieve .
161
178
* @param eventType The event type of the data that needs to be removed.
179
+ * @param cacheData Specify if the data should be retrieved from cache (this is by default always true for tvOS).
162
180
* @param error An error object is returned if an error occurs.
163
181
*/
164
- - (void )deleteEvent : (nonnull NSString *)entityId
165
- eventType : (OPTLYDataStoreEventType)eventType
166
- error : (NSError * _Nullable * _Nullable)error ;
182
+ - (nullable NSArray *)getFirstNEvents : (NSInteger )numberOfEvents
183
+ eventType : (OPTLYDataStoreEventType)eventType
184
+ cacheData : (bool )cacheData
185
+ error : (NSError * _Nullable * _Nullable)error ;
167
186
168
187
/* *
169
- * Deletes data from a database table.
188
+ * Gets all events.
189
+ *
190
+ * @param eventType The event type of the data that needs to be retrieved.
191
+ * @param cacheData Specify if the data should be retrieved from cache (this is by default always true for tvOS).
192
+ * @param error An error object is returned if an error occurs.
193
+ * @return The return value is an array of OPTLYDatabaseEntity.
194
+ */
195
+ - (nullable NSArray *)getAllEvents : (OPTLYDataStoreEventType)eventType
196
+ cacheData : (bool )cacheData
197
+ error : (NSError * _Nullable * _Nullable)error ;
198
+
199
+ /* *
200
+ * Deletes the oldest event.
170
201
*
171
- * @param entityIds The entity ids to remove from the table.
172
202
* @param eventType The event type of the data that needs to be removed.
203
+ * @param cacheData Specify if the data should be removed from cache (this is by default always true for tvOS).
173
204
* @param error An error object is returned if an error occurs.
174
205
*/
175
- - (void )deleteEvents : (nonnull NSArray *) entityIds
176
- eventType : (OPTLYDataStoreEventType) eventType
177
- error : (NSError * _Nullable * _Nullable)error ;
206
+ - (void )removeOldestEvent : (OPTLYDataStoreEventType) eventType
207
+ cacheData : ( bool ) cacheData
208
+ error : (NSError * _Nullable * _Nullable)error ;
178
209
179
210
/* *
180
- * Retrieve all entries from the table .
211
+ * Deletes the first N events (i.e., the N oldest events) .
181
212
*
182
- * @param eventType The event type of the data that needs to be retrieved.
213
+ * @param numberOfEvents The number of events to retrieve.
214
+ * @param eventType The event type of the data that needs to be removed.
215
+ * @param cacheData Specify if the data should be removed from cache (this is by default always true for tvOS).
183
216
* @param error An error object is returned if an error occurs.
184
- * @return The return value is an array of OPTLYDatabaseEntity.
185
217
*/
186
- - (nullable NSArray *)retrieveAllEvents : (OPTLYDataStoreEventType)eventType
187
- error : (NSError * _Nullable * _Nullable)error ;
218
+ - (void )removeFirstNEvents : (NSInteger )numberOfEvents
219
+ eventType : (OPTLYDataStoreEventType)eventType
220
+ cacheData : (bool )cacheData
221
+ error : (NSError * _Nullable * _Nullable)error ;
188
222
189
223
/* *
190
- * Retrieves a set of N entries from the table .
224
+ * Deletes all events .
191
225
*
192
- * @param numberOfEntries The number of entries to read from the table
193
- * @param eventType The event type of the data that needs to be retrieved .
226
+ * @param eventType The event type of the data that needs to be removed.
227
+ * @param cacheData Specify if the data should be removed from cache (this is by default always true for tvOS) .
194
228
* @param error An error object is returned if an error occurs.
195
- * @return The return value is an array of OPTLYDatabaseEntity.
196
229
*/
197
- - (nullable NSArray *) retrieveFirstNEvents : ( NSInteger ) numberOfEntries
198
- eventType : (OPTLYDataStoreEventType) eventType
199
- error : (NSError * _Nullable * _Nullable)error ;
230
+ - (void ) removeAllEvents : (OPTLYDataStoreEventType) eventType
231
+ cacheData : ( bool ) cacheData
232
+ error : (NSError * _Nullable * _Nullable)error ;
200
233
201
234
/* *
202
235
* Returns the number of saved events.
203
236
*
204
237
* @param eventType The event type of the data.
238
+ * @param cacheData Specify if the event count should be retrieved from cache (this is by default always true for tvOS).
205
239
* @param error An error object is returned if an error occurs.
206
- * @return The number of rows in a table .
240
+ * @return The number of events saved .
207
241
*/
208
242
- (NSInteger )numberOfEvents : (OPTLYDataStoreEventType)eventType
243
+ cacheData : (bool )cacheData
209
244
error : (NSError * _Nullable * _Nullable)error ;
210
- #endif
211
245
212
- // ---- NSUserDefault ----
246
+ /* *
247
+ * Removes all cached events.
248
+ */
249
+ - (void )removeCachedEvents ;
250
+
251
+ /* *
252
+ * Removes all saved events.
253
+ *
254
+ * @param error An error object is returned if an error occurs.
255
+ */
256
+ - (void )removeSavedEvents : (NSError * _Nullable * _Nullable)error ;
257
+
258
+ // -------- User Data Storage --------
259
+ // Saves data in dictionary format in NSUserDefault
260
+ // This is available for iOS and tvOS.
261
+
213
262
/* *
214
263
* Saves data in dictionary format in NSUserDefault
215
264
*
216
265
* @param data The dictionary data to save.
217
266
* @param dataType The type of data (e.g., datafile, user profile, event dispatcher, etc.)
218
267
*/
219
- - (void )save : (nonnull NSDictionary *)data type : (OPTLYDataStoreDataType)dataType ;
268
+ - (void )saveUserData : (nonnull NSDictionary *)data type : (OPTLYDataStoreDataType)dataType ;
220
269
221
270
/* *
222
271
* Gets saved data from NSUserDefault.
223
272
*
224
273
* @param dataType The type of data (e.g., datafile, user profile, event dispatcher, etc.)
225
274
* @return data retrieved.
226
275
*/
227
- - (nullable NSDictionary *)getDataForType : (OPTLYDataStoreDataType)dataType ;
276
+ - (nullable NSDictionary *)getUserDataForType : (OPTLYDataStoreDataType)dataType ;
228
277
229
278
/* *
230
279
* Removes data for a particular type of data in NSUserDefault.
231
280
*
232
281
* @param dataType The type of data (e.g., datafile, user profile, event dispatcher, etc.)
233
282
*/
234
- - (void )removeDataForType : (OPTLYDataStoreDataType)dataType ;
283
+ - (void )removeUserDataForType : (OPTLYDataStoreDataType)dataType ;
235
284
236
285
/* *
237
286
* Removes all Optimizely-related data in NSUserDefault.
238
287
*/
239
- - (void )removeAllData ;
288
+ - (void )removeAllUserData ;
240
289
241
290
/* *
242
291
* Removes an object from the dictionary data saved in NSUserDefault.
243
292
*
244
293
* @param dataKey The key for the dictionary data to remove.
245
294
* @param dataType The type of data (e.g., datafile, user profile, event dispatcher, etc.)
246
295
*/
247
- - (void )removeObjectInData : (nonnull id )dataKey type : (OPTLYDataStoreDataType)dataType ;
248
-
249
- // ---- Cached Data ----
250
- - (void )insertCachedData : (nonnull NSDictionary *)data
251
- eventType : (OPTLYDataStoreEventType)eventType ;
252
-
253
- - (nullable NSDictionary *)retrieveCachedItem : (OPTLYDataStoreEventType)eventType ;
254
-
255
- - (nullable NSArray *)retrieveNCachedItems : (NSInteger )numberOfItems
256
- eventType : (OPTLYDataStoreEventType)eventType ;
257
-
258
- - (void )removeCachedItem : (OPTLYDataStoreEventType)eventType ;
259
-
260
- - (void )removeNCachedItem : (NSInteger )numberOfItems
261
- eventType : (OPTLYDataStoreEventType)eventType ;
296
+ - (void )removeObjectInUserData : (nonnull id )dataKey type : (OPTLYDataStoreDataType)dataType ;
262
297
263
- - (NSInteger )numberOfCachedItems : (OPTLYDataStoreEventType)eventType ;
264
298
@end
0 commit comments