38
38
// / 所有命中试验记录属性 key
39
39
static NSString * const kSABAllHitExperimentResultIdSourcesKey = @" abtest_result" ;
40
40
41
- @interface SABExperimentDataManager ()
41
+ @interface SABExperimentDataManager () {
42
+ __block SABFetchResultResponse *_resultResponse;
43
+ }
42
44
43
45
// / 试验结果
44
- @property (atomic , strong ) SABFetchResultResponse *resultResponse;
46
+ @property (nonatomic , strong ) SABFetchResultResponse *resultResponse;
45
47
46
48
// / 试验参数白名单
47
49
// /
@@ -66,11 +68,11 @@ @implementation SABExperimentDataManager
66
68
- (instancetype )initWithSerialQueue : (dispatch_queue_t )serialQueue {
67
69
self = [super init ];
68
70
if (self) {
71
+ [self resgisterStorePlugins ];
72
+
69
73
_serialQueue = serialQueue;
70
74
_hitRecordsManager = [[SABHitExperimentRecordsManager alloc ] initWithSerialQueue: serialQueue];
71
75
72
- [self resgisterStorePlugins ];
73
-
74
76
[self buildUserIdenty ];
75
77
76
78
// 解析事件触发配置
@@ -99,7 +101,7 @@ - (void)buildUserIdenty {
99
101
SABUserIdenty *userIdenty = [[SABUserIdenty alloc ] initWithDistinctId: distinctId loginId: loginId anonymousId: anonymousId];
100
102
101
103
// 读取本地缓存前,需要先读取自定义主体 ID
102
- NSDictionary *customIDs = [[ SABStoreManager sharedInstance ] dictionaryForKey: kSABCustomIDsFileName ];
104
+ NSDictionary *customIDs = [SABStoreManager. sharedInstance dictionaryForKey: kSABCustomIDsFileName ];
103
105
if (customIDs.count > 0 ) {
104
106
userIdenty.customIDs = customIDs;
105
107
}
@@ -109,7 +111,7 @@ - (void)buildUserIdenty {
109
111
110
112
- (void )updateCustomIDs : (NSDictionary <NSString *,NSString *> *)customIDs {
111
113
self.currentUserIndenty .customIDs = customIDs;
112
- [[ SABStoreManager sharedInstance ] setObject: customIDs forKey: kSABCustomIDsFileName ];
114
+ [SABStoreManager. sharedInstance setObject: customIDs forKey: kSABCustomIDsFileName ];
113
115
}
114
116
115
117
- (void )updateUserIdenty {
@@ -159,6 +161,23 @@ - (void)asyncFetchAllExperimentWithRequest:(SABExperimentRequest *)requestData c
159
161
}];
160
162
}
161
163
164
+ #pragma mark - readWrite
165
+ // 增加读写锁,保证数据的线程安全
166
+ - (SABFetchResultResponse *)resultResponse {
167
+ __block SABFetchResultResponse *response;
168
+ sabtest_dispatch_safe_sync (self.serialQueue , ^{
169
+ response = _resultResponse;
170
+ });
171
+ return response;
172
+ }
173
+
174
+ - (void )setResultResponse : (SABFetchResultResponse *)resultResponse {
175
+ sabtest_dispatch_safe_async (self.serialQueue , ^{
176
+ _resultResponse = resultResponse;
177
+ });
178
+ }
179
+
180
+
162
181
#pragma mark - query
163
182
// / 查询扩展试验信息属性,作为预置属性采集
164
183
- (NSDictionary *)queryExtendedPropertiesWithExperimentResult : (SABExperimentResult *)resultData {
@@ -176,10 +195,15 @@ - (NSDictionary *)queryExtendedPropertiesWithExperimentResult:(SABExperimentResu
176
195
- (void )unarchiveExperimentResult {
177
196
dispatch_async (self.serialQueue , ^{
178
197
179
- id result = [SABStoreManager.sharedInstance objectForKey: kSABExperimentResultFileName ];
198
+ NSData *data = [SABStoreManager.sharedInstance objectForKey: kSABExperimentResultFileName ];
199
+ if (![data isKindOfClass: NSData .class]) {
200
+ SABLogDebug (@" unarchiveExperimentResult objectForKey failure %@ " , data);
201
+ return ;
202
+ }
203
+ SABFetchResultResponse *result = [NSKeyedUnarchiver unarchiveObjectWithData: data];
180
204
// 解析缓存
181
205
if (![result isKindOfClass: SABFetchResultResponse.class]) {
182
- SABLogDebug (@" unarchiveExperimentResult failure %@ " , result);
206
+ SABLogDebug (@" unarchiveExperimentResult unarchiveObjectWithData failure %@ " , result);
183
207
return ;
184
208
}
185
209
@@ -200,8 +224,9 @@ - (void)unarchiveExperimentResult {
200
224
// / 写入本地缓存
201
225
- (void )archiveExperimentResult : (SABFetchResultResponse *)resultResponse {
202
226
// 存储到本地
227
+ NSData *data = [NSKeyedArchiver archivedDataWithRootObject: resultResponse];
203
228
dispatch_async (self.serialQueue , ^{
204
- [SABStoreManager.sharedInstance setObject: resultResponse forKey: kSABExperimentResultFileName ];
229
+ [SABStoreManager.sharedInstance setObject: data forKey: kSABExperimentResultFileName ];
205
230
});
206
231
}
207
232
@@ -211,7 +236,7 @@ - (SABExperimentResult *)cachedExperimentResultWithParamName:(NSString *)paramNa
211
236
if (![SABValidUtils isValidString: paramName]) {
212
237
return nil ;
213
238
}
214
-
239
+
215
240
__block SABExperimentResult *result = nil ;
216
241
dispatch_sync (self.serialQueue , ^{
217
242
result = self.resultResponse .results [paramName];
@@ -224,7 +249,7 @@ - (SABExperimentResult *)queryOutResultWithParamName:(NSString *)paramName {
224
249
if (![SABValidUtils isValidString: paramName]) {
225
250
return nil ;
226
251
}
227
-
252
+
228
253
__block SABExperimentResult *result = nil ;
229
254
dispatch_sync (self.serialQueue , ^{
230
255
result = self.resultResponse .outResults [paramName];
@@ -324,11 +349,11 @@ - (BOOL)enableTrackWithHitExperiment:(SABExperimentResult *)resultData {
324
349
- (void )resgisterStorePlugins {
325
350
// 文件明文存储,兼容历史本地数据
326
351
SABFileStorePlugin *filePlugin = [[SABFileStorePlugin alloc ] init ];
327
- [[ SABStoreManager sharedInstance ] registerStorePlugin: filePlugin];
352
+ [SABStoreManager. sharedInstance registerStorePlugin: filePlugin];
328
353
329
354
// 注册 SA 的自定义插件
330
355
for (id <SAStorePlugin> plugin in SABBridge.storePlugins ) {
331
- [[ SABStoreManager sharedInstance ] registerStorePlugin: plugin];
356
+ [SABStoreManager. sharedInstance registerStorePlugin: plugin];
332
357
}
333
358
}
334
359
0 commit comments