【发布时间】:2017-10-31 12:23:02
【问题描述】:
当我尝试在后台上下文中执行提取时,它会说: [NSManagedObjectContext Multithreading_Violation_AllThatIsLeftToUsIsHonor] ()
在后台线程上,我想检查一个对象是否存在然后更新。如果没有,我将其插入然后更新。出于某种原因,当我尝试执行提取时,xcode 一直说我有并发冲突。我是否不允许在后台上下文中执行提取?
这是我的上下文设置:
Persistent store -> Root saving context -> default context (concurrency type main)
persistent store -> Root saving context -> background context (concurrency type private)
后台上下文正在尝试执行Block:
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 助手只是创建一个获取请求,一个 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)")
}
}
任何帮助将不胜感激
【问题讨论】:
标签: ios objective-c core-data magicalrecord