Skip to content

Commit 22843ee

Browse files
(fix) wrap lazy loaded optlyfilemanager in a serial queue (#342)
* don't lazy load file manager since it is used in every method call and seems to be a pretty lightweight object. * don't need getter method for data store * had to use a queue for lazy loading since there was some problem creating the file manager in the init call. * fix using queue * change queue name to something more unique * test outside of queue before trying to create file manager * fix weak self and use strong self inside of dispatch_sync block
1 parent 79221b4 commit 22843ee

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

OptimizelySDKShared/OptimizelySDKShared/OPTLYDataStore.m

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
@interface OPTLYDataStore()
4444
@property (nonatomic, strong) OPTLYFileManager *fileManager;
4545
@property (nonatomic, strong) id<OPTLYEventDataStore> eventDataStore;
46+
@property (nonatomic, strong) dispatch_queue_t fileManagerCreateQueue;
4647
@end
4748

4849
@implementation OPTLYDataStore
@@ -71,6 +72,8 @@ - (id)init {
7172
filePath = NSTemporaryDirectory();
7273
_baseDirectory = [filePath stringByAppendingPathComponent:kOptimizelyDirectory];
7374
#endif
75+
76+
_fileManagerCreateQueue = dispatch_queue_create("OptlyFileManagerCreateQueue", DISPATCH_QUEUE_SERIAL);
7477
}
7578
return self;
7679
}
@@ -93,13 +96,6 @@ - (id)init {
9396
return _eventDataStore;
9497
}
9598

96-
- (OPTLYFileManager *)fileManager {
97-
if (!_fileManager) {
98-
_fileManager = [[OPTLYFileManager alloc] initWithBaseDir:self.baseDirectory];
99-
}
100-
return _fileManager;
101-
}
102-
10399
- (BOOL)removeAll:(NSError * _Nullable __autoreleasing * _Nullable)error {
104100
BOOL ok = YES;
105101
[self removeAllUserData];
@@ -152,6 +148,26 @@ - (void)removeAllUserData
152148
}
153149

154150
# pragma mark - File Manager Methods
151+
- (OPTLYFileManager *)fileManager {
152+
if (_fileManager == nil) {
153+
[self loadFileManagerIfNecessary];
154+
}
155+
return _fileManager;
156+
157+
}
158+
159+
- (void) loadFileManagerIfNecessary {
160+
__block __weak OPTLYDataStore *weakSelf = self;
161+
dispatch_sync(_fileManagerCreateQueue, ^{
162+
if (weakSelf != nil) {
163+
OPTLYDataStore *strongSelf = weakSelf;
164+
if (strongSelf->_fileManager == nil) {
165+
strongSelf->_fileManager = [[OPTLYFileManager alloc] initWithBaseDir:strongSelf.baseDirectory];
166+
}
167+
}
168+
});
169+
}
170+
155171
- (BOOL)saveFile:(nonnull NSString *)fileName
156172
data:(nonnull NSData *)data
157173
type:(OPTLYDataStoreDataType)dataType

0 commit comments

Comments
 (0)