Commit 6b1ba75f authored by 井庆林's avatar 井庆林

code review

parent 2c1f9f3d
...@@ -66,20 +66,12 @@ NSString *const MockCityId = @"beijing"; ...@@ -66,20 +66,12 @@ NSString *const MockCityId = @"beijing";
// [Phobos track:@"page_view" attributes:dict]; // [Phobos track:@"page_view" attributes:dict];
// array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey]; // array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
[dict setObject:[PhobosUtil currentTime] forKey:@"in"]; for (int i = 0; i < 10; i++) {
[dict setObject:[PhobosUtil currentTime] forKey:@"out"]; [Phobos track:[NSString stringWithFormat:@"test-%d", i] attributes:dict sendNow:YES];
[Phobos track:@"page_view" attributes:dict sendNow:YES]; // dispatch_async(dispatch_get_global_queue(0, 0), ^{
// array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey]; // [Phobos track:[NSString stringWithFormat:@"page_view-%d", i] attributes:dict];// sendNow:(i % 2) == 0];
// });
[Phobos track:@"test" attributes:dict sendNow:YES]; }
[Phobos track:@"test2" attributes:dict sendNow:YES];
[Phobos track:@"test3" attributes:dict sendNow:YES];
[Phobos track:@"test4" attributes:dict sendNow:YES];
[Phobos track:@"test5" attributes:dict sendNow:YES];
[Phobos track:@"test6" attributes:dict sendNow:YES];
[Phobos track:@"test7" attributes:dict sendNow:YES];
[Phobos track:@"test8" attributes:dict sendNow:YES];
// array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
} }
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
......
PODS: PODS:
- GMCache (1.0.1): - GMCache (1.0.1):
- TMCache (= 2.1.0) - TMCache (= 2.1.0)
- GMPhobos (2.0.0): - GMPhobos (2.0.3):
- GMCache - GMCache
- MagicalRecord - MagicalRecord
- MJExtension - MJExtension
...@@ -28,11 +28,11 @@ EXTERNAL SOURCES: ...@@ -28,11 +28,11 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS: SPEC CHECKSUMS:
GMCache: b78d8e46db864405e91d226ce640cc80d966c611 GMCache: b78d8e46db864405e91d226ce640cc80d966c611
GMPhobos: 3cad92cfa28da2b719ccef040a8fb4e33012b1ad GMPhobos: 6bd14d7b98841b6a3e35c5aa8b1f091ad07bbbf7
MagicalRecord: 53bed74b4323b930992a725be713e53b37d19755 MagicalRecord: 53bed74b4323b930992a725be713e53b37d19755
MJExtension: 635f2c663dcb1bf76fa4b715b2570a5710aec545 MJExtension: 635f2c663dcb1bf76fa4b715b2570a5710aec545
TMCache: 95ebcc9b3c7e90fb5fd8fc3036cba3aa781c9bed TMCache: 95ebcc9b3c7e90fb5fd8fc3036cba3aa781c9bed
PODFILE CHECKSUM: ea0fac2144ac80baf8f21576cde49526c19991ad PODFILE CHECKSUM: ea0fac2144ac80baf8f21576cde49526c19991ad
COCOAPODS: 1.9.0 COCOAPODS: 1.9.1
...@@ -117,12 +117,14 @@ NSString *const MockCityId = @"beijing"; ...@@ -117,12 +117,14 @@ NSString *const MockCityId = @"beijing";
*/ */
- (void)testTrackEventWithAttrAndSendNowDonotClearNormal { - (void)testTrackEventWithAttrAndSendNowDonotClearNormal {
// Given // Given
[Phobos removeAllPhobosData];
NSDictionary *attr = @{@"attr":@"track_attr"}; NSDictionary *attr = @{@"attr":@"track_attr"};
NSDictionary *sendNowAttr = @{@"attr":@"track_attr_send_now"}; NSDictionary *sendNowAttr = @{@"attr":@"track_attr_send_now"};
// When // When
[Phobos track:MockEventId attributes:attr sendNow:NO]; [Phobos track:MockEventId attributes:attr sendNow:NO];
// When // When
[Phobos track:MockEventId attributes:sendNowAttr sendNow:YES]; [Phobos track:MockEventId attributes:sendNowAttr sendNow:YES];
XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 0, @"array should be empty"); XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 0, @"array should be empty");
} }
...@@ -132,6 +134,7 @@ NSString *const MockCityId = @"beijing"; ...@@ -132,6 +134,7 @@ NSString *const MockCityId = @"beijing";
* @since 1.1.4 * @since 1.1.4
*/ */
- (void)testTrackEventWithDoubleAttrAndSendNow { - (void)testTrackEventWithDoubleAttrAndSendNow {
[Phobos removeAllPhobosData];
NSDictionary *attr = @{@"attr":@"track_attr"}; NSDictionary *attr = @{@"attr":@"track_attr"};
[Phobos track:MockEventId attributes:attr sendNow:YES]; [Phobos track:MockEventId attributes:attr sendNow:YES];
...@@ -160,9 +163,9 @@ NSString *const MockCityId = @"beijing"; ...@@ -160,9 +163,9 @@ NSString *const MockCityId = @"beijing";
[Phobos track:MockEventId attributes:attr]; [Phobos track:MockEventId attributes:attr];
} }
XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 0, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr];
XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 1, @"array shouldn't be empty"); XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 1, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr];
XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 2, @"array shouldn't be empty");
} }
/** /**
...@@ -221,10 +224,10 @@ NSString *const MockCityId = @"beijing"; ...@@ -221,10 +224,10 @@ NSString *const MockCityId = @"beijing";
for (int i = 0; i < 50; i++) { for (int i = 0; i < 50; i++) {
[Phobos track:MockEventId attributes:attr]; [Phobos track:MockEventId attributes:attr];
} }
XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 0, @"array shouldn't be empty"); XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 1, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr]; [Phobos track:MockEventId attributes:attr];
XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 1, @"array shouldn't be empty"); XCTAssertTrue([Phobos fetchToBeSendPhobosDataCount] == 2, @"array shouldn't be empty");
} }
- (void)verfiyDict:(NSDictionary *)dict{ - (void)verfiyDict:(NSDictionary *)dict{
......
...@@ -420,18 +420,15 @@ static NewPhobos *_sharedClient; ...@@ -420,18 +420,15 @@ static NewPhobos *_sharedClient;
* 处理发送数据 * 处理发送数据
*/ */
+ (void)disposeSendDataWithImmediately:(BOOL)immediately { + (void)disposeSendDataWithImmediately:(BOOL)immediately {
NSArray<PhobosSendDataEntity *> *entities = [PhobosDataManager fetchToBeSendDataEntities]; [PhobosDataManager fetchToBeSendDataEntitiesWithEntitiesBlock:^(NSArray<PhobosSendDataEntity *> *entities) {
if (immediately || entities.count > PhobosShardCount) { if (immediately || entities.count >= PhobosShardCount) {
[PhobosDataManager updateDataEntities:entities sendStatus:PhobosDataSendStatusSending]; [PhobosDataManager updateDataEntities:entities sendStatus:PhobosDataSendStatusSending];
[PhobosSendManager sendDataWithEntities:entities completion:^(NSArray<PhobosSendDataEntity *> * _Nonnull finishEntities, NSInteger code) { [PhobosSendManager sendDataWithEntities:entities completion:^(NSArray<PhobosSendDataEntity *> * _Nonnull finishEntities, NSInteger code) {
if (code == 200) { [PhobosDataManager updateDataEntities:finishEntities sendStatus:(code == 200 ? PhobosDataSendStatusFinish : PhobosDataSendStatusError)];
[PhobosDataManager updateDataEntities:finishEntities sendStatus:PhobosDataSendStatusFinish];
[PhobosDataManager deleteDataEntities:finishEntities];
} else {
[PhobosDataManager updateDataEntities:finishEntities sendStatus:PhobosDataSendStatusError];
}
}]; }];
} }
}];
} }
@end @end
...@@ -440,12 +437,26 @@ static NewPhobos *_sharedClient; ...@@ -440,12 +437,26 @@ static NewPhobos *_sharedClient;
/** 获取所有非立即发送埋点数量 */ /** 获取所有非立即发送埋点数量 */
+ (NSUInteger)fetchToBeSendPhobosDataCount { + (NSUInteger)fetchToBeSendPhobosDataCount {
return [PhobosDataManager fetchCountOfToBeSendEntities]; dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block NSUInteger count = 0;
[PhobosDataManager fetchCountOfToBeSendEntitiesWithCountBlock:^(NSUInteger entitiesCount) {
count = entitiesCount;
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return count;
} }
/** 获取待发送埋点数据 */ /** 获取待发送埋点数据, 用同步来保障异步获取数据 */
+ (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,22 +21,22 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) { ...@@ -21,22 +21,22 @@ typedef NS_ENUM(NSInteger, PhobosDataSendStatus) {
@interface PhobosDataManager : NSObject @interface PhobosDataManager : NSObject
/** 获取待发送数据,包含待发送数据和发送失败数据 */ /** 获取待发送数据,包含待发送数据和发送失败数据 */
+ (NSArray<PhobosSendDataEntity *> *)fetchToBeSendDataEntities; + (void)fetchToBeSendDataEntitiesWithEntitiesBlock:(void (^)(NSArray<PhobosSendDataEntity *> *entities))entitiesBlock;
/** /**
* 通过 searchFilter 获取数据 * 通过 searchFilter 获取数据
*/ */
+ (NSArray<PhobosSendDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter; + (void)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter;
/** /**
* 获取待发送和发送失败的数据数量 * 获取待发送和发送失败的数据数量
*/ */
+ (NSUInteger)fetchCountOfToBeSendEntities; + (void)fetchCountOfToBeSendEntities;
/** /**
* 通过 searchFilter 获取数据数量 * 通过 searchFilter 获取数据数量
*/ */
+ (NSUInteger)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter; + (void)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter;
/** /**
* 插入埋点数据 * 插入埋点数据
......
...@@ -17,13 +17,13 @@ ...@@ -17,13 +17,13 @@
static NSManagedObjectContext *PhobosDefaultContext; static NSManagedObjectContext *PhobosDefaultContext;
static dispatch_queue_t PhobosDataSaveQueue; static dispatch_queue_t PhobosDataSaveQueue;
+ (void)load { + (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_queue_phobos", DISPATCH_QUEUE_SERIAL);
// 创建 NSManagedObjectContext,供埋点库访问CoreData库使用 // 创建 NSManagedObjectContext,供埋点库访问CoreData库使用
PhobosDefaultContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; PhobosDefaultContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
NSBundle *bundle = [NSBundle bundleForClass:[self class]]; NSBundle *bundle = [NSBundle bundleForClass:[self class]];
// 获取埋点数据的实体 // 获取埋点数据的实体
NSURL *modelURL = [bundle URLForResource:@"GMPhobosData" withExtension:@"momd"]; NSURL *modelURL = [bundle URLForResource:@"GMPhobosData" withExtension:@"momd"];
...@@ -43,32 +43,45 @@ static dispatch_queue_t PhobosDataSaveQueue; ...@@ -43,32 +43,45 @@ static dispatch_queue_t PhobosDataSaveQueue;
/** 将上次没有获取到发送结果的数据的状态修改为发送失败,待下次重新发送 */ /** 将上次没有获取到发送结果的数据的状态修改为发送失败,待下次重新发送 */
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];
NSArray<PhobosSendDataEntity *> *finishEntities = [self fetchDataEntitiesWithPredicate:finishPredicate]; [PhobosSendDataEntity MR_deleteAllMatchingPredicate:finishPredicate];
[self deleteDataEntities:finishEntities]; // [self fetchDataEntitiesWithPredicate:finishPredicate entitiesBlock:^(NSArray<PhobosSendDataEntity *> * finishEntities) {
// [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 {
return [PhobosSendDataEntity MR_findAllWithPredicate:searchFilter inContext:PhobosDefaultContext]; dispatch_async(PhobosDataSaveQueue, ^{
NSArray<PhobosSendDataEntity *> *entities = [PhobosSendDataEntity MR_findAllWithPredicate:searchFilter inContext:PhobosDefaultContext];
if (entitiesBlock) {
entitiesBlock(entities);
}
});
} }
+ (NSUInteger)fetchCountOfToBeSendEntities { + (void)fetchCountOfToBeSendEntitiesWithCountBlock:(nonnull void (^)(NSUInteger))countBlock {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d or status = %d", PhobosDataSendStatusToBeSend, PhobosDataSendStatusError]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d or status = %d", PhobosDataSendStatusToBeSend, PhobosDataSendStatusError];
return [self fetchCountOfEntitiesWithPredicate:predicate]; [self fetchCountOfEntitiesWithPredicate:predicate countBlock:countBlock];
} }
+ (NSUInteger)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter { + (void)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter countBlock:(nonnull void (^)(NSUInteger))countBlock {
return [[PhobosSendDataEntity MR_numberOfEntitiesWithPredicate:searchFilter inContext:PhobosDefaultContext] integerValue]; dispatch_async(PhobosDataSaveQueue, ^{
NSUInteger count = [[PhobosSendDataEntity MR_numberOfEntitiesWithPredicate:searchFilter inContext:PhobosDefaultContext] integerValue];
if (countBlock) {
countBlock(count);
}
});
} }
+ (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI { + (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI {
...@@ -79,12 +92,14 @@ static dispatch_queue_t PhobosDataSaveQueue; ...@@ -79,12 +92,14 @@ static dispatch_queue_t PhobosDataSaveQueue;
if (!sendAPI || [sendAPI isEqualToString:@""] || !data) { if (!sendAPI || [sendAPI isEqualToString:@""] || !data) {
return; return;
} }
dispatch_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;
entity.status = PhobosDataSendStatusToBeSend; entity.status = PhobosDataSendStatusToBeSend;
entity.id = mach_absolute_time(); entity.id = mach_absolute_time();
[self saveWithCompletion:completion]; [self saveWithCompletion:completion];
});
} }
+ (void)updateDataEntities:(NSArray<PhobosSendDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus { + (void)updateDataEntities:(NSArray<PhobosSendDataEntity *> *)entities sendStatus:(PhobosDataSendStatus)sendStatus {
...@@ -93,34 +108,44 @@ static dispatch_queue_t PhobosDataSaveQueue; ...@@ -93,34 +108,44 @@ 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, ^{
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;
}]; if (idx == count - 1) {
[self saveWithCompletion:completion]; [self saveWithCompletion:completion];
} }
}];
});
}
} }
+ (void)deleteDataEntities:(NSArray<PhobosSendDataEntity *> *)entities { + (void)deleteDataEntities:(NSArray<PhobosSendDataEntity *> *)entities {
if (entities.count > 0) { if (entities.count > 0) {
dispatch_async(PhobosDataSaveQueue, ^{
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];
}]; if (idx == count - 1) {
[self saveWithCompletion:nil]; [self saveWithCompletion:nil];
} }
}];
});
}
} }
+ (void)deleteAllEntities { + (void)deleteAllEntities {
dispatch_async(PhobosDataSaveQueue, ^{
[PhobosSendDataEntity MR_truncateAllInContext:PhobosDefaultContext]; [PhobosSendDataEntity MR_truncateAllInContext:PhobosDefaultContext];
[self saveWithCompletion:nil]; [self saveWithCompletion:nil];
});
} }
/** /**
* 在保存完成后调用的完成块。如果发生错误,块将以“BOOL”和“NSError”实例的形式传递成功状态。总是在主队列上调用。 * 在保存完成后调用的完成块。如果发生错误,块将以“BOOL”和“NSError”实例的形式传递成功状态。总是在主队列上调用。
*/ */
+ (void)saveWithCompletion:(MRSaveCompletionHandler)completion { + (void)saveWithCompletion:(MRSaveCompletionHandler)completion {
dispatch_async(PhobosDataSaveQueue, ^{
[PhobosDefaultContext MR_saveWithOptions:MRSaveSynchronously completion:completion]; [PhobosDefaultContext MR_saveWithOptions:MRSaveSynchronously completion:completion];
});
} }
@end @end
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