Commit 7e9ee617 authored by 井庆林's avatar 井庆林

优化

parent ab34ff42
...@@ -66,7 +66,7 @@ NSString *const MockCityId = @"beijing"; ...@@ -66,7 +66,7 @@ NSString *const MockCityId = @"beijing";
// [Phobos track:@"page_view" attributes:dict]; // [Phobos track:@"page_view" attributes:dict];
// array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey]; // array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
for (int i = 0; i < 200; i++) { for (int i = 0; i < 10; i++) {
[Phobos track:[NSString stringWithFormat:@"tt-%d", i] attributes:dict sendNow:YES]; [Phobos track:[NSString stringWithFormat:@"tt-%d", i] attributes:dict sendNow:YES];
[Phobos track:[NSString stringWithFormat:@"pv-%d", i] attributes:dict sendNow:YES]; [Phobos track:[NSString stringWithFormat:@"pv-%d", i] attributes:dict sendNow:YES];
} }
......
...@@ -32,6 +32,8 @@ static NewPhobos *_sharedClient; ...@@ -32,6 +32,8 @@ static NewPhobos *_sharedClient;
@implementation NewPhobos @implementation NewPhobos
static dispatch_semaphore_t _phobos_semaphore;
+ (NewPhobos *)clientWithAppName:(NSString *)appName channelId:(NSString *)channelId{ + (NewPhobos *)clientWithAppName:(NSString *)appName channelId:(NSString *)channelId{
NewPhobos.sharedClient.appName = appName; NewPhobos.sharedClient.appName = appName;
NewPhobos.sharedClient.channelId = channelId; NewPhobos.sharedClient.channelId = channelId;
...@@ -40,6 +42,7 @@ static NewPhobos *_sharedClient; ...@@ -40,6 +42,7 @@ static NewPhobos *_sharedClient;
- (instancetype)init { - (instancetype)init {
if (self = [super init]) { if (self = [super init]) {
_phobos_semaphore = dispatch_semaphore_create(1);
_appName = @""; _appName = @"";
_channelId = @""; _channelId = @"";
_logEnabled = NO; _logEnabled = NO;
...@@ -422,12 +425,13 @@ static NewPhobos *_sharedClient; ...@@ -422,12 +425,13 @@ static NewPhobos *_sharedClient;
+ (void)disposeSendDataWithImmediately:(BOOL)immediately { + (void)disposeSendDataWithImmediately:(BOOL)immediately {
NSInteger count = [PhobosDataManager fetchCountOfToBeSendEntities]; NSInteger count = [PhobosDataManager fetchCountOfToBeSendEntities];
if (immediately || count >= PhobosShardCount) { if (immediately || count >= PhobosShardCount) {
[PhobosDataManager fetchToBeSendDataEntitiesWithEntitiesBlock:^(NSArray<PhobosSendDataEntity *> *entities) { dispatch_semaphore_wait(_phobos_semaphore, DISPATCH_TIME_FOREVER);
NSArray<PhobosSendDataEntity *> *entities = [PhobosDataManager fetchToBeSendDataEntities];
[PhobosDataManager updateDataEntities:entities sendStatus:PhobosDataSendStatusSending]; [PhobosDataManager updateDataEntities:entities sendStatus:PhobosDataSendStatusSending];
dispatch_semaphore_signal(_phobos_semaphore);
[PhobosSendManager sendDataWithEntities:entities completion:^(NSArray<PhobosSendDataEntity *> * _Nonnull finishEntities, NSInteger code) { [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)];
}]; }];
}];
} }
} }
...@@ -442,14 +446,7 @@ static NewPhobos *_sharedClient; ...@@ -442,14 +446,7 @@ static NewPhobos *_sharedClient;
/** 获取待发送埋点数据, 用同步来保障异步获取数据 */ /** 获取待发送埋点数据, 用同步来保障异步获取数据 */
+ (NSArray *)fetchToBeSendPhobosData { + (NSArray *)fetchToBeSendPhobosData {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); return [PhobosDataManager fetchToBeSendDataEntities];
__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
/** 获取待发送数据,包含待发送数据和发送失败数据 */ /** 获取待发送数据,包含待发送数据和发送失败数据 */
+ (void)fetchToBeSendDataEntitiesWithEntitiesBlock:(nonnull void (^)(NSArray<PhobosSendDataEntity *> *entities))entitiesBlock; + (NSArray<PhobosSendDataEntity *> *)fetchToBeSendDataEntities;
/** /**
* 通过 searchFilter 获取数据 * 通过 searchFilter 获取数据
*/ */
+ (void)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter entitiesBlock:(nonnull void (^)(NSArray<PhobosSendDataEntity *> *entities))entitiesBlock; + (NSArray<PhobosSendDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter;
/** /**
* 获取待发送和发送失败的数据数量 * 获取待发送和发送失败的数据数量
......
...@@ -15,15 +15,12 @@ ...@@ -15,15 +15,12 @@
@implementation PhobosDataManager @implementation PhobosDataManager
static NSManagedObjectContext *PhobosDefaultContext; static NSManagedObjectContext *PhobosDefaultContext;
static dispatch_queue_t PhobosDataSaveQueue;
static dispatch_semaphore_t phobos_semaphore_t; static dispatch_semaphore_t phobos_semaphore_t;
static dispatch_semaphore_t phobos_rd_semaphore_t;
+ (void)initialize { + (void)initialize {
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
phobos_semaphore_t = dispatch_semaphore_create(1); phobos_semaphore_t = dispatch_semaphore_create(1);
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];
...@@ -46,30 +43,23 @@ static dispatch_semaphore_t phobos_rd_semaphore_t; ...@@ -46,30 +43,23 @@ static dispatch_semaphore_t phobos_rd_semaphore_t;
/** 将上次没有获取到发送结果的数据的状态修改为发送失败,待下次重新发送 */ /** 将上次没有获取到发送结果的数据的状态修改为发送失败,待下次重新发送 */
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusSending]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusSending];
[self fetchDataEntitiesWithPredicate:predicate entitiesBlock:^(NSArray<PhobosSendDataEntity *> *entities) { NSArray<PhobosSendDataEntity *> *entities = [self fetchDataEntitiesWithPredicate:predicate];
[self updateDataEntities:entities sendStatus:PhobosDataSendStatusError]; [self updateDataEntities:entities sendStatus:PhobosDataSendStatusError];
}];
/** 将发送成功的数据删除 */ /** 将发送成功的数据删除 */
NSPredicate *finishPredicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusFinish]; NSPredicate *finishPredicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusFinish];
[self fetchDataEntitiesWithPredicate:finishPredicate entitiesBlock:^(NSArray<PhobosSendDataEntity *> *entities) { NSArray<PhobosSendDataEntity *> *finishEntities = [self fetchDataEntitiesWithPredicate:finishPredicate];
[self deleteDataEntities:entities]; [self deleteDataEntities:finishEntities];
}];
}); });
} }
+ (void)fetchToBeSendDataEntitiesWithEntitiesBlock:(void (^)(NSArray<PhobosSendDataEntity *> *))entitiesBlock { + (NSArray<PhobosSendDataEntity *> *)fetchToBeSendDataEntities {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d or status = %d", PhobosDataSendStatusToBeSend, PhobosDataSendStatusError]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d or status = %d", PhobosDataSendStatusToBeSend, PhobosDataSendStatusError];
[self fetchDataEntitiesWithPredicate:predicate entitiesBlock:entitiesBlock]; return [self fetchDataEntitiesWithPredicate:predicate];
} }
+ (void)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter entitiesBlock:(nonnull void (^)(NSArray<PhobosSendDataEntity *> *))entitiesBlock { + (NSArray<PhobosSendDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter {
dispatch_semaphore_wait(phobos_semaphore_t, DISPATCH_TIME_FOREVER); return [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);
} }
+ (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