Search code examples
iosjsoncore-datamagicalrecord

Mapping ADN Post Entities in Core Data using MagicalRecord


I'm having an issue mapping posts from ADN into my database.

Please keep in mind I am using MagicalRecord to automatically map the JSON to the NSManagedObjects I've setup.

The JSON can be seen here: http://web.archive.org/web/20170123035402/https://developers.app.net/reference/resources/post/, specifically the section with the entities.

My Core Data model has been setup with the following relationships:

Post <-> Entities <->> Links/Hashtags/Mentions

The relationships have been setup with the proper names so MagicalRecord should be able to map down the tree just fine. The Post object has a bunch of attributes. Its relationship to the Entities object is called "entities" and this is where it seems to fail because of the primary key (AFAIK from where it breaks in the code in MagicalRecord).

Due to the fact that Entities is basically just an object to hold the relationships to each entity type, I don't have any attributes for the Entity object. Unfortunately it doesn't seem to help setting an attribute with a random name for the Entity object.

Based on the above information, is there anything you would do differently to map the relationships and objects properly? Is it OK to have this empty Entities object that I just use for the relationships to each entity type, sort of how the ADN post has been setup with the "entities" dictionary.


Solution

  • So the issue does lie a bit with MagicalRecord and myself for the way I had set up the NSManagedObjects and relationships.

    MagicalRecord uses a primary key to handle the relationships. If one hasn't been set it uses one of the attributes. In my case the Entities NSManagedObject did not have any attributes because I am just using it to handle the relationships to each of the entity types.

    The code for this can be found in NSObject+MagicalDataImport.m:

    - (NSString *) MR_lookupKeyForRelationship:(NSRelationshipDescription *)relationshipInfo
    {
        NSEntityDescription *destinationEntity = [relationshipInfo destinationEntity];
        if (destinationEntity == nil) 
        {
            MRLog(@"Unable to find entity for type '%@'", [self valueForKey:kMagicalRecordImportRelationshipTypeKey]);
            return nil;
        }
    
        NSString *primaryKeyName = [relationshipInfo MR_primaryKey];
    
        NSAttributeDescription *primaryKeyAttribute = [[destinationEntity attributesByName] valueForKey:primaryKeyName];
        NSString *lookupKey = [[primaryKeyAttribute userInfo] valueForKey:kMagicalRecordImportAttributeKeyMapKey] ?: [primaryKeyAttribute name];
    
        return lookupKey;
    }
    

    Specifically [[destinationEntity attributesByName] valueForKey:primaryKeyName]; returning nil.

    At first I had tested with a attribute called "test", but I am not sure what I had done that didn't work, perhaps not done a good enough job clearing the cache. I tried again to clear the relationships so I didn't have those to worry about and then I added an attributes "entities" on the Entities NSManagedObject and it all worked great!

    Sorry. Just adding the answers and comments in case anyone ever comes across this issue where they are using MagicalRecord and the NSManagedObject does not have any attributes.