仅具有一个数据上下文的核心数据。对吗?
我正在尝试使用 Objective C + Core Data 制作我的第一个应用程序,但我不确定这是正确的方法,因为这对我来说真的很奇怪。
我只有一个数据上下文,它是我在启动时在应用程序委托中创建的。该数据上下文用于所有操作(读、写)。在另一个环境(例如 C# 和 LINQ)中,我尝试使这些操作尽可能单一。在这里,我似乎只需要创建一次数据上下文,然后使用它而无需关闭它(应用程序退出时除外)。
我还有一个异步操作来更新此数据。当然,它再次使用相同的数据上下文。它可以工作,但感觉不对。
我的应用程序委托保留核心数据中包含的对象的 NSArray。我在所有视图中都使用同一个 NSArray。
实际上,一旦获得所需的所有对象,我就会自然地关闭数据上下文,但是......对象不是总是附加到数据上下文吗?如果我关闭或释放数据上下文,所有这些对象也会得到释放,对吧?
正如您所注意到的,我在这里缺少一些东西:)感谢您的帮助。
I'm trying to make my first application using Objective C + Core Data, but I'm not sure it's the correct way, as it feels really weird to me.
I have only one data context, which I create at launch time, in the Application Delegate. This data context is used for all the operations (read, write). In another environment (C# and LINQ for example), I try to make these operations as unitary as possible. Here it seems I just have to create the data context once, and work with it without closing it ever (except when the application exits).
I also have an asynchronous operation in which I update this data. Of course, it uses the same data context again. It works, but doesn't feel right.
My Application Delegate keeps a NSArray of the objects contained in Core Data. I use this same NSArray in all my views.
I would actually naturally close the data context once I got all the objects I require, but... aren't the objects always attached to the data context? If I close or release the data context, all these objects will get releases as well, right?
As you can notice, there is something I'm missing here :) Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所引用的
NSManagedObjectContext
更像是一个“便笺簿”,而不是数据库连接。对象在此工作区域中创建、修改、销毁,并且仅当您告诉 MOC 保存状态时才保留(如果您愿意,“写入数据库”)。如果您在单独的线程中工作,您可以(并且应该)init
和release
MOC,但是应用程序委托使 MOC 可用,以便在主线程上执行的所有代码都可以使用相同的上下文。这既方便又让您不必确保多个 MOC 彼此保持同步。通过保留 Core Data 对象的
NSArray
,您实际上是在重复其功能。是否有任何理由不使用 MOC 提供的核心数据对象 NSSet?如果您正在异步工作,那么您不应该跨线程共享 NSManagedObjectContext 对象,因为它们不是线程安全的。相反,为每个线程创建一个,但将它们设置为使用相同的 NSPercientStoreCoordinator。这将序列化他们对持久数据的访问,但您需要使用通知让他们每个人都了解其他更改。
这里有一个关于如何在多个线程上使用 Core Data 的很好的教程/描述:
http://www.duckrowing.com/ 2010/03/11/在多线程上使用核心数据/
The
NSManagedObjectContext
to which you refer is more of a "scratchpad" than a database connection. Objects are created, amended, destroyed in this working area, and only persisted ("written to the database" if you prefer) when you tell the MOC to save state. You can (and should)init
andrelease
MOCs if you are working in separate threads, but the App Delegate makes a MOC available so that all code executing on the main thread can use the same context. This is both convenient, and saves you from having to ensure that multiple MOCs are kept in sync with each other.By keeping an
NSArray
of Core Data objects, you are in effect duplicating its functionality. Is there any reason for not working with an NSSet of Core Data objects provided by the MOC?If you are working asynchronously, then you should not be sharing an
NSManagedObjectContext
object across threads, as they are not thread-safe. Instead, create one for each thread, but set them to use sameNSPersistentStoreCoordinator
. This will serialise their access to the persisted data, but you'll need to use notifications to make them each aware of the others changes.There is a good tutorial/description on how to use Core Data on multiple threads here:
http://www.duckrowing.com/2010/03/11/using-core-data-on-multiple-threads/
1) 核心数据和线程,轻松无忧
http://www.cimgf。 com/2011/05/04/core-data-and-threads-without-the-headache/
2)与核心数据的并发
http://developer.apple.com/ Library/ios/#documentation/cocoa/conceptual/CoreData/Articles/cdConcurrency.html
3) 多上下文 CoreData
http://www.cocoanetics.com/2012/07/multi-context-核心数据/
1) CORE DATA AND THREADS, WITHOUT THE HEADACHE
http://www.cimgf.com/2011/05/04/core-data-and-threads-without-the-headache/
2) Concurrency with Core Data
http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/CoreData/Articles/cdConcurrency.html
3) Multi-Context CoreData
http://www.cocoanetics.com/2012/07/multi-context-coredata/