@@ -241,54 +241,42 @@ - (void)fetchABTestWithModeType:(SABFetchABTestModeType)type paramName:(NSString
241241 SABLogError (@" paramName: %@ error,paramName must be a valid string!" , paramName);
242242 return ;
243243 }
244-
245- dispatch_block_t fetchABTestBlock = ^() {
246- switch (type) {
247- case SABFetchABTestModeTypeCache: {
248- // 从缓存读取
249- id cacheValue = [self fetchCacheABTestWithParamName: paramName defaultValue: defaultValue];
250- return completionHandler (cacheValue ? : defaultValue);
251- }
252- case SABFetchABTestModeTypeFast: {
253- id cacheValue = [self fetchCacheABTestWithParamName: paramName defaultValue: defaultValue];
254- if (cacheValue) {
255- return completionHandler (cacheValue);
256- }
257- [self fetchAsyncABTestWithParamName: paramName defaultValue: defaultValue timeoutInterval: timeoutInterval completionHandler: completionHandler];
258- break ;
259- }
260- case SABFetchABTestModeTypeAsync: {
261- // 异步请求
262- [self fetchAsyncABTestWithParamName: paramName defaultValue: defaultValue timeoutInterval: timeoutInterval completionHandler: completionHandler];
244+
245+ switch (type) {
246+ case SABFetchABTestModeTypeCache: {
247+ // 从缓存读取
248+ id cacheValue = [self fetchCacheABTestWithParamName: paramName defaultValue: defaultValue];
249+ return completionHandler (cacheValue ? : defaultValue);
250+ }
251+ case SABFetchABTestModeTypeFast: {
252+ id cacheValue = [self fetchCacheABTestWithParamName: paramName defaultValue: defaultValue];
253+ if (cacheValue) {
254+ dispatch_async (dispatch_get_main_queue (), ^{
255+ completionHandler (cacheValue);
256+ });
257+ return ;
263258 }
264- break ;
265- default :
266- break ;
259+ [self fetchAsyncABTestWithParamName: paramName defaultValue: defaultValue timeoutInterval: timeoutInterval completionHandler: completionHandler];
260+ break ;
267261 }
268- };
269-
270- dispatch_queue_t saSerialQueue = [SABBridge saSerialQueue ];
271- if (saSerialQueue) {
272- // Cache 获取试验,同步执行
273- if (type == SABFetchABTestModeTypeCache) {
274- dispatch_sync (saSerialQueue, fetchABTestBlock);
275- } else {
276- dispatch_async (saSerialQueue, fetchABTestBlock);
262+ case SABFetchABTestModeTypeAsync: {
263+ // 异步请求
264+ [self fetchAsyncABTestWithParamName: paramName defaultValue: defaultValue timeoutInterval: timeoutInterval completionHandler: completionHandler];
277265 }
278- } else {
279- fetchABTestBlock ();
266+ break ;
267+ default :
268+ break ;
280269 }
281270}
282271
283272// / 获取缓存试验
284273- (nullable id )fetchCacheABTestWithParamName : (NSString *)paramName defaultValue : (id )defaultValue {
285-
286274 if (![SABValidUtils isValidString: paramName]) {
287275 SABLogError (@" paramName::%@ error,paramName must be a valid string!" , paramName);
288276 return nil ;
289277 }
290278 SABExperimentResult *resultData = [self .dataManager cachedExperimentResultWithParamName: paramName];
291-
279+
292280 if (!resultData) {
293281 SABLogWarn (@" The cache experiment result is empty, paramName: %@ " , paramName);
294282 return nil ;
@@ -300,7 +288,7 @@ - (nullable id)fetchCacheABTestWithParamName:(NSString *)paramName defaultValue:
300288 SABLogWarn (@" The experiment result type is inconsistent with the defaultValue type of the interface setting, paramName: %@ , experiment result type: %@ , defaultValue type: %@ " , paramName, resulTypeString, defaultValueTypeString);
301289 return nil ;
302290 }
303-
291+
304292 if (!resultData.isWhiteList ) {
305293 // 埋点
306294 [self trackABTestWithData: resultData];
@@ -313,19 +301,23 @@ - (void)fetchAsyncABTestWithParamName:(NSString *)paramName defaultValue:(id)def
313301 // 异步请求
314302 SABExperimentRequest *requestData = [[SABExperimentRequest alloc ] initWithBaseURL: self .configOptions.baseURL projectKey: self .configOptions.projectKey];
315303 requestData.timeoutInterval = timeoutInterval;
304+
305+ __weak typeof (self) weakSelf = self;
316306 [self .dataManager asyncFetchAllExperimentWithRequest: requestData completionHandler: ^(SABFetchResultResponse *_Nullable responseData, NSError *_Nullable error) {
307+ __strong typeof (weakSelf) strongSelf = weakSelf;
308+
317309 if (error || !responseData) {
318310 SABLogError (@" asyncFetchAllExperimentWithRequest failure,error: %@ " , error);
319311 completionHandler (defaultValue);
320312 return ;
321313 }
322314
323315 // 获取缓存并触发 $ABTestTrigger 事件
324- id cacheValue = [self fetchCacheABTestWithParamName: paramName defaultValue: defaultValue];
316+ id cacheValue = [strongSelf fetchCacheABTestWithParamName: paramName defaultValue: defaultValue];
325317
326318 // 切到主线程回调结果
327319 dispatch_async (dispatch_get_main_queue (), ^{
328- completionHandler (cacheValue ?: defaultValue);
320+ completionHandler (cacheValue ? : defaultValue);
329321 });
330322 }];
331323}
@@ -339,21 +331,19 @@ - (void)fetchAllABTestResult {
339331// / @param times 最大次数
340332- (void )fetchAllABTestResultWithTimes : (NSInteger )times {
341333 NSInteger fetchIndex = times - 1 ;
342-
334+
343335 SABExperimentRequest *requestData = [[SABExperimentRequest alloc ] initWithBaseURL: self .configOptions.baseURL projectKey: self .configOptions.projectKey];
344-
345- [self .dataManager asyncFetchAllExperimentWithRequest: requestData completionHandler: ^(SABFetchResultResponse * _Nullable responseData, NSError * _Nullable error) {
346-
336+
337+ [self .dataManager asyncFetchAllExperimentWithRequest: requestData completionHandler: ^(SABFetchResultResponse *_Nullable responseData, NSError *_Nullable error) {
347338 if (fetchIndex <= 0 || !error) {
348339 return ;
349340 }
350341 dispatch_after (dispatch_time (DISPATCH_TIME_NOW , (int64_t )(kSABAsyncFetchExperimentRetryIntervalTime * NSEC_PER_SEC )), dispatch_get_main_queue (), ^{
351- [self fetchAllABTestResultWithTimes: fetchIndex];
352- });
342+ [self fetchAllABTestResultWithTimes: fetchIndex];
343+ });
353344 }];
354345}
355346
356-
357347#pragma mark - action
358348// / track $ABTestTrigger
359349- (void )trackABTestWithData : (SABExperimentResult *)resultData {
@@ -388,17 +378,15 @@ - (void)trackABTestWithData:(SABExperimentResult *)resultData {
388378 properties[kSABLoginId ] = userIdenty.loginId ;
389379 properties[kSABDistinctId ] = userIdenty.distinctId ;
390380 properties[kSABAnonymousId ] = userIdenty.anonymousId ;
391-
381+
392382 static dispatch_once_t onceToken;
393383 dispatch_once (&onceToken, ^{
394384 // 首次触发事件,添加版本号
395385 NSString *libVersion = [NSString stringWithFormat: @" %@ :%@ " , kSABLibPrefix , kSABLibVersion ];
396386 properties[kSABLibPluginVersion ] = @[libVersion];
397387 });
398388
399- dispatch_async (dispatch_get_main_queue (), ^{
400- [SABBridge track: kSABTriggerEventName properties: properties];
401- });
389+ [SABBridge track: kSABTriggerEventName properties: properties];
402390}
403391
404392// / 开始更新计时
0 commit comments