核心数据:无法使用以前保存的对象
我真的被这两件事困住了。
我想做的:
- 我的实体很简单。这是一个“记录”。
- 它有一个“名称(NSString)”和“父(relationShip)”,
- “父”连接到它自己,实体“记录”。
好的,现在我想创建“parentRecord”和“simpleRecord”。
我尝试处理该代码:
groupRecord = (record *)[NSEntityDescription insertNewObjectForEntityForName:@"record"
inManagedObjectContext:self.managedObjectContext];
groupRecord.name = GroupTextField.text;
[self saveContext];
它是“parentRecord”,我将其保存以供将来使用,并捕获“groupRecord”变量。
现在我必须创建一个“simpleRecord”。这是一段代码:
record *newRecord = (record *)[NSEntityDescription
insertNewObjectForEntityForName:@"record"
inManagedObjectContext:self.managedObjectContext];
newRecord.name = textField.text;
[newRecord setMyParent:groupRecord]; //and it crashes here!
我重新排列了这段代码,所以 *我不在“parentRecord”中执行 [self saveContext];
*。 只需从变量 groupRecord
中使用它即可。并将其保存在“childRecord”块中。那么一切都很好。记录保存到存储中,我可以从那里读取它。
为什么会发生这种情况?如果我想先创建“parentRecord”,保存它,然后再创建“childRecord”,我该怎么办?
为什么我不能使用以前保存的对象? NSManagedObjectContext 是一样的 - 有什么问题吗?
我对“经典”SQL 已经足够好了,但是核心数据正在扼杀我的大脑。
谢谢大家。
更新:
看,saveContext 没有崩溃的理由。这是:
- 创建父实体。
- 将其设置为 appDelegate 的变量。
- 保存上下文(针对家长)。
- 创建子实体。
- 从 appDelegate 的变量设置parentProperty。应用程序崩溃!
并且:
- 创建父实体。
- 将其设置为 appDelegate 的变量。
- ///////////保存上下文(针对父级)。
- 创建子实体。
- 从 appDelegate 的变量设置parentProperty。没有任何崩溃。
- 这次保存上下文。
- 现在一切都很好。
父属性 - 只是属性的名称。它不是 MOM 文件中父级的一些附加设置。
我想做具有层次结构的实体。 Xcode 没有为我创建一些额外的方法 - 只是一个属性。
I am really stuck with these two things.
What I am trying to do:
- My entity is simple. It's a "record".
- It has a "name (NSString)" and "parent (relationShip)"
- "parent" connect to itself, entity "record".
Ok, now I want to create "parentRecord" and "simpleRecord".
I try to do with that code:
groupRecord = (record *)[NSEntityDescription insertNewObjectForEntityForName:@"record"
inManagedObjectContext:self.managedObjectContext];
groupRecord.name = GroupTextField.text;
[self saveContext];
It's "parentRecord", I save it for a future use, and catch in "groupRecord" variable.
Now I have to create a "simpleRecord". This is a code:
record *newRecord = (record *)[NSEntityDescription
insertNewObjectForEntityForName:@"record"
inManagedObjectContext:self.managedObjectContext];
newRecord.name = textField.text;
[newRecord setMyParent:groupRecord]; //and it crashes here!
I rearranged this code, so *I don't do [self saveContext];
* in "parentRecord".
Just use it from variable groupRecord
. And save it in "childRecord" block. Then all is fine. Records save to storage and I can read it from there.
Why does it happens? What should I do, if I want to create "parentRecord" first, SAVE IT ,and later - "childRecord"?
Why can't I use previously saved object? NSManagedObjectContext is the same - what's wrong?
I am good enough with "classic" SQL, but Core Data is killing my brain.
Thanks to everyone.
Update:
Look, saveContext is out of reasons to crash. Here is:
- Create parent entity.
- Set it to variable of appDelegate.
- Save context (for a parent).
- Create childEntity.
- Set parentProperty from variable of appDelegate. App crashes!
And:
- Create parent entity.
- Set it to variable of appDelegate.
- ///////////Save context (for a parent).
- Create childEntity.
- Set parentProperty from variable of appDelegate. No any crash.
- Savecontext this time.
- All is fine now.
Parent property - is just a name of the property. It is not some additional setup for a parent in MOM file.
I want to do entity with hierarchy.
And there is NO some additional methods, that Xcode create for me - just a properties.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,听起来你有一个简单的数据模型,如下所示(伪代码):
这很危险,因为没有相互关系。这可能会导致孤立对象并损害对象图的完整性。而是使用:
现在,您有一个简单的一维链表,如数组或集合。除了最上面的记录对象之外,每个记录对象都有一个父对象,并且最下面的对象每个都有一个子对象。要为每个分配一个,您将执行以下操作:
现在,如果您想要一个树结构,其中每个父级可以有多个子级,您将拥有一个如下所示的对象模型:
然后,您的插入和分配更改为:
原因是 a to - 许多关系需要一个方法来添加新对象来设置。这不能通过简单的分配来完成。然而,孩子只有一个父母,因此它可以使用简单的分配。由于关系是互惠的,分配给一个对象会自动分配给关系另一侧的对象。
这就是它应该如何运作的。您看到的错误很可能来自于错误的对象模型。如果您有这样的一对一的必需关系:
...当您尝试保存父母或孩子失踪时,您将遇到问题。同样,如果您尝试将多个对象分配给一对一关系,您可能会收到所看到的错误。
执行插入时切勿使用强制转换,因为如果分配的类与强制转换类之间不匹配,则运行时将强制另一个类进行强制转换,从而导致各种奇怪的错误。
我无法确切地说出您的问题是什么,因为我看不到您的对象模型。然而,这应该为您指明正确的方向。
Okay it sounds like you have a simple data model that looks like this (pseudocode):
This is dangerous because there is no reciprocal relationship. This can lead to orphaned objects and compromise the integrity of the object graph. Instead use:
Now, you have a simple, one dimensional linked list like an array or set. Except for the topmost record object, every record object has a parent and expect for the bottommost object each has a child. To assign one to each you would do:
Now if you want a tree structure in which each parent can have more than one child, you would have an object model like so:
Your insertion and assignments then change to:
The reason is that a to-many relationship requires a method to add the new object to set. Which cannot be done with a simple assignment. The child, however, only has one parent so it can use a simple assignment. Since the relationship is reciprocal, assigning to one object automatically assigns to the object on the other side of the relationship.
That is how it should work. The errors you are seeing most likely come from having the wrong object model. If you have one-to-one, required relationships like this:
... you will encounter problems when you try to save if either a parent or child is missing. Likewise, if you try to assign multiple objects to a to-one relationship, you can get the error you are seeing.
You should never use the cast when doing an insertion because if you have a mismatch between the assigned class and the cast class, the runtime will force the other class into the cast causing all kinds of strange errors.
I can't say for certain exactly what your problem is because I can't see your object model. This however, should point you in the right direction.
您会分享“saveContext”和“setMyParent”的代码吗?
NSManagedObjectContext 有 -(BOOL)save:(NSError**)error 方法。这是在“saveContext”中调用的吗?
而且,如果您的关系被称为“父”,那么您应该使用类似 -addParentObject: ... 的内容设置关系,这将在您的 Record.h 文件中声明。如果您按特定顺序执行操作,Xcode 将为您执行此操作。否则,您将需要自己编写方法声明。
Would you share code for "saveContext" and for "setMyParent"?
NSManagedObjectContext has -(BOOL)save:(NSError**)error method. Is that being called within "saveContext"?
And, if your relationship is called "parent", then you should be setting relationship with something like -addParentObject: ... which would be declared in your Record.h file. Xcode will do this for you, if you do things in a certain order. Otherwise, you will need to write the method declarations yourself.