Commit ab34ff42 authored by 井庆林's avatar 井庆林

code review

parent 34057f67
...@@ -420,14 +420,15 @@ static NewPhobos *_sharedClient; ...@@ -420,14 +420,15 @@ static NewPhobos *_sharedClient;
* 处理发送数据 * 处理发送数据
*/ */
+ (void)disposeSendDataWithImmediately:(BOOL)immediately { + (void)disposeSendDataWithImmediately:(BOOL)immediately {
NSArray<PhobosSendDataEntity *> *entities = [PhobosDataManager fetchToBeSendDataEntities]; NSInteger count = [PhobosDataManager fetchCountOfToBeSendEntities];
if (immediately || entities.count >= PhobosShardCount) { if (immediately || count >= PhobosShardCount) {
[PhobosDataManager updateDataEntities:entities sendStatus:PhobosDataSendStatusSending]; [PhobosDataManager fetchToBeSendDataEntitiesWithEntitiesBlock:^(NSArray<PhobosSendDataEntity *> *entities) {
[PhobosSendManager sendDataWithEntities:entities completion:^(NSArray<PhobosSendDataEntity *> * _Nonnull finishEntities, NSInteger code) { [PhobosDataManager updateDataEntities:entities sendStatus:PhobosDataSendStatusSending];
[PhobosDataManager updateDataEntities:finishEntities sendStatus:(code == 200 ? PhobosDataSendStatusFinish : PhobosDataSendStatusError)]; [PhobosSendManager sendDataWithEntities:entities completion:^(NSArray<PhobosSendDataEntity *> * _Nonnull finishEntities, NSInteger code) {
[PhobosDataManager updateDataEntities:finishEntities sendStatus:(code == 200 ? PhobosDataSendStatusFinish : PhobosDataSendStatusError)];
}];
}]; }];
} }
} }
@end @end
...@@ -441,7 +442,14 @@ static NewPhobos *_sharedClient; ...@@ -441,7 +442,14 @@ static NewPhobos *_sharedClient;
/** 获取待发送埋点数据, 用同步来保障异步获取数据 */ /** 获取待发送埋点数据, 用同步来保障异步获取数据 */
+ (NSArray *)fetchToBeSendPhobosData { + (NSArray *)fetchToBeSendPhobosData {
return [PhobosDataManager fetchToBeSendDataEntities]; dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block NSArray<PhobosSendDataEntity *> *entityArray;
[PhobosDataManager fetchToBeSendDataEntitiesWithEntitiesBlock:^(NSArray<PhobosSendDataEntity *> *entities) {
entityArray = entities;
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return entityArray;
} }
/** 清除待发送埋点数据缓存 */ /** 清除待发送埋点数据缓存 */
......
...@@ -21,12 +21,12 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) { ...@@ -21,12 +21,12 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
@interface PhobosDataManager : NSObject @interface PhobosDataManager : NSObject
/** 获取待发送数据,包含待发送数据和发送失败数据 */ /** 获取待发送数据,包含待发送数据和发送失败数据 */
+ (NSArray<PhobosSendDataEntity *> *)fetchToBeSendDataEntities; + (void)fetchToBeSendDataEntitiesWithEntitiesBlock:(nonnull void (^)(NSArray<PhobosSendDataEntity *> *entities))entitiesBlock;
/** /**
* 通过 searchFilter 获取数据 * 通过 searchFilter 获取数据
*/ */
+ (NSArray<PhobosSendDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter; + (void)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter entitiesBlock:(nonnull void (^)(NSArray<PhobosSendDataEntity *> *entities))entitiesBlock;
/** /**
* 获取待发送和发送失败的数据数量 * 获取待发送和发送失败的数据数量
...@@ -36,7 +36,7 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) { ...@@ -36,7 +36,7 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
/** /**
* 通过 searchFilter 获取数据数量 * 通过 searchFilter 获取数据数量
*/ */
+ (NSUInteger)fetchCountOfEntities; + (NSUInteger)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter;
/** /**
* 插入埋点数据 * 插入埋点数据
......
...@@ -46,27 +46,30 @@ static dispatch_semaphore_t phobos_rd_semaphore_t; ...@@ -46,27 +46,30 @@ static dispatch_semaphore_t phobos_rd_semaphore_t;
/** 将上次没有获取到发送结果的数据的状态修改为发送失败,待下次重新发送 */ /** 将上次没有获取到发送结果的数据的状态修改为发送失败,待下次重新发送 */
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusSending]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusSending];
NSArray<PhobosSendDataEntity *> *entities = [self fetchDataEntitiesWithPredicate:predicate]; [self fetchDataEntitiesWithPredicate:predicate entitiesBlock:^(NSArray<PhobosSendDataEntity *> *entities) {
[self updateDataEntities:entities sendStatus:PhobosDataSendStatusError]; [self updateDataEntities:entities sendStatus:PhobosDataSendStatusError];
}];
/** 将发送成功的数据删除 */ /** 将发送成功的数据删除 */
NSPredicate *finishPredicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusFinish]; NSPredicate *finishPredicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusFinish];
// [PhobosSendDataEntity MR_deleteAllMatchingPredicate:finishPredicate]; [self fetchDataEntitiesWithPredicate:finishPredicate entitiesBlock:^(NSArray<PhobosSendDataEntity *> *entities) {
NSArray<PhobosSendDataEntity *> *finishEntities = [self fetchDataEntitiesWithPredicate:finishPredicate]; [self deleteDataEntities:entities];
[self deleteDataEntities:finishEntities]; }];
}); });
} }
+ (NSArray<PhobosSendDataEntity *> *)fetchToBeSendDataEntities { + (void)fetchToBeSendDataEntitiesWithEntitiesBlock:(void (^)(NSArray<PhobosSendDataEntity *> *))entitiesBlock {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d or status = %d", PhobosDataSendStatusToBeSend, PhobosDataSendStatusError]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d or status = %d", PhobosDataSendStatusToBeSend, PhobosDataSendStatusError];
return [self fetchDataEntitiesWithPredicate:predicate]; [self fetchDataEntitiesWithPredicate:predicate entitiesBlock:entitiesBlock];
} }
+ (NSArray<PhobosSendDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter { + (void)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter entitiesBlock:(nonnull void (^)(NSArray<PhobosSendDataEntity *> *))entitiesBlock {
dispatch_semaphore_wait(phobos_semaphore_t, DISPATCH_TIME_FOREVER); dispatch_semaphore_wait(phobos_semaphore_t, DISPATCH_TIME_FOREVER);
NSArray<PhobosSendDataEntity *> *entities = [PhobosSendDataEntity MR_findAllWithPredicate:searchFilter inContext:PhobosDefaultContext]; NSArray<PhobosSendDataEntity *> *entities = [PhobosSendDataEntity MR_findAllWithPredicate:searchFilter inContext:PhobosDefaultContext];
if (entitiesBlock) {
entitiesBlock(entities);
}
dispatch_semaphore_signal(phobos_semaphore_t); dispatch_semaphore_signal(phobos_semaphore_t);
return entities;
} }
+ (NSUInteger)fetchCountOfToBeSendEntities { + (NSUInteger)fetchCountOfToBeSendEntities {
......
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