Skip to content

Commit 3945ad6

Browse files
Consolidated methods for persisting events so that the cached and uncached events are handled by a parameterized method.
1 parent d0691f4 commit 3945ad6

File tree

9 files changed

+497
-242
lines changed

9 files changed

+497
-242
lines changed

OptimizelySDKCore/OptimizelySDKCore/OPTLYErrorHandlerMessages.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef NS_ENUM(NSUInteger, OPTLYErrorTypes) {
5555
OPTLYErrorTypesEventDispatchInvalid,
5656
OPTLYErrorTypesBuilderInvalid,
5757
OPTLYErrorTypesDatabase,
58+
OPTLYErrorTypesDataStore,
5859
};
5960

6061
@interface OPTLYErrorHandlerMessages : NSObject

OptimizelySDKCore/OptimizelySDKCore/OptimizelySDKCore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#import "OPTLYProjectConfig.h"
2828
#import "OPTLYVariation.h"
2929
#import "OPTLYLog.h"
30+
#import "OPTLYQueue.h"
3031

3132
FOUNDATION_EXPORT double OptimizelySDKCoreVersionNumber;
3233
FOUNDATION_EXPORT const unsigned char OptimizelySDKCoreVersionString[];

OptimizelySDKShared/OptimizelySDKShared.xcodeproj/project.pbxproj

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
2DB3D1801DC8181100ECF72E /* OPTLYManagerBuilder.h in Headers */ = {isa = PBXBuildFile; fileRef = 2DB3D17A1DC816D600ECF72E /* OPTLYManagerBuilder.h */; settings = {ATTRIBUTES = (Public, ); }; };
3939
2DB3D1811DC8181400ECF72E /* OPTLYManagerBuilder.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DB3D17B1DC816D600ECF72E /* OPTLYManagerBuilder.m */; };
4040
2DCC50B31DD3CDAB006C9815 /* OPTLYManagerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DCC50B21DD3CDAB006C9815 /* OPTLYManagerTest.m */; };
41-
2DCC50B41DD3CDAB006C9815 /* OPTLYManagerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DCC50B21DD3CDAB006C9815 /* OPTLYManagerTest.m */; };
42-
2DCC50BD1DD535A5006C9815 /* OPTLYFileManager.h in Headers */ = {isa = PBXBuildFile; fileRef = EA29D8FC1DCBBE250034A4FE /* OPTLYFileManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
4341
5318433267A9188A3D16D3A2 /* Pods_OptimizelySDKSharediOSTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7B9FE2FE02E36C64D6F1F442 /* Pods_OptimizelySDKSharediOSTests.framework */; };
4442
5A8753FCE65715A7A353C14C /* Pods_OptimizelySDKSharediOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 09704701D3CB97F3D3F408E3 /* Pods_OptimizelySDKSharediOS.framework */; };
4543
6433BEBF70E8BE35397D1778 /* Pods_OptimizelySDKSharedTVOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5EDD2F1182FB354ABC0E04A1 /* Pods_OptimizelySDKSharedTVOS.framework */; };
@@ -55,8 +53,8 @@
5553
EA1E16961DC87E0000F00033 /* OPTLYTestHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = EA1E16901DC87DC700F00033 /* OPTLYTestHelper.m */; };
5654
EA1E16971DC87E0100F00033 /* OPTLYTestHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = EA1E16901DC87DC700F00033 /* OPTLYTestHelper.m */; };
5755
EA29D8FF1DCBBE250034A4FE /* OPTLYFileManager.m in Sources */ = {isa = PBXBuildFile; fileRef = EA29D8FD1DCBBE250034A4FE /* OPTLYFileManager.m */; };
58-
EA29D94E1DCE778C0034A4FE /* OPTLYDatabase.h in Headers */ = {isa = PBXBuildFile; fileRef = EA29D94C1DCE778C0034A4FE /* OPTLYDatabase.h */; };
59-
EA29D9541DCED4580034A4FE /* OPTLYDatabaseEntity.h in Headers */ = {isa = PBXBuildFile; fileRef = EA29D9521DCED4580034A4FE /* OPTLYDatabaseEntity.h */; };
56+
EA29D94E1DCE778C0034A4FE /* OPTLYDatabase.h in Headers */ = {isa = PBXBuildFile; fileRef = EA29D94C1DCE778C0034A4FE /* OPTLYDatabase.h */; settings = {ATTRIBUTES = (Public, ); }; };
57+
EA29D9541DCED4580034A4FE /* OPTLYDatabaseEntity.h in Headers */ = {isa = PBXBuildFile; fileRef = EA29D9521DCED4580034A4FE /* OPTLYDatabaseEntity.h */; settings = {ATTRIBUTES = (Public, ); }; };
6058
EA29D9551DCED4580034A4FE /* OPTLYDatabaseEntity.m in Sources */ = {isa = PBXBuildFile; fileRef = EA29D9531DCED4580034A4FE /* OPTLYDatabaseEntity.m */; };
6159
EA29D9E31DD274870034A4FE /* OPTLYFileManager.m in Sources */ = {isa = PBXBuildFile; fileRef = EA29D8FD1DCBBE250034A4FE /* OPTLYFileManager.m */; };
6260
EA29D9E81DD274A30034A4FE /* OPTLYFileManager.h in Headers */ = {isa = PBXBuildFile; fileRef = EA29D8FC1DCBBE250034A4FE /* OPTLYFileManager.h */; };
@@ -428,9 +426,9 @@
428426
EA52479A1DC724AF00AF6685 /* OPTLYNetworkService.h in Headers */,
429427
EA064BD21DD4030700DF7537 /* OPTLYDataStore.h in Headers */,
430428
2DB3D17C1DC816D600ECF72E /* OPTLYManagerBuilder.h in Headers */,
431-
EA064BDC1DD4186400DF7537 /* OPTLYFileManager.h in Headers */,
432429
EA29D94E1DCE778C0034A4FE /* OPTLYDatabase.h in Headers */,
433430
EA29D9541DCED4580034A4FE /* OPTLYDatabaseEntity.h in Headers */,
431+
EA064BDC1DD4186400DF7537 /* OPTLYFileManager.h in Headers */,
434432
);
435433
runOnlyForDeploymentPostprocessing = 0;
436434
};

