14
14
* limitations under the License. *
15
15
***************************************************************************/
16
16
17
- <<<<<<< HEAD
18
- <<<<<<< HEAD
19
17
#import < UIKit/UIKit.h>
20
18
#import < OptimizelySDKCore/OPTLYErrorHandler.h>
21
19
#import < OptimizelySDKCore/OPTLYLog.h>
22
20
#import < OptimizelySDKCore/OPTLYLogger.h>
23
21
#import < OptimizelySDKShared/OPTLYDataStore.h>
24
22
#import < OptimizelySDKShared/OPTLYNetworkService.h>
25
23
#import " OPTLYDatafileManager.h"
26
- =======
27
- #import " OPTLYDatafileManager.h"
28
- =======
29
- #import < UIKit/UIKit.h>
30
- #import < OptimizelySDKCore/OPTLYLog.h>
31
- >>>>>>> Added a timer to the datafile manager to periodically download the datafile. Also moved the datafile manager protocol to core as the core should have a basic datafile downloader (the network classes will be moved to core as well in another commit.). Cleaned up the headers and was being more deligent about alphabetizing imports and initializing modules.
32
- #import < OptimizelySDKCore/OPTLYErrorHandler.h>
33
- #import < OptimizelySDKShared/OPTLYDataStore.h>
34
- <<<<<<< HEAD
35
- >>>>>>> Moved the network manager code to the Core. This is needed for basic event dispatch and datafile download.
36
- =======
37
- #import < OptimizelySDKShared/OPTLYNetworkService.h>
38
- #import " OPTLYDatafileManager.h"
39
- >>>>>>> Added a timer to the datafile manager to periodically download the datafile. Also moved the datafile manager protocol to core as the core should have a basic datafile downloader (the network classes will be moved to core as well in another commit.). Cleaned up the headers and was being more deligent about alphabetizing imports and initializing modules.
40
24
41
- NSTimeInterval const kDefaultDatafileFetchInterval = 0 ;
25
+ NSTimeInterval const kDefaultDatafileFetchInterval_s = 120 ;
42
26
43
27
@interface OPTLYDatafileManager ()
44
28
@property (nonatomic , strong ) OPTLYDataStore *dataStore;
@@ -56,7 +40,7 @@ - (instancetype)initWithBuilder:(OPTLYDatafileManagerBuilder *)builder {
56
40
if (builder != nil ) {
57
41
self = [super init ];
58
42
if (self != nil ) {
59
- _datafileFetchInterval = kDefaultDatafileFetchInterval ;
43
+ _datafileFetchInterval = kDefaultDatafileFetchInterval_s ;
60
44
_datafileFetchInterval = builder.datafileFetchInterval ;
61
45
_projectId = builder.projectId ;
62
46
_errorHandler = builder.errorHandler ;
@@ -77,50 +61,50 @@ - (instancetype)initWithBuilder:(OPTLYDatafileManagerBuilder *)builder {
77
61
}
78
62
79
63
- (void )downloadDatafile : (NSString *)projectId completionHandler : (OPTLYHTTPRequestManagerResponse)completion {
80
-
81
64
NSString *logMessage = [NSString stringWithFormat: OPTLYLoggerMessagesDatafileManagerDatafileDownloading, projectId];
82
65
[self .logger logMessage: logMessage withLevel: OptimizelyLogLevelInfo];
83
66
84
67
NSString *lastSavedModifiedDate = [self getLastModifiedDate: projectId];
85
68
logMessage = [NSString stringWithFormat: OPTLYLoggerMessagesDatafileManagerLastModifiedDate, lastSavedModifiedDate];
86
69
[self .logger logMessage: logMessage withLevel: OptimizelyLogLevelDebug];
87
-
70
+
88
71
[self .networkService downloadProjectConfig: self .projectId
89
72
lastModified: lastSavedModifiedDate
90
73
completionHandler: ^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
91
-
92
- NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
93
- NSInteger statusCode = [httpResponse statusCode ];
94
- NSString *logMessage = @" " ;
95
- if (error != nil ) {
96
- [self .errorHandler handleError: error];
97
- }
98
- else if (statusCode == 200 ) { // got datafile OK
99
- [self saveDatafile: data];
100
-
101
- // save the last modified date
102
- NSDictionary *dictionary = [httpResponse allHeaderFields ];
103
- NSString *lastModifiedDate = [dictionary valueForKey: @" Last-Modified" ];
104
- [self saveLastModifiedDate: lastModifiedDate project: projectId];
105
-
106
- logMessage = [NSString stringWithFormat: OPTLYLoggerMessagesDatafileManagerDatafileDownloaded, projectId, lastModifiedDate];
107
- [self .logger logMessage: logMessage withLevel: OptimizelyLogLevelInfo];
108
- }
109
- else if (statusCode == 304 ) {
110
- logMessage = [NSString stringWithFormat: OPTLYLoggerMessagesDatafileManagerDatafileNotDownloadedNoChanges, projectId];
111
- [self .logger logMessage: logMessage withLevel: OptimizelyLogLevelDebug];
112
- }
113
- else {
114
- // TODO: Josh W. handle bad response
115
- logMessage = [NSString stringWithFormat: OPTLYLoggerMessagesDatafileManagerDatafileNotDownloadedError, projectId, error];
116
- [self .logger logMessage: logMessage withLevel: OptimizelyLogLevelDebug];
117
- }
118
-
119
- // call the completion handler
120
- if (completion != nil ) {
121
- completion (data, response, error);
122
- }
123
- }];
74
+ NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
75
+ NSInteger statusCode = [httpResponse statusCode ];
76
+ NSString *logMessage = @" " ;
77
+ if (error != nil ) {
78
+ [self .errorHandler handleError: error];
79
+ }
80
+ else if (statusCode == 200 ) { // got datafile OK
81
+ [self saveDatafile: data];
82
+
83
+ // save the last modified date
84
+ NSDictionary *dictionary = [httpResponse allHeaderFields ];
85
+ NSString *lastModifiedDate = [dictionary valueForKey: @" Last-Modified" ];
86
+ [self saveLastModifiedDate: lastModifiedDate project: projectId];
87
+
88
+ logMessage = [NSString stringWithFormat: OPTLYLoggerMessagesDatafileManagerDatafileDownloaded, projectId, lastModifiedDate];
89
+ [self .logger logMessage: logMessage withLevel: OptimizelyLogLevelInfo];
90
+ }
91
+ else if (statusCode == 304 ) {
92
+ logMessage = [NSString stringWithFormat: OPTLYLoggerMessagesDatafileManagerDatafileNotDownloadedNoChanges, projectId];
93
+ [self .logger logMessage: logMessage withLevel: OptimizelyLogLevelDebug];
94
+ }
95
+ else {
96
+ // TODO: Josh W. handle bad response
97
+ logMessage = [NSString stringWithFormat: OPTLYLoggerMessagesDatafileManagerDatafileNotDownloadedError, projectId, error];
98
+ [self .logger logMessage: logMessage withLevel: OptimizelyLogLevelDebug];
99
+ }
100
+
101
+
102
+
103
+ // call the completion handler
104
+ if (completion != nil ) {
105
+ completion (data, response, error);
106
+ }
107
+ }];
124
108
}
125
109
126
110
- (void )downloadDatafile {
@@ -155,7 +139,7 @@ - (void)saveLastModifiedDate:(nonnull NSString *)lastModifiedDate
155
139
- (nullable NSString *)getLastModifiedDate : (nonnull NSString *)projectId {
156
140
NSDictionary *userData = [self .dataStore getUserDataForType: OPTLYDataStoreDataTypeDatafile];
157
141
NSString *lastModifiedDate = [userData objectForKey: projectId];
158
-
142
+
159
143
NSString *logMessage = @" " ;
160
144
if ([lastModifiedDate length ]) {
161
145
logMessage = [NSString stringWithFormat: OPTLYLoggerMessagesDatafileManagerLastModifedDate, lastModifiedDate, projectId];
@@ -167,6 +151,7 @@ - (nullable NSString *)getLastModifiedDate:(nonnull NSString *)projectId {
167
151
return lastModifiedDate;
168
152
}
169
153
154
+
170
155
#pragma mark - Application Lifecycle Handlers
171
156
- (void )setupApplicationNotificationHandlers {
172
157
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter ];
0 commit comments