2017-05-30 101 views
0

取回当我尝试对背景上下文获取它说: [的NSManagedObjectContext Multithreading_Violation_AllThatIsLeftToUsIsHonor()核心数据进行的performBlock在后台线程

在后台线程,我要检查如果一个对象存在然后更新。如果它不,我插入它,然后更新。出于某种原因,当我尝试执行提取操作时,xcode一直说我有并发冲突。我不允许在后台执行提取操作吗?

这是我对我的背景设置:

Persistent store -> Root saving context -> default context (concurrency type main) persistent store -> Root saving context -> background context (concurrency type private)

背景方面正试图performBlock:

NSManagedObjectContext *backgroundContext = [NSManagedObjectContext MR_context]; 
    [backgroundContext performBlock:^{ 
     [Conversation parseConversationAndCalls:objects inContext:backgroundContext]; 

     ... 
    }]; 

那么parseConversationAndCalls方法调用将进行获取:

+ (void) parseConversationAndCalls:(NSArray*)objects inContext:(NSManagedObjectContext*)localContext { 
    ... 
    for (NSArray *callData in conversationsCallData) { 

     NSNumber *callID = [BPDObject scrubbedInteger:callData[1]]; 
     Call *call = (Call*) [FetchRequestHelper fetchWithClass:Call.self withID:callID inContext:localContext]; // breakpoint hit here 

     if (call == nil) { 
      call = [Call insertInManagedObjectContext:localContext]; 
     } 

     [call updateWithValues:callData inContext:localContext]; 
     call.contact = contact; 
    } 
} 

fetchrequest helpe [R只是创建了一个获取请求,FRC,然后执行获取在performBlock块:

... //here context is the same as backgroundContext and frc is the fetched results controller 
    context.performAndWait { 
     do { 
      try frc.performFetch() 
     } catch { 
      fatalError("Failed to perform fetch from FetchedResultsController: \(error)") 
     } 
    } 

任何帮助将不胜感激

回答

0

一般一个fetchedResultsController,因为它被用来更新UI主线程上运行。当fetchedResultsController被创建时,它被赋予了一个managedObjectContext,我强烈怀疑它是主要的上下文。因此,当您在后台上下文中调用try frc.performFetch()时,您正在后台线程中访问主线程上下文。

这是我在你分享的代码中看到的。您可能还有其他违规行为没有分享。祝你好运跟踪他们。

+0

你可能是对的。我结束了使用没有FRC的提取请求,并且工作正常。 – Minimi