Skip to content

Commit cc74b7b

Browse files
committed
Release 0.0.3
1 parent ba0b36a commit cc74b7b

17 files changed

+237
-96
lines changed

Podfile.lock

-25
This file was deleted.

SensorsABTest.xcodeproj/project.pbxproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@
523523
"@loader_path/Frameworks",
524524
);
525525
MACH_O_TYPE = mh_dylib;
526-
MARKETING_VERSION = 0.0.2;
526+
MARKETING_VERSION = 0.0.3;
527527
PRODUCT_BUNDLE_IDENTIFIER = cn.sensorsdata.SensorsABTest;
528528
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
529529
SKIP_INSTALL = YES;
@@ -554,7 +554,7 @@
554554
"@loader_path/Frameworks",
555555
);
556556
MACH_O_TYPE = mh_dylib;
557-
MARKETING_VERSION = 0.0.2;
557+
MARKETING_VERSION = 0.0.3;
558558
PRODUCT_BUNDLE_IDENTIFIER = cn.sensorsdata.SensorsABTest;
559559
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
560560
SKIP_INSTALL = YES;

SensorsABTest/DataManger/SABExperimentDataManager.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ NS_ASSUME_NONNULL_BEGIN
3333
- (nullable SABExperimentResult *)cachedExperimentResultWithParamName:(NSString *)paramName;
3434

3535
/// 异步请求所有试验
36-
- (void)asyncFetchAllExperimentWithRequest:(NSURLRequest *)request completionHandler:(SABFetchResultResponseCompletionHandler)completionHandler;
36+
- (void)asyncFetchAllExperimentWithRequest:(SABExperimentRequest *)requestData completionHandler:(SABFetchResultResponseCompletionHandler)completionHandler;
37+
38+
/// 校验当前缓存试验,如果 distinctId 不一致则删除
39+
- (void)validateExperiment;
3740

3841
@end
3942

SensorsABTest/DataManger/SABExperimentDataManager.m

+57-13
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@
2727
#import "SABLogBridge.h"
2828
#import "SABValidUtils.h"
2929
#import "SABFileStore.h"
30+
#import "SABBridge.h"
3031

3132
@interface SABExperimentDataManager()
3233

3334
/// 试验结果
34-
@property (atomic, copy) NSDictionary<NSString *, SABExperimentResult *> *experimentResults;
35+
@property (atomic, strong) SABFetchResultResponse *resultResponse;
3536
@property (nonatomic, strong) dispatch_queue_t serialQueue;
3637
@end
3738

@@ -48,9 +49,10 @@ - (instancetype)init {
4849
return self;
4950
}
5051

