Mapping MagicalRecord Relationships Duplicate Objects Despite Primary Keys

I need help resolving the problem I am having when importing MagicalRecord data. I got the impression that MagicalRecord was able to handle relationship matching without duplicating objects by looking at primary keys ( relatedByAttribute ).

Here's a simple JSON:

 [ { parentId: "<unique id>", parentName : "<name>", children : [ { childId: "<unique id>", childName: "<name>" }, { childId: "<unique id>", childName: "<name>" } ] }, { <another parent with children> } ] 

I have an NSManagedObject Parent that has to-many relationship with Child NSManagedObject. The relationship name is children , and I set relatedByAttribute to Child and Parent to childId and parentId respectively.

When I parse JSON, Parent not duplicated and validates the primary key correctly and uses the existing object, if present. However, for children it duplicates objects every time I parse JSON. If I parse children separately (so JSON just contains an array with child dictionaries), it has no problems with correctly displaying data and using existing objects for children that already exist in the database.

Am I misunderstood and mistakenly believe that MagicalRecord maps relationships? Currently, I have configured the extension class using the importChildren: function, where I can handle all search queries manually and create / import objects accordingly.

Thanks!

+5
source share
1 answer

I managed to do this a while ago.

What I did was add relatedByAttribute to the user information dictionary for the children AND relation for the Child object.

So, click on the link and set relatedByAttribute to childId (in my example above), and also click on the child on the left, and for that also set relatedByAttribute to childId in the user information dictionary for the entity itself.

This allows Magical Record to correctly display existing objects or create new ones if necessary - provided that the ID attribute is 100% unique!

enter image description here

+11
source

All Articles