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

code review

parent 6b1ba75f
...@@ -68,9 +68,9 @@ NSString *const MockCityId = @"beijing"; ...@@ -68,9 +68,9 @@ NSString *const MockCityId = @"beijing";
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
[Phobos track:[NSString stringWithFormat:@"test-%d", i] attributes:dict sendNow:YES]; [Phobos track:[NSString stringWithFormat:@"test-%d", i] attributes:dict sendNow:YES];
// dispatch_async(dispatch_get_global_queue(0, 0), ^{ dispatch_async(dispatch_get_global_queue(0, 0), ^{
// [Phobos track:[NSString stringWithFormat:@"page_view-%d", i] attributes:dict];// sendNow:(i % 2) == 0]; [Phobos track:[NSString stringWithFormat:@"page_view-%d", i] attributes:dict];// sendNow:(i % 2) == 0];
// }); });
} }
} }
......
...@@ -26,17 +26,17 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) { ...@@ -26,17 +26,17 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
/** /**
* 通过 searchFilter 获取数据 * 通过 searchFilter 获取数据
*/ */
+ (void)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter; + (void)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter entitiesBlock:(void (^)(NSArray<PhobosSendDataEntity *> *entities))entitiesBlock;
/** /**
* 获取待发送和发送失败的数据数量 * 获取待发送和发送失败的数据数量
*/ */
+ (void)fetchCountOfToBeSendEntities; + (void)fetchCountOfToBeSendEntitiesWithCountBlock:(nonnull void (^)(NSUInteger count))countBlock;
/** /**
* 通过 searchFilter 获取数据数量 * 通过 searchFilter 获取数据数量
*/ */
+ (void)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter; + (void)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter countBlock:(nonnull void (^)(NSUInteger count))countBlock;
/** /**
* 插入埋点数据 * 插入埋点数据
......
...@@ -20,7 +20,7 @@ static dispatch_queue_t PhobosDataSaveQueue; ...@@ -20,7 +20,7 @@ static dispatch_queue_t PhobosDataSaveQueue;
+ (void)initialize { + (void)initialize {
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
PhobosDataSaveQueue = dispatch_queue_create("dispatch_queue_phobos", DISPATCH_QUEUE_SERIAL); PhobosDataSaveQueue = dispatch_queue_create("dispatch_rw_queue_phobos", DISPATCH_QUEUE_CONCURRENT);
// 创建 NSManagedObjectContext,供埋点库访问CoreData库使用 // 创建 NSManagedObjectContext,供埋点库访问CoreData库使用
PhobosDefaultContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; PhobosDefaultContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
...@@ -49,10 +49,10 @@ static dispatch_queue_t PhobosDataSaveQueue; ...@@ -49,10 +49,10 @@ static dispatch_queue_t PhobosDataSaveQueue;
/** 将发送成功的数据删除 */ /** 将发送成功的数据删除 */
NSPredicate *finishPredicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusFinish]; NSPredicate *finishPredicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusFinish];
[PhobosSendDataEntity MR_deleteAllMatchingPredicate:finishPredicate]; // [PhobosSendDataEntity MR_deleteAllMatchingPredicate:finishPredicate];
// [self fetchDataEntitiesWithPredicate:finishPredicate entitiesBlock:^(NSArray<PhobosSendDataEntity *> * finishEntities) { [self fetchDataEntitiesWithPredicate:finishPredicate entitiesBlock:^(NSArray<PhobosSendDataEntity *> * finishEntities) {
// [self deleteDataEntities:finishEntities]; [self deleteDataEntities:finishEntities];
// }]; }];
}); });
} }
...@@ -62,7 +62,7 @@ static dispatch_queue_t PhobosDataSaveQueue; ...@@ -62,7 +62,7 @@ static dispatch_queue_t PhobosDataSaveQueue;
} }
+ (void)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter entitiesBlock:(nonnull void (^)(NSArray<PhobosSendDataEntity *> *))entitiesBlock { + (void)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter entitiesBlock:(nonnull void (^)(NSArray<PhobosSendDataEntity *> *))entitiesBlock {
dispatch_async(PhobosDataSaveQueue, ^{ dispatch_sync(PhobosDataSaveQueue, ^{
NSArray<PhobosSendDataEntity *> *entities = [PhobosSendDataEntity MR_findAllWithPredicate:searchFilter inContext:PhobosDefaultContext]; NSArray<PhobosSendDataEntity *> *entities = [PhobosSendDataEntity MR_findAllWithPredicate:searchFilter inContext:PhobosDefaultContext];
if (entitiesBlock) { if (entitiesBlock) {
entitiesBlock(entities); entitiesBlock(entities);
...@@ -76,7 +76,7 @@ static dispatch_queue_t PhobosDataSaveQueue; ...@@ -76,7 +76,7 @@ static dispatch_queue_t PhobosDataSaveQueue;
} }
+ (void)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter countBlock:(nonnull void (^)(NSUInteger))countBlock { + (void)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter countBlock:(nonnull void (^)(NSUInteger))countBlock {
dispatch_async(PhobosDataSaveQueue, ^{ dispatch_sync(PhobosDataSaveQueue, ^{
NSUInteger count = [[PhobosSendDataEntity MR_numberOfEntitiesWithPredicate:searchFilter inContext:PhobosDefaultContext] integerValue]; NSUInteger count = [[PhobosSendDataEntity MR_numberOfEntitiesWithPredicate:searchFilter inContext:PhobosDefaultContext] integerValue];
if (countBlock) { if (countBlock) {
countBlock(count); countBlock(count);
...@@ -92,7 +92,7 @@ static dispatch_queue_t PhobosDataSaveQueue; ...@@ -92,7 +92,7 @@ static dispatch_queue_t PhobosDataSaveQueue;
if (!sendAPI || [sendAPI isEqualToString:@""] || !data) { if (!sendAPI || [sendAPI isEqualToString:@""] || !data) {
return; return;
} }
dispatch_async(PhobosDataSaveQueue, ^{ dispatch_barrier_async(PhobosDataSaveQueue, ^{
PhobosSendDataEntity *entity = [PhobosSendDataEntity MR_createEntityInContext:PhobosDefaultContext]; PhobosSendDataEntity *entity = [PhobosSendDataEntity MR_createEntityInContext:PhobosDefaultContext];
entity.data = [data mj_JSONData]; entity.data = [data mj_JSONData];
entity.api = sendAPI; entity.api = sendAPI;
...@@ -108,7 +108,7 @@ static dispatch_queue_t PhobosDataSaveQueue; ...@@ -108,7 +108,7 @@ static dispatch_queue_t PhobosDataSaveQueue;
+ (void)updateDataEntities:(NSArray<PhobosSendDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus completion:(MRSaveCompletionHandler)completion { + (void)updateDataEntities:(NSArray<PhobosSendDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus completion:(MRSaveCompletionHandler)completion {
if (entities.count > 0) { if (entities.count > 0) {
dispatch_async(PhobosDataSaveQueue, ^{ dispatch_barrier_async(PhobosDataSaveQueue, ^{
NSInteger count = entities.count; NSInteger count = entities.count;
[entities enumerateObjectsUsingBlock:^(PhobosSendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) { [entities enumerateObjectsUsingBlock:^(PhobosSendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.status = sendStatus; obj.status = sendStatus;
...@@ -122,7 +122,7 @@ static dispatch_queue_t PhobosDataSaveQueue; ...@@ -122,7 +122,7 @@ static dispatch_queue_t PhobosDataSaveQueue;
+ (void)deleteDataEntities:(NSArray<PhobosSendDataEntity *> *)entities { + (void)deleteDataEntities:(NSArray<PhobosSendDataEntity *> *)entities {
if (entities.count > 0) { if (entities.count > 0) {
dispatch_async(PhobosDataSaveQueue, ^{ dispatch_barrier_async(PhobosDataSaveQueue, ^{
NSInteger count = entities.count; NSInteger count = entities.count;
[entities enumerateObjectsUsingBlock:^(PhobosSendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) { [entities enumerateObjectsUsingBlock:^(PhobosSendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj MR_deleteEntityInContext:PhobosDefaultContext]; [obj MR_deleteEntityInContext:PhobosDefaultContext];
...@@ -135,7 +135,7 @@ static dispatch_queue_t PhobosDataSaveQueue; ...@@ -135,7 +135,7 @@ static dispatch_queue_t PhobosDataSaveQueue;
} }
+ (void)deleteAllEntities { + (void)deleteAllEntities {
dispatch_async(PhobosDataSaveQueue, ^{ dispatch_barrier_async(PhobosDataSaveQueue, ^{
[PhobosSendDataEntity MR_truncateAllInContext:PhobosDefaultContext]; [PhobosSendDataEntity MR_truncateAllInContext:PhobosDefaultContext];
[self saveWithCompletion:nil]; [self saveWithCompletion:nil];
}); });
......
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