51-
- (void)asyncFetchAllExperimentWithRequest:(NSURLRequest *)request completionHandler:(SABFetchResultResponseCompletionHandler)completionHandler {
52-
53-
[SABNetwork dataTaskWithRequest:request completionHandler:^(id _Nullable jsonObject, NSError *_Nullable error) {
52+
- (void)asyncFetchAllExperimentWithRequest:(SABExperimentRequest *)requestData completionHandler:(SABFetchResultResponseCompletionHandler)completionHandler {
53+
54+
NSString *requestDistinctId = [SABBridge distinctId];
55+
[SABNetwork dataTaskWithRequest:requestData.request completionHandler:^(id _Nullable jsonObject, NSError *_Nullable error) {
5456
if (error) {
5557
SABLogError(@"asyncFetchAllABTest failure, error: %@, jsonObject: %@", error, jsonObject);
5658
completionHandler(nil, error);
@@ -64,17 +66,35 @@ - (void)asyncFetchAllExperimentWithRequest:(NSURLRequest *)request completionHan
6466
return;
6567
}
6668

69+
// 判断 distinctId 是否变化
70+
NSString *currentDistinctId = [SABBridge distinctId];
71+
if (![currentDistinctId isEqualToString:requestDistinctId]) {
72+
SABLogWarn(@"distinctId has been changed, requestDistinctId is %@, currentDistinctId is %@, Retry to asyncFetchAllABTest", requestDistinctId, currentDistinctId);
73+
74+
// 刷新用户标识
75+
[requestData refreshUserIdenty];
76+
77+
// 重试请求
78+
[self asyncFetchAllExperimentWithRequest:requestData completionHandler:^(SABFetchResultResponse * _Nullable responseData, NSError * _Nullable error) {
79+
completionHandler(responseData, error);
80+
}];
81+
return;
82+
}
83+
6784
// 数据解析
6885
SABFetchResultResponse *responseData = [[SABFetchResultResponse alloc] initWithDictionary:jsonObject];
6986

7087
// 获取试验成功,更新缓存
7188
if (responseData.status == SABFetchResultResponseStatusSuccess) {
7289
SABLogInfo(@"asyncFetchAllExperiment success jsonObject %@", jsonObject);
73-
self.experimentResults = responseData.results;
90+
91+
// 缓存请求时刻的 distinctId
92+
responseData.distinctId = requestDistinctId;
93+
self.resultResponse = responseData;
7494
// 存储到本地
75-
[self archiveExperimentResult:responseData.results];
95+
[self archiveExperimentResult:responseData];
7696
} else {
77-
SABLogWarn(@"asyncFetchAllExperiment fail,request: %@,jsonObject %@", request, jsonObject);
97+
SABLogWarn(@"asyncFetchAllExperiment fail,request: %@,jsonObject %@", requestData.request, jsonObject);
7898
}
7999
completionHandler(responseData, nil);
80100
}];
@@ -84,18 +104,37 @@ - (void)asyncFetchAllExperimentWithRequest:(NSURLRequest *)request completionHan
84104
- (void)unarchiveExperimentResult {
85105
dispatch_async(self.serialQueue, ^{
86106
NSData *data = [SABFileStore unarchiveWithFileName:kSABExperimentResultFileName];
87-
NSDictionary <NSString *, SABExperimentResult *> *result = [NSKeyedUnarchiver unarchiveObjectWithData:data];
88-
if ([result isKindOfClass:NSDictionary.class] && result.count > 0) {
89-
self.experimentResults = [result copy];
107+
id result = [NSKeyedUnarchiver unarchiveObjectWithData:data];
108+
109+
// 解析缓存
110+
if ([result isKindOfClass:SABFetchResultResponse.class]) {
111+
SABFetchResultResponse *resultResponse = (SABFetchResultResponse *)result;
112+
NSString *distinctId = [SABBridge distinctId];
113+
// 校验缓存试验的 distinctId
114+
if ([resultResponse.distinctId isEqualToString:distinctId] && resultResponse.results.count > 0) {
115+
self.resultResponse = resultResponse;
116+
117+
SABLogInfo(@"unarchiveExperimentResult success jsonObject %@", resultResponse.responseObject);
118+
}
119+
120+
// TODO: v0.0.3 添加,尝试删除一次老版缓存,下个版本移除
121+
} else {
122+
// 首次安装,删除老版缓存
123+
BOOL isDelete = [SABFileStore deleteFileWithFileName:kSABExperimentResultOldFileName];
124+
if (isDelete) {
125+
SABLogInfo(@"delete old version experimentResult success");
126+
} else {
127+
SABLogWarn(@"delete old version experimentResult fail");
128+
}
90129
}
91130
});
92131
}
93132

94133
/// 写入本地缓存
95-
- (void)archiveExperimentResult:(NSDictionary <NSString *, SABExperimentResult *> *)result {
134+
- (void)archiveExperimentResult:(SABFetchResultResponse *)resultResponse {
96135
// 存储到本地
97136
dispatch_async(self.serialQueue, ^{
98-
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:result];
137+
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:resultResponse];
99138
[SABFileStore archiveWithFileName:kSABExperimentResultFileName value:data];
100139
});
101140
}
@@ -107,7 +146,12 @@ - (SABExperimentResult *)cachedExperimentResultWithParamName:(NSString *)paramNa
107146
if (![SABValidUtils isValidString:paramName]) {
108147
return nil;
109148
}
110-
return self.experimentResults[paramName];
149+
return self.resultResponse.results[paramName];
150+
}
151+
152+
- (void)validateExperiment {
153+
// TODO: v0.0.3 添加,因为 SA 的问题,这里或 distinctId 可能不是最新,直接清除缓存即可,待 SA 通知逻辑更新后,改成判断 distinctId 是否一致来清除缓存
154+
self.resultResponse = nil;
111155
}
112156

113157
@end

SensorsABTest/DataManger/SABFetchResultResponse.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*
2828
* @discussion
2929
* 试验结果类型枚举
30-
* SABExperimentResultTypeInvalid 类型无效,可能为 nil
30+
* SABExperimentResultTypeInvalid 类型无效,一般为 nil
3131
* SABExperimentResultTypeInt - INTEGER 类型结果
3232
* SABExperimentResultTypeString - STRING 类型结果
3333
* SABExperimentResultTypeBool - BOOLEAN 类型结果
@@ -98,7 +98,7 @@ typedef NS_ENUM(NSInteger, SABFetchResultResponseStatus) {
9898
@end
9999

100100
/// 网络回调完整数据结构
101-
@interface SABFetchResultResponse : NSObject
101+
@interface SABFetchResultResponse : NSObject<NSCoding>
102102

103103
/// 获取试验状态
104104
@property (nonatomic, assign) SABFetchResultResponseStatus status;
@@ -119,6 +119,9 @@ value: result 试验结果
119119
/// 原始数据
120120
@property (nonatomic, copy) NSDictionary *responseObject;
121121

122+
/// 保存 distinctId,校验试验
123+
@property (nonatomic, copy) NSString *distinctId;
124+
122125
- (instancetype)initWithDictionary:(NSDictionary *)responseDic;
123126

124127
@end

SensorsABTest/DataManger/SABFetchResultResponse.m

+35-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#import "SABFetchResultResponse.h"
2626
#import "SABJSONUtils.h"
2727
#import "SABValidUtils.h"
28+
#import "SABLogBridge.h"
29+
#import "SABBridge.h"
2830

2931
static id dictionaryValueForKey(NSDictionary *dic, NSString *key) {
3032
if (![SABValidUtils isValidDictionary:dic]) {
@@ -54,6 +56,7 @@ - (instancetype)initWithDictionary:(NSDictionary *)configDic {
5456
/// 试验结果类型解析
5557
- (SABExperimentResultType)experimentResultTypeTransformWithString:(NSString *)typeString {
5658
if (![SABValidUtils isValidString:typeString]) {
59+
SABLogWarn(@"experimentResult type error: %@" ,typeString);
5760
return SABExperimentResultTypeInvalid;
5861
}
5962

@@ -66,6 +69,7 @@ - (SABExperimentResultType)experimentResultTypeTransformWithString:(NSString *)t
6669
} else if ([typeString isEqualToString:@"JSON"]) {
6770
return SABExperimentResultTypeJSON;
6871
} else {
72+
SABLogWarn(@"experimentResult type error: %@" ,typeString);
6973
return SABExperimentResultTypeInvalid;
7074
}
7175
}
@@ -240,11 +244,11 @@ - (instancetype)initWithDictionary:(NSDictionary *)responseDic {
240244
}
241245
/*
242246
{
243-
"status": "SUCCESS", 查询结果标志(SUCCESS:进行试验;FAILED:不进入试验)
244-
"error_type": "", 错误类型,请求结果为 FAILED 时返回
245-
"error": "", 错误描述信息
246-
"results": [{ 用户命中的所有试验结果
247-
}]
247+
"status": "SUCCESS", 查询结果标志(SUCCESS:进行试验;FAILED:不进入试验)
248+
"error_type": "", 错误类型,请求结果为 FAILED 时返回
249+
"error": "", 错误描述信息
250+
"results": [{ 用户命中的所有试验结果
251+
}]
248252
}
249253
*/
250254
if ([dictionaryValueForKey(responseDic, @"status") isEqualToString:@"SUCCESS"]) {
@@ -254,7 +258,7 @@ - (instancetype)initWithDictionary:(NSDictionary *)responseDic {
254258
}
255259
_errorType = dictionaryValueForKey(responseDic, @"error_type");
256260
_errorMessage = dictionaryValueForKey(responseDic, @"error");
257-
261+
258262
NSArray <NSDictionary *> *results = dictionaryValueForKey(responseDic, @"results");
259263
if (results.count > 0) {
260264
// 构造试验数据
@@ -274,15 +278,38 @@ - (instancetype)initWithDictionary:(NSDictionary *)responseDic {
274278
newResult.variable = variable;
275279
resultsDic[variable.paramName] = newResult;
276280
}
277-
281+
278282
}
279283
_results = [resultsDic copy];
280284
}
281-
285+
282286
_responseObject = [responseDic copy];
283287
}
284288
return self;
285289
}
286290

291+
#pragma mark NSCoding
292+
- (void)encodeWithCoder:(NSCoder *)coder {
293+
[coder encodeInteger:self.status forKey:@"status"];
294+
[coder encodeObject:self.errorType forKey:@"errorType"];
295+
[coder encodeObject:self.errorMessage forKey:@"errorMessage"];
296+
[coder encodeObject:self.results forKey:@"results"];
297+
[coder encodeObject:self.responseObject forKey:@"responseObject"];
298+
[coder encodeObject:self.distinctId forKey:@"distinctId"];
299+
}
300+
301+
- (instancetype)initWithCoder:(NSCoder *)coder {
302+
self = [super init];
303+
if (self) {
304+
self.status = [coder decodeIntegerForKey:@"status"];
305+
self.errorType = [coder decodeObjectForKey:@"errorType"];
306+
self.errorMessage = [coder decodeObjectForKey:@"errorMessage"];
307+
self.results = [coder decodeObjectForKey:@"results"];
308+
self.responseObject = [coder decodeObjectForKey:@"responseObject"];
309+
self.distinctId = [coder decodeObjectForKey:@"distinctId"];
310+
}
311+
return self;
312+
}
313+
287314
@end
288315

SensorsABTest/DataManger/SABFileStore.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ NS_ASSUME_NONNULL_BEGIN
3535
/// @return 缓存数据
3636
+ (nullable id)unarchiveWithFileName:(NSString *)fileName;
3737

38-
/// 获取文件路径
38+
/// 删除文件
3939
/// @param fileName 文件名
40-
/// @return 文件路径
41-
+ (NSString *)filePath:(NSString *)fileName;
40+
/// @return YES/NO 是否删除成功
41+
+ (BOOL)deleteFileWithFileName:(NSString *)fileName;
4242

4343
@end
4444

SensorsABTest/DataManger/SABFileStore.m

+17
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ + (id)unarchiveFromFile:(NSString *)filePath {
6969
return unarchivedData;
7070
}
7171

72+
#pragma mark deleteFile
73+
+ (BOOL)deleteFileWithFileName:(NSString *)fileName {
74+
if (!fileName) {
75+
SABLogError(@"key should not be nil for file store");
76+
return NO;
77+
}
78+
79+
NSString *filePath = [SABFileStore filePath:fileName];
80+
@try {
81+
BOOL isDelete = [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
82+
return isDelete;
83+
} @catch (NSException *exception) {
84+
SABLogError(@"%@ unable to delete data in %@, starting fresh", self, filePath);
85+
}
86+
return NO;
87+
}
88+
7289
#pragma mark - file path
7390
+ (NSString *)filePath:(NSString *)key {
7491
NSString *filename = [NSString stringWithFormat:@"sensorsanalytics-abtest-%@.plist", key];

SensorsABTest/Network/SABRequest.h

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ NS_ASSUME_NONNULL_BEGIN
3838

3939
@property (nonatomic, copy) NSDictionary *body;
4040

41+
/// 更新用户标识信息
42+
- (void)refreshUserIdenty;
43+
4144
- (instancetype)initWithBaseURL:(NSURL *)url projectKey:(NSString *)key;
4245

4346
@end

SensorsABTest/Network/SABRequest.m

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ - (instancetype)initWithBaseURL:(NSURL *)url projectKey:(NSString *)key {
6767
return self;
6868
}
6969

70+
- (void)refreshUserIdenty {
71+
NSMutableDictionary *newBody = [self.body mutableCopy];
72+
newBody[@"login_id"] = [SABBridge loginId];
73+
newBody[@"anonymous_id"] = [SABBridge anonymousId];
74+
self.body = [newBody copy];
75+
}
76+
7077
- (void)setTimeoutInterval:(NSTimeInterval)timeoutInterval {
7178
// timeoutInterval 合法性校验
7279
if (timeoutInterval <= 0) {

SensorsABTest/SABConstants.h

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ extern NSString *const kSABIOSLibPrefix;
5050
extern NSTimeInterval const kSABFetchABTestResultDefaultTimeoutInterval;
5151

5252
#pragma mark - fileName
53+
/// 0.0.2 及之前版本试验缓存文件名
54+
extern NSString *const kSABExperimentResultOldFileName;
55+
5356
/// 试验缓存文件名
5457
extern NSString *const kSABExperimentResultFileName;
5558

SensorsABTest/SABConstants.m

+5-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#import "SABConstants.h"
2626

2727
// 当前版本号
28-
NSString *const kSABLibVersion = @"0.0.2";
28+
NSString *const kSABLibVersion = @"0.0.3";
2929

3030
// SA 最低支持版本
3131
NSString *const kSABMinSupportedSALibVersion = @"2.1.14";
@@ -50,7 +50,10 @@
5050
NSTimeInterval const kSABFetchABTestResultDefaultTimeoutInterval = 30;
5151

5252
#pragma mark - fileName
53-
NSString *const kSABExperimentResultFileName = @"SensorsABTestExperimentResuls";
53+
// TODO: v0.0.3 添加,老版本本地缓存文件名,下个版本移除
54+
NSString *const kSABExperimentResultOldFileName = @"SensorsABTestExperimentResuls";
55+
56+
NSString *const kSABExperimentResultFileName = @"SensorsABTestExperimentResultResponse";
5457

5558
#pragma mark - NSNotificationName
5659
#pragma mark H5 打通相关

0 commit comments

Comments
 (0)