Say, I have my coredata structured to something like this…
Class
- classID <- RestKit primaryKeyAttribute
- name
- time
- students (relationship 1-to-many)
Student (nested in Class)
- studentID
- name
- class (inverse relationship) <- RestKit primaryKeyAttribute ** this doesn’t work?! **
I specifed primaryKeyAttribute of the mappings as shown above. When I send a load request using restkit, it will correctly update object with the same primarykey. RestKit seems to update the class correctly, but for students, it just adds new ones and nullify the class relationship of old students. So now I have unwanted student entities in db with no reference to class.
Is there a way I can use RestKit to update the students relationship correctly? Btw, I thought about using studentID as primaryKeyAttribute instead, but it may not work correctly if have some students removed in the new updates. May be I have to clear all students for the class before updating, but I also don’t know how to do that in RestKit. Because by the time RKObjectLoader didLoadObject is called, seems like everything is already saved to the managedObjectContext. Any ideas? 🙁
Still can’t find a RestKit-way solution, and doesn’t look like there will be anyone who can answer this :-\ So I’m posting the best approach I can come up with.
My current work around is to iterate through class’ students and manually delete them in
objectLoader:willMapData:. I have to do here (not inobjectLoader:didLoadObject:) because it is the place where the new data is not saved into MOC yet.You can also delete the Class entity entirely, but that way doesn’t actually updates the old one. It creates a new one with a new “core data internal _pk” (primary key) (yes, I do care about it).