Jan*_*Jan 1 multithreading core-data ios
我有我的应用程序冻结问题,所以我使用Instruments找到问题,发现问题与CoreData保存和获取有关.我尝试过背景coredata方法(父子,通知),但我的问题一直没有解决.我也提到了http://martiancraft.com/blog/2015/03/core-data-stack/,但不知道这个方法在我的应用程序中是如何实现的.
仪器日志:https: //www.dropbox.com/s/agjtw1wqubsgwew/Instruments9.trace.zip?dl = 0
保存到DB
-(void)updateThreadEntityWithSyncDetails:(NSMutableDictionary *)inDictionary
{
NSString *loginUser=[[NSUserDefaults standardUserDefaults] valueForKey:@"currentUser"];
AppDelegate *sharedDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [sharedDelegate managedObjectContext];
// NSManagedObjectContext *writerContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
// [writerContext setPersistentStoreCoordinator:[sharedDelegate persistentStoreCoordinator]];
// create main thread MOC
// context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
// context.parentContext = writerContext;
// NSManagedObjectContext *contextforThread = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
// contextforThread.parentContext = context;
// [contextforThread performBlock:^{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ThreadInfo"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *userPredicate = [NSPredicate predicateWithFormat:@"userEmail == %@",loginUser];
NSPredicate *threadPredicate = [NSPredicate predicateWithFormat:@"threadID == %@",[inDictionary valueForKey:@"thread"]];
NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates: @[userPredicate, threadPredicate]];
[fetchRequest setPredicate:compoundPredicate];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:nil];
for (ThreadInfo *threadInfo in fetchedObjects)
{
if([[inDictionary allKeys] containsObject:@"userEmail"])
{
if([inDictionary valueForKey:@"userEmail"]!=[NSNull null])
{
threadInfo.userEmail=[inDictionary valueForKey:@"userEmail"];
}
}
if([[inDictionary allKeys] containsObject:@"badgeValue"])
{
if([inDictionary valueForKey:@"badgeValue"]!=[NSNull null])
{
threadInfo.badgeValue=[inDictionary valueForKey:@"badgeValue"];
}
}
if([[inDictionary allKeys] containsObject:@"choice4Percentage"])
{
if([inDictionary valueForKey:@"choice4Percentage"]!=[NSNull null])
{
threadInfo.choice4Percentage=[inDictionary valueForKey:@"choice4Percentage"];
}
}
if([[inDictionary allKeys] containsObject:@"choice5Percentage"])
{
if([inDictionary valueForKey:@"choice5Percentage"]!=[NSNull null])
{
threadInfo.choice5Percentage=[inDictionary valueForKey:@"choice5Percentage"];
}
}
}
NSError *error;
if(![context save:&error]) {
NSLog(@"Child error : %@",error);
}
// [context performBlock:^{
// NSError *error;
// if(![context save:&error]) {
// NSLog(@"%@",error);
// }
// }];
// }];
}
Run Code Online (Sandbox Code Playgroud)
取
-(ThreadInfo *)retrieveSolicitationInfoForThreadID:(NSString*)inThreadID;
{
NSString *loginUser=[[NSUserDefaults standardUserDefaults] valueForKey:@"currentUser"];
AppDelegate *sharedDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [sharedDelegate managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ThreadInfo"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *userPredicate = [NSPredicate predicateWithFormat:@"userEmail == %@",loginUser];
NSPredicate *threadPredicate = [NSPredicate predicateWithFormat:@"threadID == %@",inThreadID];
\
NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates: @[userPredicate, threadPredicate]];
[fetchRequest setPredicate:compoundPredicate];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:nil];
if(fetchedObjects.count!=0)
{
ThreadInfo *threadInfo=[fetchedObjects objectAtIndex:0];
return threadInfo;
}
return nil;
}
Run Code Online (Sandbox Code Playgroud)
同步
-(void)updateSolicitationWithSyncDetails:(NSDictionary *)inDictionary
{
NSMutableDictionary *paramDict=[NSMutableDictionary dictionaryWithDictionary:inDictionary];
NSString *userEmail=[[NSUserDefaults standardUserDefaults] valueForKey:@"currentUser"];
[paramDict setObject:[NSNumber numberWithBool:NO] forKey:@"isSystemMessage"];
[paramDict setObject:userEmail forKey:@"userEmail"];
if([[inDictionary allKeys] containsObject:@"owned"])
{
BOOL isDuplicate=[[IXDataBaseManager sharedNetworkDataManager] checkForExistenceOfThreadDetailsForSolicitationID:[inDictionary objectForKey:@"solicitation"]];// FETCH
if(!isDuplicate)
{
int randomIndex=[[IXNetworkDataManager sharedNetworkDataManager] getIndexForColorImageForTab:@"OUT"];
[paramDict setObject:message forKey:@"threadDescription"];
[paramDict setObject:[NSNumber numberWithInt:randomIndex] forKey:@"colorCode"];
BOOL isDuplicateVal=[[IXDataBaseManager sharedNetworkDataManager] checkForExistenceOfSolicitationID:[inDictionary objectForKey:@"solicitation"]];// FETCH
[paramDict setObject:message forKey:@"threadDescription"];
ThreadInfo *threadInfo=[[IXDataBaseManager sharedNetworkDataManager] retrieveSolicitationInfoForThreadID:[inDictionary objectForKey:@"solicitation"]];
[paramDict setObject:threadInfo.threadID forKey:@"thread"];
[[IXDataBaseManager sharedNetworkDataManager] updateThreadEntityWithSyncDetails:paramDict];
}
}
}
Run Code Online (Sandbox Code Playgroud)
首先,感谢您立即制作代码片段和跟踪.这非常有帮助.
所以,看看跟踪和代码.
-updateThreadEntityWithSyncDetails:在主线程上被调用,并且在那里花费了33%的时间.不好.
你逝去nil的error: 永远不会做到这一点.始终传递错误指针并检查调用结果以查看是否存在错误.
这个条件可以更清洁:
if([[inDictionary allKeys] containsObject:@"userEmail"])
{
if([inDictionary valueForKey:@"userEmail"]!=[NSNull null])
{
threadInfo.userEmail=[inDictionary valueForKey:@"userEmail"];
}
}
Run Code Online (Sandbox Code Playgroud)
考虑:
if (inDictionary[@"userEmail"] != nil && inDictionary[@"userEmail"] != [NSNull null]) {
threadInfo.userEmail = inDictionary[@"userEmail"];
}
Run Code Online (Sandbox Code Playgroud)
更容易阅读.
重写该方法将使主线程的工作失效:
- (void)updateThreadEntityWithSyncDetails:(NSMutableDictionary*)inDictionary
{
NSString *loginUser = [[NSUserDefaults standardUserDefaults] valueForKey:@"currentUser"];
AppDelegate *sharedDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[context setParentContext:[sharedDelegate managedObjectContext]];
[context performBlock:^{
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"ThreadInfo"];
[fetchRequest setReturnsObjectsAsFaults:NO];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"userEmail == %@ && threadID == %@",loginUser, inDictionary[@"thread"]];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:nil];
for (ThreadInfo *threadInfo in fetchedObjects) {
if (inDictionary[@"userEmail"] != nil && inDictionary[@"userEmail"] != [NSNull null]) {
threadInfo.userEmail = inDictionary[@"userEmail"];
}
if (inDictionary[@"badgeValue"] != nil && inDictionary[@"badgeValue"] != [NSNull null]) {
threadInfo.badgeValue = inDictionary[@"badgeValue"];
}
if (inDictionary[@"choice4Percentage"] != nil && inDictionary[@"choice4Percentage"] != [NSNull null]) {
threadInfo.choice4Percentage = inDictionary[@"choice4Percentage"];
}
if (inDictionary[@"choice5Percentage"] != nil && inDictionary[@"choice5Percentage"] != [NSNull null]) {
threadInfo.choice5Percentage = inDictionary[@"choice5Percentage"];
}
}
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Child error : %@",error);
}
}];
}
Run Code Online (Sandbox Code Playgroud)
但它仍然可能阻塞主线程,因为我猜测大部分CPU时间是在执行此提取.获取速度很慢.在该获取中有两个字符串比较.这很糟糕,应予以纠正.如果ThreadID不是字符串,则反转获取.否则这只是糟糕的数据模型设计,除了修复fetch之外没有太多帮助.
你的另一个非常慢点是-checkForExistenceOfThreadDetailsForThreadID:.您没有发布该方法,但我怀疑它是同一个问题,您的提取会花费您大量的时间并且它在主队列中.
总体而言,这种设计很差,需要重新设计.您正在比较数据存储中的字符串,这是检索数据的最慢方法之一.对于那些看起来不需要在主线程上的东西,你也是主线程.
记住黄金法则,如果数据没有被用户操纵,那么操作绝不能在主队列上.没有例外.