Core Data 的推断映射模型创建(轻量级迁移)崩溃。线程问题?

发布于 2024-08-26 05:12:50 字数 893 浏览 7 评论 0原文

在我的应用程序中创建推断映射模型(使用 Core Data 的轻量级迁移)时,我遇到随机崩溃。顺便说一句,我必须在应用程序运行时以编程方式执行此操作。

这就是我创建此模型的方式(当然,在我创建了正确的 currentModel 和 newModel 对象之后):

NSMappingModel *mappingModel = [NSMappingModel inferredMappingModelForSourceModel:currentModel destinationModel:newModel error:&error];

问题是这样的:这个方法随机崩溃。当它工作时,它工作得很好,没有问题。但是当它崩溃时,它会导致我的应用程序崩溃(而不是像应该那样返回 nil 来表示该方法失败)。我所说的随机,是指有时会发生,有时不会。这是不可预测的。

现在,事情是这样的:我正在另一个线程中运行这个方法。更准确地说,它位于通过 GCD 传递以在全局主队列上运行的块内。我需要这样做,以便我的 UI 对用户来说显得清晰,即以便我可以在工作正在进行时显示进度指示器。

奇怪的是,如果我删除 GCD 的东西并让它在主线程上运行,它似乎工作正常并且永远不会崩溃。因此,是否是因为我在不同的线程上运行它而导致崩溃?

我觉得这很奇怪,因为我不相信我违反了任何有关多线程的核心数据规则。特别是,我没有传递任何托管对象,每当我需要访问 MOC 时,我都会创建一个新的 MOC,即我不依赖任何 MOC(或就此而言:任何东西) 之前已在主线程上创建。除了发生的小 MOC 内容之外,还发生在映射模型创建方法之后,即应用程序崩溃的点之后,因此它不可能是导致这里正在考虑的崩溃。

我所做的就是获取两个 MOM 并要求它们之间的映射模型。即使在线程下这也不会错,不是吗?

对可能发生的事情有什么想法吗?

I'm getting random crashes when creating an inferred mapping model (with Core Data's lightweight migration) within my application. By the way, I have to do it programmatically in my application while it is running.

This is how I create this model (after I have made proper currentModel and newModel objects, of course):

NSMappingModel *mappingModel = [NSMappingModel inferredMappingModelForSourceModel:currentModel destinationModel:newModel error:&error];

The problem is this: This method is crashing randomly. When it works, it works just fine without issues. But when it crashes, it crashes my application (instead of returning nil to signify that the method failed, as it should). By randomly, I mean that sometimes it happens and sometimes not. It is unpredictable.

Now, here is the deal: I'm running this method in another thread. More precisely, it is located inside a block that is passed via GCD to run on the global main queue. I need to do this for my UI to appear crisp to the user, i.e. so that I can display a progress indicator while the work is underway.

The strange thing seems to be that if I remove the GCD stuff and just let it run on the main thread, it seems to be working fine and never crashing. Thus, could it be because I'm running this on a different thread that this is crashing?

I somehow find that weird because I don't believe I'm breaking any Core Data rules regarding multi-threading. In particular, I'm not passing any managed objects around, and whenever I need access to the MOC, I create a new MOC, i.e. I'm not relying on any MOC (or for that matter: anything) that has been created earlier on the main thread. Besides the little MOC stuff that occurs, occurs after the mapping model creation method, i.e. after the point at which the app crashes, so it can't possibly be a cause of the crashes under consideration here.

All I'm doing is taking two MOMs and asking for a mapping model between them. That can't be wrong even under threading, now can it?

Any ideas on what could be going on?

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

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

发布评论

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

评论(2

没有心的人 2024-09-02 05:12:50

首先,什么是崩溃?

其次,Core Data 通常是单线程 API。您可以在多个线程中执行某些操作,但创建 NSMappingModel 很可能不是其中之一。为什么必须动态创建映射模型?如果 MOM 是已知量,则映射也可以是已知量。

更新

首先,线程问题。 Core Data 是单线程的。但是,NSManagedObjectContext 知道如何正确锁定 NSPersistentStoreCoordinator,因此每个线程可以拥有一个 NSManagedObjectContext,因为它们知道如何正确锁定。然而,当您使用和创建映射模型时,情况并非如此。

但是,您提供的错误本身并不是核心数据错误。该错误表明您正在代码中的某个位置尝试将 nil 插入集合中。如果没有看到生成映射模型的代码,尽管很难猜测到底在哪里。

您是否在 objc_throw_exception 处放置了断点并查看代码中的哪一行导致了此崩溃?如果它是在一些不明显的东西中,那么我建议您在构建映射模型时存在某个点,该点给核心数据带来了意想不到的零。

您可以尝试的一件事是自己锁定 NSPersistentStore 和/或 NSManagedObjectContext 以查看是否可以解决崩溃问题。然而,我怀疑当你这样做时,你将再次处理性能问题。

First, what is the crash?

Second, Core Data generally is a single threaded API. There are things you can do in multiple threads but creating a NSMappingModel is most likely not one of them. Why must you create the mapping model dynamically? If the MOMs are a known quantity then the mapping can be a known quantity as well.

update

First, the threading issue. Core Data is meant to be single threaded. However, the NSManagedObjectContext knows how to lock the NSPersistentStoreCoordinator correctly therefore you can have one NSManagedObjectContext per thread because they know how to lock correctly. However this is not the case when you are working with and creating a mapping model.

However, the error you provided is not a Core Data error per se. That error indicates that somewhere in your code you are trying to stick a nil into a set. Without seeing the code that is generating the mapping model though it is difficult to guess as to exactly where.

Have you put a breakpoint at objc_throw_exception and see what line in your code is causing this crash? If it is in something non-obvious then I would suggest there is some point in your building of the mapping model that is giving Core Data an unexpected nil.

One thing you can try is locking the NSPersistentStore and/or the NSManagedObjectContext yourself to see if that resolves the crash. However I suspect when you do that you are going to again deal with performance issues.

青春如此纠结 2024-09-02 05:12:50

我最终完全放弃了这个问题,只是自己创建了该死的映射模型。

I ended up giving up on this problem altogether and just created the damned mapping models myself.

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