Commit 2d57d66e authored by 井庆林's avatar 井庆林

优化

parent 19039216
......@@ -66,15 +66,19 @@ NSString *const MockCityId = @"beijing";
// [Phobos track:@"page_view" attributes:dict];
// array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
for (int i = 0; i < 200; i++) {
[Phobos track:[NSString stringWithFormat:@"tt-%d", i] attributes:dict sendNow:YES];
[Phobos track:[NSString stringWithFormat:@"pv-%d", i] attributes:dict sendNow:(arc4random() % 2 == 0)];
}
for (int i = 5; i < 5; i++) {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[Phobos track:[NSString stringWithFormat:@"ay-%d", i] attributes:dict sendNow:YES];
});
}
// for (int i = 0; i < 200; i++) {
// [Phobos track:[NSString stringWithFormat:@"phobos>>>tt-%d", i] attributes:dict sendNow:YES];
// [Phobos track:[NSString stringWithFormat:@"phobos>>>pv-%d", i] attributes:dict sendNow:(arc4random() % 2 == 0)];
// NSUInteger count = [Phobos fetchToBeSendPhobosDataCount];
// NSLog(@"%lu", (unsigned long)count);
// }
// for (int i = 0; i < 200; i++) {
// dispatch_async(dispatch_get_global_queue(0, 0), ^{
// NSUInteger count = [Phobos fetchToBeSendPhobosDataCount];
// NSLog(@"%lu", (unsigned long)count);
// [Phobos track:[NSString stringWithFormat:@"phobos>>>ay-%d", i] attributes:dict sendNow:YES];
// });
// }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
......
......@@ -32,8 +32,6 @@ static NewPhobos *_sharedClient;
@implementation NewPhobos
static dispatch_semaphore_t _phobos_semaphore;
+ (NewPhobos *)clientWithAppName:(NSString *)appName channelId:(NSString *)channelId{
NewPhobos.sharedClient.appName = appName;
NewPhobos.sharedClient.channelId = channelId;
......@@ -42,7 +40,6 @@ static dispatch_semaphore_t _phobos_semaphore;
- (instancetype)init {
if (self = [super init]) {
_phobos_semaphore = dispatch_semaphore_create(1);
_appName = @"";
_channelId = @"";
_logEnabled = NO;
......@@ -218,7 +215,7 @@ static dispatch_semaphore_t _phobos_semaphore;
@"uuid": [PhobosUtil deviceId],
@"build_version_release": [[UIDevice currentDevice] systemVersion],
};
[NewPhobos track:@"on_app_session_over" attributes:dict];
[NewPhobos trackSessionOverWithAttributes:dict];
[GMCache removeObjectAtDocumentPathWithkey:PhobosBeginTime];
//当前session结束之后,把id置为0
_serialId = 0;
......@@ -328,7 +325,6 @@ static dispatch_semaphore_t _phobos_semaphore;
[deviceParams setValue:[PhobosUtil getIPAddress:YES] forKey:@"ip"];
[deviceParams setValue:PhobosSafeString(_networkStatus) forKey:@"net_type"];
[deviceParams setValue:PhobosSafeString([PhobosUtil platform]) forKey:@"model"];
[deviceParams setValue:@(_isGray) forKey:@"isGray"];
[deviceParams setValue:PhobosSafeString([UIDevice currentDevice].systemVersion) forKey:@"sys_version"];
NSMutableDictionary *appParams = [NSMutableDictionary new];
......@@ -337,6 +333,7 @@ static dispatch_semaphore_t _phobos_semaphore;
[appParams setValue:PhobosSafeString(_appVersion) forKey:@"version"];
[appParams setValue:PhobosSafeString(_channelId) forKey:@"channel"];
[appParams setValue:PhobosSafeString(_userType) forKey:@"user_type"];
[appParams setValue:@(_isGray) forKey:@"is_gray"];
[appParams setValue:PhobosSafeString(_currentCityId) forKey:@"current_city_id"];
[appParams setValue:@(_serialId++) forKey:@"serial_id"];
......@@ -381,11 +378,11 @@ static dispatch_semaphore_t _phobos_semaphore;
}
+ (void)onClickButtonWithAttributes:(NSDictionary *)attributes {
[self track:@"on_cick_button" attributes:attributes];
[self track:@"on_click_button" attributes:attributes];
}
+ (void)onClickButtonWithAttributes:(NSDictionary *)attributes sendNow:(BOOL)sendNow {
[self track:@"on_cick_button" attributes:attributes sendNow:sendNow];
[self track:@"on_click_button" attributes:attributes sendNow:sendNow];
}
+ (void)track:(NSString *)eventName{
......@@ -412,25 +409,34 @@ static dispatch_semaphore_t _phobos_semaphore;
NSDictionary *dataDict = [_sharedClient prepareDictionaryForEvent:eventName attributes:attributes];
@try {
NSData *JSON = [PhobosUtil encodeJSON:dataDict];
[PhobosDataManager insertData:dataDict sendAPI:currentAPI];
[self disposeSendDataWithImmediately:sendNow];
[PhobosDataManager insertData:dataDict sendAPI:currentAPI completion:^(BOOL contextDidSave, NSError * _Nullable error) {
[self disposeSendDataWithImmediately:sendNow];
}];
} @catch (NSException *exception) {
NSAssert(NO, @"哎呀呀,VALUE不能为NSObject ");
}
}
/**
* 为app_session_over提供埋点发送
* 其他情况在写入数据库成功,异步回调后在发送数据
* 这样可以优化发送次数,并能保障数据安全,但因为completion是使用主线程回调的,但进入后台后,主线程会休眠,所以单独处理该埋点
*/
+ (void)trackSessionOverWithAttributes:(NSDictionary *)attributes {
NSDictionary *dataDict = [_sharedClient prepareDictionaryForEvent:@"on_app_session_over" attributes:attributes];
[PhobosDataManager insertData:dataDict sendAPI:_sharedClient.serverAPI completion:nil];
[self disposeSendDataWithImmediately:YES];
}
/**
* 处理发送数据
*/
+ (void)disposeSendDataWithImmediately:(BOOL)immediately {
NSInteger count = [PhobosDataManager fetchCountOfToBeSendEntities];
if (immediately || count >= PhobosShardCount) {
dispatch_semaphore_wait(_phobos_semaphore, DISPATCH_TIME_FOREVER);
NSArray<PhobosSendDataEntity *> *entities = [PhobosDataManager fetchToBeSendDataEntities];
[PhobosDataManager updateDataEntities:entities sendStatus:PhobosDataSendStatusSending];
dispatch_semaphore_signal(_phobos_semaphore);
NSArray<PhobosSendDataEntity *> *entities = [PhobosDataManager fetchToBeSendDataEntitiesAndUpdateWithSendStatus:PhobosDataSendStatusSending];
[PhobosSendManager sendDataWithEntities:entities completion:^(NSArray<PhobosSendDataEntity *> * _Nonnull finishEntities, NSInteger code) {
[PhobosDataManager updateDataEntities:finishEntities sendStatus:(code == 200 ? PhobosDataSendStatusFinish : PhobosDataSendStatusError)];
[PhobosDataManager updateDataEntities:finishEntities sendStatus:(code == 200 ? PhobosDataSendStatusFinish : PhobosDataSendStatusError) completion:nil];
}];
}
}
......
......@@ -65,7 +65,7 @@ static BOOL isGray = NO;
if (isGray) {
[NewPhobos onClickButtonWithAttributes:attributes];
} else {
[self track:@"on_cick_button" attributes:attributes];
[self track:@"on_click_button" attributes:attributes];
}
}
......@@ -73,7 +73,7 @@ static BOOL isGray = NO;
if (isGray) {
[NewPhobos onClickButtonWithAttributes:attributes sendNow:sendNow];
} else {
[self track:@"on_cick_button" attributes:attributes sendNow:sendNow];
[self track:@"on_click_button" attributes:attributes sendNow:sendNow];
}
}
......
......@@ -20,23 +20,22 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
@interface PhobosDataManager : NSObject
/** 获取待发送数据,包含待发送数据和发送失败数据 */
+ (NSArray<PhobosSendDataEntity *> *)fetchToBeSendDataEntities;
/**
* 通过 searchFilter 获取数据
* 获取待发送和发送失败的数据数量
*/
+ (NSArray<PhobosSendDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter;
+ (NSUInteger)fetchCountOfToBeSendEntities;
/**
* 获取待发送和发送失败的数据数量
* 获取待发送数据,包含待发送数据和发送失败数据
* predicate 通过谓词获取相应的数据
*/
+ (NSUInteger)fetchCountOfToBeSendEntities;
+ (NSArray<PhobosSendDataEntity *> *)fetchToBeSendDataEntities;
+ (NSArray<PhobosSendDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)predicate;
/**
* 通过 searchFilter 获取数据数量
* 获取待发送数据,包含待发送数据和发送失败数据, 并修改状态为sendStatus
*/
+ (NSUInteger)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter;
+ (NSArray<PhobosSendDataEntity *> *)fetchToBeSendDataEntitiesAndUpdateWithSendStatus:(PhobosDataSendStatus)sendStatus;
/**
* 插入埋点数据
......@@ -45,7 +44,6 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
* completion 插入数据库成功后的回调方法,在保存完成后调用的完成块。如果发生错误,块将以“BOOL”和“NSError”实例的形式传递成功状态。总是在主队列上调用。
* 该方法调用完成,fetch方法获取相应的数据就可获取到,不需要等completion回调。
*/
+ (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI;
+ (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI completion:(MRSaveCompletionHandler)completion;
/**
......@@ -55,13 +53,12 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
* completion 插入数据库成功后的回调方法,在保存完成后调用的完成块。如果发生错误,块将以“BOOL”和“NSError”实例的形式传递成功状态。总是在主队列上调用。
* 该方法调用完成,fetch方法获取相应的数据就可获取到,不需要等completion回调。
*/
+ (void)updateDataEntities:(NSArray<PhobosSendDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus;
+ (void)updateDataEntities:(NSArray<PhobosSendDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus completion:(MRSaveCompletionHandler)completion;
/**
* 删除数据
* 删除所有数据
* 仅限单元测试使用
*/
+ (void)deleteDataEntities:(NSArray<PhobosSendDataEntity *> *)entities;
+ (void)deleteAllEntities;
@end
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment