Skip to content

Commit a5361df

Browse files
Added isDatafileCached flag.
1 parent a5629da commit a5361df

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

OptimizelySDKDatafileManager/OptimizelySDKDatafileManager/OPTLYDatafileManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
/// The time interval to regularly fetch the datafile.
2626
@property (nonatomic, readonly) NSTimeInterval datafileFetchInterval;
27+
/// This flag specifies if the datafile has been cached or not.
28+
@property (nonatomic, assign) BOOL isDatafileCached;
2729
/// The project ID of the datafile this datafile manager will monitor
2830
@property (nonatomic, readonly, strong, nonnull) NSString *projectId;
2931
/// The error handler to be used for the manager, client, and all subcomponents

OptimizelySDKDatafileManager/OptimizelySDKDatafileManager/OPTLYDatafileManager.m

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
***************************************************************************/
1616

1717
#import <UIKit/UIKit.h>
18-
#import <OptimizelySDKCore/OPTLYLog.h>
1918
#import <OptimizelySDKCore/OPTLYErrorHandler.h>
19+
#import <OptimizelySDKCore/OPTLYLog.h>
2020
#import <OptimizelySDKShared/OPTLYDataStore.h>
2121
#import <OptimizelySDKShared/OPTLYNetworkService.h>
2222
#import "OPTLYDatafileManager.h"
@@ -64,21 +64,21 @@ - (void)downloadDatafile:(NSString *)projectId completionHandler:(OPTLYHTTPReque
6464
OPTLYLogInfo(@"Downloading datafile: %@", projectId);
6565
[self.networkService downloadProjectConfig:self.projectId
6666
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
67-
if (error != nil) {
68-
[self.errorHandler handleError:error];
69-
}
70-
else if ([(NSHTTPURLResponse *)response statusCode] == 200) { // got datafile OK
71-
[self saveDatafile:data];
72-
OPTLYLogInfo(@"Datafile for project ID %@ downloaded. Saving datafile.");
73-
}
74-
else {
75-
// TODO: Josh W. handle bad response
76-
}
77-
// call the completion handler
78-
if (completion != nil) {
79-
completion(data, response, error);
80-
}
81-
}];
67+
if (error != nil) {
68+
[self.errorHandler handleError:error];
69+
}
70+
else if ([(NSHTTPURLResponse *)response statusCode] == 200) { // got datafile OK
71+
[self saveDatafile:data];
72+
OPTLYLogInfo(@"Datafile for project ID %@ downloaded. Saving datafile.");
73+
}
74+
else {
75+
// TODO: Josh W. handle bad response
76+
}
77+
// call the completion handler
78+
if (completion != nil) {
79+
completion(data, response, error);
80+
}
81+
}];
8282
}
8383

8484
- (void)downloadDatafile {
@@ -94,6 +94,11 @@ - (void)saveDatafile:(NSData *)datafile {
9494

9595
}
9696

97+
- (BOOL)isDatafileCached {
98+
BOOL isCached = [self.dataStore fileExists:self.projectId type:OPTLYDataStoreDataTypeDatafile];
99+
return isCached;
100+
}
101+
97102
#pragma mark - Application Lifecycle Handlers
98103
- (void)setupApplicationNotificationHandlers {
99104
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];

OptimizelySDKDatafileManager/OptimizelySDKDatafileManagerTests/OPTLYDatafileManagerTest.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,22 @@ - (void)testNetworkTimerIsDisabled
203203
XCTAssertFalse(datafileManager.datafileDownloadTimer.valid, @"Timer should not be valid.");
204204
}
205205

206+
- (void)testIsDatafileCachedFlag
207+
{
208+
// setup datafile manager and datastore
209+
OPTLYDatafileManager *datafileManager = [OPTLYDatafileManager initWithBuilderBlock:^(OPTLYDatafileManagerBuilder * _Nullable builder) {
210+
builder.projectId = kProjectId;
211+
}];
212+
213+
XCTAssertFalse(datafileManager.isDatafileCached, @"Datafile cached flag should be false.");
214+
215+
// get the datafile
216+
NSData *datafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject:kDatamodelDatafileName];
217+
218+
// save the datafile
219+
[datafileManager saveDatafile:datafile];
220+
221+
XCTAssertTrue(datafileManager.isDatafileCached, @"Datafile cached flag should be true.");
222+
}
223+
206224
@end

0 commit comments

Comments
 (0)