仅具有一个数据上下文的核心数据。对吗?

发布于 2024-10-11 05:31:06 字数 497 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拿命拼未来 2024-10-18 05:31:06

您所引用的 NSManagedObjectContext 更像是一个“便笺簿”,而不是数据库连接。对象在此工作区域中创建、修改、销毁,并且仅当您告诉 MOC 保存状态时才保留(如果您愿意,“写入数据库”)。如果您在单独的线程中工作,您可以(并且应该)initrelease 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 and release 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 same NSPersistentStoreCoordinator. 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文