核心数据线程 [英] Core Data & Threads

查看:156
本文介绍了核心数据线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个使用核心数据的应用程序,我知道这不是线程安全,但是很难理解如何正确管理。让我解释一下我到目前为止...

I'm working on an app that uses core data and I'm aware that this is not thread safe but struggling to understand how to manage this correctly. Let me explain what I have so far...

我创建了一个单例,启动管理对象,然后可用于所有其他类,这似乎是

I've created a singleton the initiates the managed object and is then available to all other classes, this appears to be working correctly as the same managed object address is received by all classes.

然后,我有两个clases,按此顺序执行...

I then have two clases that are executed in this order...


  1. 数据加载。

  2. 此时,Tableview会显示步骤1中存储的数据。

我的问题是第1步。总是返回零记录,第2步。工作并返回正确的记录数。

My problem is that step 1. Is always returning zero records, and step 2. Is working and returns the correct record count.

已经做了一些测试的问题与1.是由于线程它的运行,如果我在同一个线程上发送的托管对象创建它的工作完美的请求。

Having done some testing the problem with 1. Is due to the thread its running on, if I despatch the request on the same thread that the managed object was created on it works perfectly.

所以为什么是在类1.我必须确保请求是在正确的线程,但在类2.它只是工作,或者它只是纯运气。

So why is it in class 1. i have to ensure that the request is on the correct thread but in class 2. It just works, or is it just pure luck. Is there a recommended and documented approach to threads and core data?

感谢

推荐答案

Core Data中并发的黄金规则是:每个NSManagedObjectContext只能从一个线程 - 它是在上创建的线程 - 访问。此外,一个托管对象只能与您获取的MOC一起使用 - 不会将其传递给其他MOC!

The golden rule for concurrency in Core Data is: each NSManagedObjectContext must only be accessed from one thread - the thread it was created on. Also, a managed object can only be used with the MOC you fetched it with - no passing it to other MOCs!

但是您可以在线程之间共享持久存储协调器,因为

But you can share a persistent store coordinator between threads because each MOC locks the PSC when it uses it - so a common setup is just to have one PSC for multiple MOCs.

更多信息:

http: //developer.apple.com/library/ios/#documentation/cocoa/conceptual/CoreData/Articles/cdConcurrency.html

如果您的案例2。似乎工作时使用不同的线程,我会说,更多的是运气,它不会使它是正确的事情做。

If your case 2. seems to be working when using different threads, I'd say it's more down to luck, and it doesn't make it the right thing to do. Don't depend on luck, follow the rule I mentioned above and all will be good.

其他相关阅读:

什么是CoreData不是线程安全的?

http://www.cocoanetics.com/2012/07/multi-context-coredata/

http://www.cimgf.com/2011/ 05/04 / core-data-and-threads-without-the-headache /

http://digitalflapjack.com/blog/2010/jun/11/parallelcoredata/

http:// www。 duckrowing.com/2010/03/11/using-core-data-on-multiple-threads/

作为一般规则

As a general rule, it's worth remembering that when APIs or docs say "Don't do X", it doesn't mean that doing X will fail or run into problems every time -- it just means that it will probably come back to haunt you one day. Don't leave things to chance, find out what you're allowed to do (API docs etc.) and do that.

这篇关于核心数据线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