本文介绍了为什么我的一些核心数据对象的managedObjectContext设置为nil?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题添加对象到我的一个核心数据对象有一个关系,经过一些调查后,我意识到这是因为对象managedObjectContext是nil(0x0)。一些更多的调查显示,任何时候我创建了一个类型的对象,managedObjectContext总是nil。我创建的任何其他类型的每个对象都有正确的managedObjectContext。没有,对象没有被删除,这是紧接着调用NSEntityDescription:insertNewObjectForEntityForName:inManagedObjectContext

I was having problems adding objects to one of my core data objects that has a relationship and after doing some investigating I realized it was because that objects managedObjectContext was nil (0x0). Some more investigating revealed that anytime I created an object of that type, the managedObjectContext was always nil. Every object I create of any other type has the correct managedObjectContext. And no, the object was not deleted, this is immediately after calling NSEntityDescription:insertNewObjectForEntityForName:inManagedObjectContext

为什么只有这个特定的对象被打破?也奇怪的是,即使managedObjectContext似乎是nil,它否则工作正常(除了关系问题)。我已经尝试重置模拟器几次,我甚至重新生成的.h和.m文件核心数据为该对象产生,但我不能让它工作。就我可以告诉,没有错误,managedObjectContext只是从未设置。

Why would only this particular object be broken? It's also strange that even though the managedObjectContext seems to be nil, it otherwise works correctly (other than the relationship problem). I've tried resetting the simulator several times and I've even regenerated the .h and .m files that core data produces for that object but I can't get it to work. As far as I can tell, there are no errors, the managedObjectContext just never gets set.

推荐答案

我想出来。我添加了一个成员变量managedObjectContext,我本能地添加到我的应用程序中的大多数类,因为我几乎总是需要它。我没有意识到,我是重写在父类NSManagedObject中同名的方法。这就是为什么它被重置为nil并仍然工作,直到我试图添加关系,查询managedObjectContext,发现它是nil,并抱怨。

I figured it out. I had added a member variable called managedObjectContext, which I instinctively add to most classes in my application since I pretty much always need it. What I didn't realize is that I was overriding the methods of the same name in the parent class NSManagedObject. That's why it was reset to be nil and still worked until I tried to add relationship which queried the managedObjectContext, found it to be nil, and complained.

这篇关于为什么我的一些核心数据对象的managedObjectContext设置为nil?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:03