2017-05-30 85 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