从多个线程将对象加载到一个托管对象上下文会出现什么问题?

发布于 2024-12-04 10:54:16 字数 398 浏览 0 评论 0原文

好的,所以我有多个线程将数据从持久存储(SQLite 数据库)提取到单个 NSManagedObjectContext (MOC) 实例中。我已经对其进行了相当彻底的测试,在操作重叠的情况下(线程 4 加载到 MOC,而线程 5 也加载到 MOC),一切都运行顺利。

我已经阅读了(大部分)核心数据编程指南,并且知道苹果不会纵容这种行为,但我想知道具体原因是什么。苹果在文档中简要提到了这样一个事实:不仅写入是危险的,读取也是危险的,因为可能会发生错误。如何(如果我从单独线程加载的所有对象都是彼此不重叠的单独实体)?这难道不是意味着该应用程序的性能会受到影响吗?因此,如果我确定性能下降比增加内存使用更好,我会倾向于使用单一 MOC(不推荐)方法。

无论如何,我都会恢复推荐的方法,但我只是好奇如果不遵守这些规则可能会发生什么的具体细节。提前致谢。

Ok, so I have multiple threads pull data from the persistent store (SQLite database) into a single NSManagedObjectContext (MOC) instance. I have tested it pretty thoroughly and in the case where operations overlap (thread 4 loads into the MOC while thread 5 does so too), everything runs smoothly.

I have read (most of) the Core Data Programming guide and am aware that Apple does not condone this behavior, but I am wondering what the particular reasons are. In the docs Apple briefly alludes to the fact that not only writes are dangerous, but reads are as well because faulting may occur. How (if all the objects I load from the separate threads are individual entities with no overlap with each other)? And doesn't that just mean that the app will take a performance hit? So if I determine that a performance hit is a better tradeoff than increased memory usage, I would be inclined to use the one MOC (non-recommended) method.

I am going to revert to the recommended method in any-case but am just curious about the specific details of what could happen if these rules aren't adhered to. Thanks in advance.

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

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

发布评论

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

评论(2

暗喜 2024-12-11 10:54:16

我真的不知道,但是还没有人回答。

不是线程安全的就不是线程安全的。想象一下,Core Data 程序员将一些未公开的实例变量放入 NSManagedObjectContext numberOfObjectsLoaded 中。在线程 A 中,您访问托管对象 foo.bar 的关系。在线程 B 中,您对一些完全不相关的对象 baz.number = 14 进行了更改。 foo 和 baz 都是错误。他们被加载了。两个线程都尝试执行该代码行,

numberOfObjectsLoaded += 1

但线程 A 读取 numberOfObjectsLoaded,得到 77,然后进行上下文切换。线程 B 也读取 77,加 1,分配 78。回到线程 A。它将读取的 77 加 1,得到 78,并将 78 分配给 numberOfObjectsLoaded。 numberOfObjectsLoaded 应该为 79,但实际为 78。这种不一致将导致意外错误,并可能最终导致崩溃。这种情况可能只发生在您运行程序的 1/1000 次,并且可能直到实际错误发生几分钟后才显现出任何不好的情况。非常难以调试。

听从他们的建议,不要尝试。

I don't really know, but since nobody answered yet.

Not thread-safe is not thread-safe. Imagine the Core Data programmers put some unexposed instance variable in NSManagedObjectContext numberOfObjectsLoaded. In thread A, you access a relationship of a managed object, foo.bar. In thread B, you make a change to some completely unrelated object baz.number = 14. Both foo and baz are faults. They get loaded. Both threads try to execute the line of code

numberOfObjectsLoaded += 1

But thread A reads numberOfObjectsLoaded, gets 77, then context switch. Thread B reads 77 also, adds 1, assigns 78. Back to thread A. It adds 1 to 77, which is what it read, gets 78, and also assigns 78 to numberOfObjectsLoaded. numberOfObjectsLoaded should have 79, but has 78. That inconsistency will lead to unexpected errors, and probably eventually a crash. It will only happen maybe 1/1000 times you run your program, and possibly nothing bad will be apparent until several minutes after the actual error occurred. Very hard to debug.

Follow their advice and don't try it.

幸福不弃 2024-12-11 10:54:16

您是否从数据库中提取了对象,或者是否也尝试访问属性?那就是处理错误的时候......

很可能在某个时候,您的代码会崩溃。

Did you pull the objects out of the database, or did you also try to access properties? That is when faults get dealt with...

It is very likely that at some point, your code will crash.

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