开发者

Core Data: Can't Use Previously Saved Object

开发者 https://www.devze.com 2023-01-25 02:01 出处:网络
I am really stuck with these two things. What I am trying to do: My entity is simple. It\'s a \"record\".

I am really stuck with these two things.

What I am trying to do:

  1. My entity is simple. It's a "record".
  2. It has a "name (NSString)" and "parent (relationShip)"
  3. "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.

Thank开发者_如何学编程s to everyone.


Update:

Look, saveContext is out of reasons to crash. Here is:

  1. Create parent entity.
  2. Set it to variable of appDelegate.
  3. Save context (for a parent).
  4. Create childEntity.
  5. Set parentProperty from variable of appDelegate. App crashes!

And:

  1. Create parent entity.
  2. Set it to variable of appDelegate.
  3. ///////////Save context (for a parent).
  4. Create childEntity.
  5. Set parentProperty from variable of appDelegate. No any crash.
  6. Savecontext this time.
  7. 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.


Okay it sounds like you have a simple data model that looks like this (pseudocode):

Record{
    name:string
    parent-->Record
}

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:

Record{
    name:string
    parent<--(optional)-->Record.child
    child<--(optional)-->Record.parent
}

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:

Record *firstRec; //assuming you have created a custom class for Record
Record *secRec;
firstRec=[NSEntityDescription insertNewObjectForEntityForName:@"Record" 
                                       inManagedObjectContext:self.managedObjectContext];
//-------------------------------------^
secRec=[NSEntityDescription insertNewObjectForEntityForName:@"Record" 
                                       inManagedObjectContext:self.managedObjectContext];
//-------------------------------------^
firstRec.name=someText;
secRec,name=someOtherText;

firstRec.child=secRec;
[self saveContext];

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:

Record{
    name:string
    parent<--(optional)-->>Record.child
    child<<--(optional)-->Record.parent
}

Your insertion and assignments then change to:

Record *firstRec;
Record *secRec;
firstRec=[NSEntityDescription insertNewObjectForEntityForName:@"Record" 
                                       inManagedObjectContext:self.managedObjectContext];
//-------------------------------------^
secRec=[NSEntityDescription insertNewObjectForEntityForName:@"Record" 
                                       inManagedObjectContext:self.managedObjectContext];
//-------------------------------------^
firstRec.name=someText;
secRec.name=someOtherText;
[firstRec.addChildObject:secRec];
// or
secRec.parent=firsRec;

[self saveContext];

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:

Record{
    name:string
    parent<--(required)-->Record.child
    child<--(required)-->Record.parent
}

... 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消