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

code review

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