0
votes

I'm trying to use RestKit just to map local JSON to my Core Data entity, as I'm dealing with authenticating to an API and already am using another library to do that, so I'd just be passing RestKit the array of JSON data (which I've verified I'm receiving correctly). I already have all the basic necessary components set up (data model/entity, NSManagedObject class, managed object context, etc.), as I'm adapting an existing app I wrote that used RestKit to access a RSS feed and map it into Core Data.

From researching, it seems that I should use RKManagedObjectMappingOperationDataSource to accomplish this, and have adapted an example I found in the unit tests for this:

-(void)performMapping
{
    FoodTruck *foodTruck = FoodTruck.new;
    RKEntityMapping *mapping = [ObjectMappings FoodTruckArticleMapping];
    RKManagedObjectStore *store = [[FoodTruckDataModel sharedDataModel] objectStore];
    RKManagedObjectMappingOperationDataSource *mappingDS = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:store.mainQueueManagedObjectContext cache:store.managedObjectCache];
    mappingDS.operationQueue = [NSOperationQueue new];
    RKMappingOperation *operation = [[RKMappingOperation alloc] initWithSourceObject:self.moreStatuses destinationObject:foodTruck mapping:mapping];
    operation.dataSource = mappingDS;
    NSError *error = nil;
    [operation performMapping:&error];
    [mappingDS.operationQueue waitUntilAllOperationsAreFinished];
}

My object mappings are done in a separate mappings class and look like this:

+(RKEntityMapping *)FoodTruckArticleMapping
{
    RKEntityMapping *jsonMapping = [RKEntityMapping mappingForEntityForName:@"FoodTruck" inManagedObjectStore:[[FoodTruckDataModel sharedDataModel] objectStore]];
    jsonMapping.identificationAttributes = @[@"tweetID"];

    [jsonMapping addAttributeMappingsFromDictionary:@{
 @"text": @"tweet", @"user.screen_name": @"foodTruckName", @"id": @"tweetID", @"created_at": @"timeStamp"}];

    return jsonMapping;
}

My FoodTruckDataModel class looks like this:

+(id)sharedDataModel {
    static FoodTruckDataModel *__sharedDataModel = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        __sharedDataModel = [[FoodTruckDataModel alloc] init];
    });

    return __sharedDataModel;
}

- (NSManagedObjectModel *)managedObjectModel
{
    return [NSManagedObjectModel mergedModelFromBundles:nil];
}

-(id)optionsForSQLiteStore
{
    return @{
         NSInferMappingModelAutomaticallyOption: @YES,
         NSMigratePersistentStoresAutomaticallyOption: @YES
         };
}

-(void)setup
{
    self.objectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:[self managedObjectModel]];

    [self.objectStore createPersistentStoreCoordinator];

    NSString *path = [RKApplicationDataDirectory()     stringByAppendingPathComponent:@"FoodTruckxxx.sqlite"];
    NSLog(@"Setting up store at %@", path);
    [self.objectStore addSQLitePersistentStoreAtPath:path
                          fromSeedDatabaseAtPath:nil
                               withConfiguration:nil
                                         options:self.optionsForSQLiteStore
                                           error:nil];

    [self.objectStore createManagedObjectContexts];

    self.objectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:self.objectStore.persistentStoreManagedObjectContext];
}

However, something isn't being done correctly, as I'm getting this Core Data error:

2013-05-25 19:38:17.867 FoodTrucks[37340:c07] CoreData: error: Failed to call designated initializer on NSManagedObject class 'FoodTruck'

and I'm also getting a crash on this:

2013-05-25 19:38:17.887 FoodTrucks[37340:4903] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil'

It appears as if it's performing mapping operations on my data, but that it's not being passed my entity and mapping info, as can be seen from my log (JSON data erased). I'm not sure what I'm doing wrong, as it was working fine before when I was using RKManagedObjectRequestOperation.

If anyone has any suggestions or help, it'd be greatly appreciated!

1

1 Answers

1
votes

The cause of the first problem (and probably the second too) is caused by:

FoodTruck *foodTruck = FoodTruck.new;

As you are not permitted to instantiate managed objects like that. Instead you must use NSEntityDescription. The code you have at the moment doesn't create a valid instance so when you try and use it bad things happen.