核心数据和中央调度?

发布于 2024-11-04 03:12:40 字数 381 浏览 0 评论 0原文

如果我知道没有其他事情发生(即我的程序此时仅对数据执行此 GCD 处理),那么在 GCD 块内以不交互的方式使用 Core Data NSManagedObjects 是否可以?

据说 NSManagedObject 不能与线程一起使用。然而这对我来说很模糊。由于发生错误,它可能是不安全的(因此数据实际上并不是不可变的)——但是,错误不应影响内存中的其他 NSManagedObject 数据。除非,当我使用其他 NSManagedObjects 时,数据的错误实际上导致了其他 NSManagedObject 的错误。会发生这种情况吗?

更具体地说,我谈论的是在我的 Core Data 对象图的一个节点上获取一组所有 NSManagedObjects (NSNumbers) 并向每个节点添加 1 之类的事情。

Is it OK to use Core Data NSManagedObjects in a manner within GCD blocks where they don't interact, if I know that nothing else is going on (i.e. my program is only executing this GCD processing on the data at this point in time)?

Supposedly NSManagedObject is not ok to use with threads. However it's vague to me. It could be unsafe because of the faulting that occurs (so the data isn't actually immutable) - however, the faulting shouldn't affect other NSManagedObject data in memory. Unless, the faulting in of data actually faults out other NSManagedObjects while I'm using them. Would that happen?

To be more concrete, I'm talking about something like obtaining a set of all NSManagedObjects (NSNumbers) at a node of my Core Data object graph and adding 1 to each of them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

赠意 2024-11-11 03:12:40

您只需要确保 (1) 每个线程/操作都有自己唯一的托管对象上下文,并且 (2) 最终将更改与所有其他同时活动的上下文合并。

后台线程下载数据并将其放入 Core Data,而前台线程管理 UI,这是很常见的情况。每个线程都有自己的上下文,并且它们独立操作,直到下载完成,此时后台上下文通知前端线程上下文它必须合并更新的对象。

You just need to make sure that (1) each thread/operation has its own unique managed object context and that (2) you eventually merge changes with all the other simultaneously active context.

It is quite common to have a background thread downloading data and putting it into Core Data while the foreground thread manages the UI. Each thread has its own context and they operate independently until the download is complete at which time the background context notifies the front thread context that it must merge the updated objects.

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