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

code review

parent 331224cf
......@@ -14,13 +14,16 @@
@implementation PhobosDataManager
static NSManagedObjectContext *Phobos_defaultContext;
static NSManagedObjectContext *PhobosDefaultContext;
static dispatch_queue_t PhobosDataSaveQueue;
+ (void)initialize {
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
PhobosDataSaveQueue = dispatch_queue_create("dispatch_queue_phobos", DISPATCH_QUEUE_SERIAL);
// 创建 NSManagedObjectContext,供埋点库访问CoreData库使用
Phobos_defaultContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
PhobosDefaultContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
// 获取埋点数据的实体
NSURL *modelURL = [bundle URLForResource:@"GMPhobosData" withExtension:@"momd"];
......@@ -36,7 +39,7 @@ static NSManagedObjectContext *Phobos_defaultContext;
[coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:dataPath] options:nil error:nil];
// 设置storeCoordinator
Phobos_defaultContext.persistentStoreCoordinator = coordinator;
PhobosDefaultContext.persistentStoreCoordinator = coordinator;
/** 将上次没有获取到发送结果的数据的状态修改为发送失败,待下次重新发送 */
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d", PhobosDataSendStatusSending];
......@@ -56,7 +59,7 @@ static NSManagedObjectContext *Phobos_defaultContext;
}
+ (NSArray<PhobosSendDataEntity *> *)fetchDataEntitiesWithPredicate:(NSPredicate *)searchFilter {
return [PhobosSendDataEntity MR_findAllWithPredicate:searchFilter inContext:Phobos_defaultContext];
return [PhobosSendDataEntity MR_findAllWithPredicate:searchFilter inContext:PhobosDefaultContext];
}
+ (NSUInteger)fetchCountOfToBeSendEntities {
......@@ -65,7 +68,7 @@ static NSManagedObjectContext *Phobos_defaultContext;
}
+ (NSUInteger)fetchCountOfEntitiesWithPredicate:(NSPredicate *)searchFilter {
return [[PhobosSendDataEntity MR_numberOfEntitiesWithPredicate:searchFilter inContext:Phobos_defaultContext] integerValue];
return [[PhobosSendDataEntity MR_numberOfEntitiesWithPredicate:searchFilter inContext:PhobosDefaultContext] integerValue];
}
+ (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI {
......@@ -76,7 +79,7 @@ static NSManagedObjectContext *Phobos_defaultContext;
if (!sendAPI || [sendAPI isEqualToString:@""] || !data) {
return;
}
PhobosSendDataEntity *entity = [PhobosSendDataEntity MR_createEntityInContext:Phobos_defaultContext];
PhobosSendDataEntity *entity = [PhobosSendDataEntity MR_createEntityInContext:PhobosDefaultContext];
entity.data = [data mj_JSONData];
entity.api = sendAPI;
entity.status = PhobosDataSendStatusToBeSend;
......@@ -100,14 +103,14 @@ static NSManagedObjectContext *Phobos_defaultContext;
+ (void)deleteDataEntities:(NSArray<PhobosSendDataEntity *> *)entities {
if (entities.count > 0) {
[entities enumerateObjectsUsingBlock:^(PhobosSendDataEntity *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj MR_deleteEntityInContext:Phobos_defaultContext];
[obj MR_deleteEntityInContext:PhobosDefaultContext];
}];
[self saveWithCompletion:nil];
}
}
+ (void)deleteAllEntities {
[PhobosSendDataEntity MR_truncateAllInContext:Phobos_defaultContext];
[PhobosSendDataEntity MR_truncateAllInContext:PhobosDefaultContext];
[self saveWithCompletion:nil];
}
......@@ -115,8 +118,8 @@ static NSManagedObjectContext *Phobos_defaultContext;
* 在保存完成后调用的完成块。如果发生错误,块将以“BOOL”和“NSError”实例的形式传递成功状态。总是在主队列上调用。
*/
+ (void)saveWithCompletion:(MRSaveCompletionHandler)completion {
dispatch_async(dispatch_get_main_queue(), ^{
[Phobos_defaultContext MR_saveWithOptions:MRSaveSynchronously completion:completion];
dispatch_async(PhobosDataSaveQueue, ^{
[PhobosDefaultContext MR_saveWithOptions:MRSaveSynchronously completion:completion];
});
}
......
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