ios – 为什么在尝试访问获取对象的属性时会出现CoreData多线程违规?

ios – 为什么在尝试访问获取对象的属性时会出现CoreData多线程违规?,第1张

概述以下代码适用于没有最后一个语句.但是,在最后一行, Xcode停止并显示以下消息: CoreData`+[NSManagedObjectContext Multithreading_Violation_AllThatIsLeftToUsIsHonor]: NSManagedObjectContext *context = GLOBAL_appDelegate.coreDataHelper.cont 以下代码适用于没有最后一个语句.但是,在最后一行,@L_502_0@停止并显示以下消息:

CoreData`+[NSManagedobjectContext
Multithreading_Violation_AllThatIsleftToUsIsHonor]:

NSManagedobjectContext *context = GLOBAL_appDelegate.coreDataHelper.contextBackground;    [context performBlock:^{         NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];         [fetchRequest setEntity:[NSEntityDescription entityForname:@"CdTag" inManagedobjectContext:context]];         nspredicate *predicate = [nspredicate predicateWithFormat:@"Title BEGINSWITH[cd] %@",@"webpages"];         [fetchRequest setPredicate:predicate];         NSArray *cdTags = [context executeFetchRequest:fetchRequest error:nil];         NSLog(@"number of Tags: %li",cdTags.count);         CdTag *cdTag = [cdTags objectAtIndex:0];         // Accessing a property causes the assertion warning to be shown         cdTag.Title;    }];

我正在使用Xcode 7 beta 5,我启用了多线程断言.

上下文定义如下:

_model = [NSManagedobjectModel mergedModelFromBundles:nil];_coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedobjectModel:_model];_context = [[NSManagedobjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];[_context setPersistentStoreCoordinator:_coordinator];_contextBackground = [[NSManagedobjectContext alloc] initWithConcurrencyType:nsprivateQueueConcurrencyType];[_contextBackground setParentContext:_context];

这是一个Xcode错误还是我做错了什么?

解决方法 你还可以加上上下文初始化发生的代码吗?

您获得的错误与从初始化的线程/队列之外的线程/队列访问托管对象上下文有关.

通常发生的是使用默认/遗留“限制类型”并发类型初始化托管对象上下文

performBlock:

performBlockAnDWait:

方法实际上不会做他们做广告的事情,即确保在初始化时与其关联的队列上访问上下文.

如果我上面提到的是实际发生的事情你可以(假设其他一切都已到位)通过初始化你的上下文来解决这个问题:

_managedobjectContext = [[NSManagedobjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];

要么

_managedobjectContext = [[NSManagedobjectContext alloc] initWithConcurrencyType:nsprivateQueueConcurrencyType];

这是关于并发策略的文档:

When you create a managed object context using initWithConcurrencyType:,you have three options for its thread (queue) association

Confinement (NSConfinementConcurrencyType)

For backwards compatibility,this is the default. You promise that context will not be used by any thread other than the one on which you created it. In general,to make the behavior explicit you’re encouraged to use one of the other types instead.

You can only use this concurrency type if the managed object context’s parent store is a persistent store coordinator.

Private queue (nsprivateQueueConcurrencyType)

The context creates and manages a private queue.

Main queue (NSMainQueueConcurrencyType)

The context is associated with the main queue,and as such is tIEd into the application’s event loop,but it is otherwise similar to a private queue-based context. You use this queue type for contexts linked to controllers and UI objects that are required to be used only on the main thread.

If you use contexts using the confinement pattern,you send the contexts messages directly; it’s up to you to ensure that you send the messages from the right queue.

You use contexts using the queue-based concurrency types in conjunction with performBlock: and performBlockAnDWait:.

Apple Docs

作为旁注,你得到的错误信息是仍然站在Cocoa内的最后几个“复活节彩蛋”之一.

总结

以上是内存溢出为你收集整理的ios – 为什么在尝试访问获取对象的属性时会出现CoreData多线程违规?全部内容,希望文章能够帮你解决ios – 为什么在尝试访问获取对象的属性时会出现CoreData多线程违规?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1055671.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存