MagicalRecord cannot automatically match relationships with this JSON format:
{ Id : thisIsThePhotoID, Date : today, UserId : 12345asdfg }
For MagicalRecord to map the relation to the Person object, it also had to be an object in JSON, for example:
{ Id : thisIsThePhotoID, Date : today, User : { UserId : 12345asdfg } }
Thus, MagicalRecord knows that this is an object, and it will do a corresponding search in your existing database to record Person with the identifier above and match the relationship.
So there are two problems with this. If you cannot change the JSON output, you need to create a category class on Photo , where you manually map this relationship yourself. I will get to this after the second problem.
The second problem is that the aforementioned JSON format assumes that you have already analyzed the users and saved the records in your database. If you do not have MagicalRecord, you will create a new Person record with the identifier above, but since there are no other attributes on this object (note that the UserId key is the only attribute in the dictionary), it will be quite empty and will not contain the name and email address mail. You can always expand your JSON (if you have such an opportunity) to include these attributes in the Person dictionary in the photo dictionary:
{ Id : thisIsThePhotoID, Date : today, User : { UserId : 12345asdfg, FullName : Oliver, EmailAddress : oliver@oliver.com } }
The JSON payload is pretty small, so it doesn't hurt to do this if you can. In addition, it will create a new Person record if it no longer exists in the database.
And then for manual matching. If you cannot change the JSON to the above format, you need to manually override the relationship mapping, since JSON is not prepared for how MagicalRecord does.
Create a category class for Photo called Photo+Mapping.h/.m . I like to stick +Mapping for them. Then the class should be Photo (Mapping) in the header and implementation file, and you're good to go.
MagicalRecord has several instance methods available for redefinition (see the last part of this article on importing MagicalRecord , written by the author of MagicalRecord), among them import<;attributeName>;: and import<;relationshipName>;: . There are also willImport: didImport: and shouldImport: the class itself, which allows you to override any mapping.
In your case, you can use import<;relationshipName>;: or shouldImport: I took these two because you have a few advantages depending on whether you have already matched all your Person objects, and they are available for matching relationships in the Photo object.
Here are examples of what you can do (you can choose a combination of several of them, if you want, it will not hurt to do this). Note: ALWAYS use the current NSManagedObjectContext when overriding the display (easily accessible with MagicalRecord via self.managedObjectContext ), otherwise you will run into context problems.
Be sure to import Person:
#import "Photo+Mapping.h" #import "Person.h"
Hope this helps! Let me know if you have any questions.