ios – 嵌套的performBlock:在NSManagedObjectContext上

ios – 嵌套的performBlock:在NSManagedObjectContext上,第1张

概述为NSManagedObjectContext使用NSPrivateQueueConcurrencyType和NSMainQueueConcurrencyType类型时, 在同一个上下文中进行嵌套的performBlock调用是否安全? [backgroundContext performBlock:^{ NSFetchRequest *myRequest = ...; __blo 为NSManagedobjectContext使用nsprivateQueueConcurrencyType和NSMainQueueConcurrencyType类型时,
在同一个上下文中进行嵌套的performBlock调用是否安全?
[backgroundContext performBlock:^{   NSFetchRequest *myRequest = ...;     __block NSArray *result= nil;   [backgroundContext performBlockAnDWait:^{       results = [backgroundContext executeFetchRequest:myRequest error:NulL];   }];}];

它可能看起来很愚蠢,但我有一个现有的代码库,其中包含许多帮助方法,这些方法封装了executeFetchRequest调用.我不想假设调用者是否已经使用了performBlock.
例如:

-(voID)updateObjects:(BOol)synchronous{    if (YES == synchronous)        [self fetchHelper];    else    {        [backgroundContext performBlock:^{             [self fetchHelper];        }];    }}-(NSArray*)fetchHelper{     [self.backgroundContext performBlockAnDWait:^{         //Fetch the objects...         [self.backgroundContext executeFetchRequest: (...)];     }];}

我试过了它,它的工作原理.但我已经学会了(艰难的方式)对核心数据和多线程非常小心.

解决方法 是的,performBlockAnDWait是可重入的.直接来自Apple的发行说明……

Core Data formalizes the concurrency model for the NSManagedobjectContext class with new options. When you create a context,you can specify the concurrency pattern to use with it: thread confinement,a private dispatch queue,or the main dispatch queue. The NSConfinementConcurrencyType option provIDes the same behavior that was present on versions of iOS prior to 5.0 and is the default. When sending messages to a context created with a queue association,you must use the performBlock: or performBlockAnDWait: method if your code is not already executing on that queue (for the main queue type) or within the scope of a performBlock… invocation (for the private queue type). Within the blocks passed to those methods,you can use the methods of NSManagedobjectContext freely. The performBlockAnDWait: method supports API reentrancy. The performBlock: method includes an autorelease pool and calls the processpendingChanges method upon completion.

总结

以上是内存溢出为你收集整理的ios – 嵌套的performBlock:在NSManagedObjectContext上全部内容,希望文章能够帮你解决ios – 嵌套的performBlock:在NSManagedObjectContext上所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1103928.html

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

发表评论

登录后才能评论

评论列表(0条)

保存