4
votes

I'm using RestKit to convert a nested core data model into a JSON file and upload it to a web service. Then I'm trying to get request the same JSON back and re-inflate it into a core data object. I'm getting this error during inflation:

 CoreData: error: Failed to call designated initializer on NSManagedObject class 'AppUser' 
    2012-04-26 10:25:50.850 DropboxSync[3824:5843] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<AppUser 0x2b7820> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "lastName".'

The class has a lastName property:

@property (nonatomic, retain) NSString * firstName;
@property (nonatomic, retain) NSString * lastName;
@property (nonatomic, retain) NSString * localDataFilepath;
@property (nonatomic, retain) NSSet *events;
@property (nonatomic, retain) AppUserWrapper *wrapper;

**What could be causing this?** 

I got 3 entities defined in my Core Data model:

User Event Images

user has multiple events, events have multiple images

I've defined a RestKit object mapping as defined below.

-(void)setupObjectMapping { RKObjectManager *objectManager = [RKObjectManager sharedManager ] ; RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Article class]];

    [mapping mapAttributes:@"articleID", @"title", @"body", nil];

    //********************************    

    RKObjectMapping *imageMapping = [RKObjectMapping mappingForClass:[ImageEntity class]];

    [imageMapping mapAttributes:@"createDate", @"localFilePath", nil];
    [objectManager.mappingProvider addObjectMapping:bleedImageMapping];
    [objectManager.mappingProvider setSerializationMapping:[imageMapping inverseMapping] forClass:[ImageEntity class]];
    [objectManager.mappingProvider imageMapping forKeyPath:@"images"];

    //********************************    

    RKObjectMapping *eventMapping = [RKObjectMapping mappingForClass:[Event class]];

    [eventMapping mapAttributes:@"createDate", @"severity", nil];
    [eventMapping mapRelationship:@"images" withMapping:imageMapping];

    [objectManager.mappingProvider addObjectMapping:eventMapping];
    [objectManager.mappingProvider setSerializationMapping:[eventMapping inverseMapping] forClass:[Event class]];
    [objectManager.mappingProvider setMapping:eventMapping forKeyPath:@"bleedEvents"];

    //******************************** 
    //setup App user mapping
    RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[AppUser class]];

    [userMapping mapAttributes:@"firstName", @"lastName", nil];
    [userMapping mapRelationship:@"events" withMapping:eventMapping];
    [objectManager.mappingProvider addObjectMapping:userMapping];
    [objectManager.mappingProvider setSerializationMapping:[userMapping inverseMapping] forClass:[AppUser class]];
    [objectManager.mappingProvider setMapping:userMapping forKeyPath:@"appUser"];

    //******************************** 
    //setup App user wrapper mapping
    RKObjectMapping *userWrapperMapping = [RKObjectMapping mappingForClass:[AppUserWrapper class]];


    [userWrapperMapping mapRelationship:@"appUser" withMapping:userMapping];
    [objectManager.mappingProvider addObjectMapping:userWrapperMapping];
    [objectManager.mappingProvider setSerializationMapping:[userWrapperMapping inverseMapping] forClass:[AppUserWrapper class]];
    [objectManager.mappingProvider setMapping:userWrapperMapping forKeyPath:@"appUserWrapper"];


}

Here's how I convert the nested data structure to JSON:

-(void)convertTestUserToJSON
{

    NSString* fullPath = [[$ documentPath] stringByAppendingPathComponent:@"sampleJSONFolder"];
    [[NSFileManager defaultManager] createDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes:nil error:nil];
    // Make the file

    NSString* fullFilepath = [NSString stringWithFormat:@"%@_%@.json",appUser.firstName,appUser.lastName];
    NSString* dataFile = [fullPath stringByAppendingPathComponent:fullFilepath];
    appUser.localDataFilepath = dataFile;
    [[AppUser managedObjectContext] save:nil];


    NSError* error = nil;


    RKObjectMapping *serMap = [[[RKObjectManager sharedManager] mappingProvider] serializationMappingForClass:[AppUser class]];
    NSDictionary *d = [[RKObjectSerializer serializerWithObject:appUser mapping:serMap] serializedObject:&error];


    if(error!=nil)
    {
        NSLog(@"!!!!! Error: %@",[error localizedDescription]);
    }

   //this is where the JSON is generated
    NSString* dataContents = [d JSONString];
  BOOL success =  [dataContents writeToFile:dataFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if(!success)
    {
        NSLog(@"Error writing to data file!");
    }
}

Here's the result of the JSON conversion

{"firstName":"First Name0","events":[{"severity":0,"images":[{"createDate":"2005-04-21 08:28:47 +0000","localFilePath":"localPhoto#10.png"},{"createDate":"2009-12-19 07:26:54 +0000","localFilePath":"localPhoto#11.png"}],"createDate":"2003-05-25 15:32:53 +0000"},{"severity":1,"images":[{"createDate":"2008-08-02 19:40:14 +0000","localFilePath":"localPhoto#10.png"},{"createDate":"2007-01-12 05:50:27 +0000","localFilePath":"localPhoto#12.png"},{"createDate":"2007-05-28 12:19:39 +0000","localFilePath":"localPhoto#11.png"}],"createDate":"2001-05-28 23:38:23 +0000"},{"severity":2,"images":[{"createDate":"2002-10-09 19:47:39 +0000","localFilePath":"localPhoto#10.png"},{"createDate":"2008-08-17 03:21:12 +0000","localFilePath":"localPhoto#11.png"}],"createDate":"2005-10-18 03:40:52 +0000"}],"lastName":"Last Name83"}

When I'm trying to inflate this JSON back into a nested core data entity, I get an error:

CoreData: error: Failed to call designated initializer on NSManagedObject class 'AppUser' 
2012-04-26 10:25:50.850 DropboxSync[3824:5843] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<AppUser 0x2b7820> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "lastName".'  

It appears that the inflater does not instantiate all relationships before attempting mapping. Am I wrong? What could be causing this to happen? I've tried to re-inflate my JSON in two different ways, and both times I got this error. How can it be resolved?

Thank you for any input!

1
Have you found out any reason for this? I am facing similar trouble. Please update your solutions with the answer.Raj Pawan Gumdal

1 Answers

4
votes

The problem is using RKObjectMapping instead of RKEntityMapping when dealing with Core Data, so RestKit's instantiating an NSManagedObject as regular NSObject.