OptimizelySDKShared/OptimizelySDKShared/OPTLYDataStore.h

Lines changed: 97 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ typedef NS_ENUM(NSUInteger, OPTLYDataStoreDataType)
2424
OPTLYDataStoreDataTypeUserProfile,
2525
};
2626

27+
// if adding event type, update: removeAllUserData!!
2728
typedef NS_ENUM(NSUInteger, OPTLYDataStoreEventType)
2829
{
2930
OPTLYDataStoreEventTypeImpression,
@@ -33,8 +34,11 @@ typedef NS_ENUM(NSUInteger, OPTLYDataStoreEventType)
3334
@class OPTLYFileManager;
3435

3536
/*
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).
3842
*/
3943

4044
@interface OPTLYDataStore : NSObject
@@ -43,11 +47,16 @@ typedef NS_ENUM(NSUInteger, OPTLYDataStoreEventType)
4347
@property (nonatomic, strong, readonly, nonnull) NSString *baseDirectory;
4448

4549
/**
46-
* Removes all data persisted by Optimizely
50+
* Wipes all Optimizely data (saved and cached).
51+
*
4752
**/
4853
- (void)removeAll;
4954

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+
5160
/**
5261
* Saves a file.
5362
* 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)
131140
error:(NSError * _Nullable * _Nullable)error;
132141

133142

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+
137150
/**
138-
* Creates a new table.
151+
* Saves an event.
139152
*
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).
141156
* @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;
145162

146163
/**
147-
* Inserts data into a database table.
164+
* Gets the oldest event.
148165
*
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).
151168
* @param error An error object is returned if an error occurs.
152169
*/
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;
156173

157174
/**
158-
* Deletes data from a database table.
175+
* Gets the first N entries (i.e., the N oldest events).
159176
*
160-
* @param entityId The entity id to remove from the table.
177+
* @param numberOfEvents The number of events to retrieve.
161178
* @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).
162180
* @param error An error object is returned if an error occurs.
163181
*/
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;
167186

168187
/**
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.
170201
*
171-
* @param entityIds The entity ids to remove from the table.
172202
* @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).
173204
* @param error An error object is returned if an error occurs.
174205
*/
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;
178209

179210
/**
180-
* Retrieve all entries from the table.
211+
* Deletes the first N events (i.e., the N oldest events).
181212
*
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).
183216
* @param error An error object is returned if an error occurs.
184-
* @return The return value is an array of OPTLYDatabaseEntity.
185217
*/
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;
188222

189223
/**
190-
* Retrieves a set of N entries from the table.
224+
* Deletes all events.
191225
*
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).
194228
* @param error An error object is returned if an error occurs.
195-
* @return The return value is an array of OPTLYDatabaseEntity.
196229
*/
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;
200233

201234
/**
202235
* Returns the number of saved events.
203236
*
204237
* @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).
205239
* @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.
207241
*/
208242
- (NSInteger)numberOfEvents:(OPTLYDataStoreEventType)eventType
243+
cacheData:(bool)cacheData
209244
error:(NSError * _Nullable * _Nullable)error;
210-
#endif
211245

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+
213262
/**
214263
* Saves data in dictionary format in NSUserDefault
215264
*
216265
* @param data The dictionary data to save.
217266
* @param dataType The type of data (e.g., datafile, user profile, event dispatcher, etc.)
218267
*/
219-
- (void)save:(nonnull NSDictionary *)data type:(OPTLYDataStoreDataType)dataType;
268+
- (void)saveUserData:(nonnull NSDictionary *)data type:(OPTLYDataStoreDataType)dataType;
220269

221270
/**
222271
* Gets saved data from NSUserDefault.
223272
*
224273
* @param dataType The type of data (e.g., datafile, user profile, event dispatcher, etc.)
225274
* @return data retrieved.
226275
*/
227-
- (nullable NSDictionary *)getDataForType:(OPTLYDataStoreDataType)dataType;
276+
- (nullable NSDictionary *)getUserDataForType:(OPTLYDataStoreDataType)dataType;
228277

229278
/**
230279
* Removes data for a particular type of data in NSUserDefault.
231280
*
232281
* @param dataType The type of data (e.g., datafile, user profile, event dispatcher, etc.)
233282
*/
234-
- (void)removeDataForType:(OPTLYDataStoreDataType)dataType;
283+
- (void)removeUserDataForType:(OPTLYDataStoreDataType)dataType;
235284

236285
/**
237286
* Removes all Optimizely-related data in NSUserDefault.
238287
*/
239-
- (void)removeAllData;
288+
- (void)removeAllUserData;
240289

241290
/**
242291
* Removes an object from the dictionary data saved in NSUserDefault.
243292
*
244293
* @param dataKey The key for the dictionary data to remove.
245294
* @param dataType The type of data (e.g., datafile, user profile, event dispatcher, etc.)
246295
*/
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;
262297

263-
- (NSInteger)numberOfCachedItems:(OPTLYDataStoreEventType)eventType;
264298
@end

0 commit comments

Comments
 (0)