Zia
Zia

Reputation: 14722

Object Mapping with RestKit to CoreData

This code:

NSString* MIMEType = @"application/xml";
[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:MIMEType];

NSError* error = nil;
NSString* XMLString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:myLocallyStoredFileName ofType:@"xml"]
                                                encoding:NSUTF8StringEncoding
                                                   error:nil];
NSData* XMLData = [XMLString dataUsingEncoding:NSUTF8StringEncoding];
id parsedData = [RKMIMETypeSerialization objectFromData:XMLData MIMEType:MIMEType error:&error];
if (parsedData == nil && error) {
    // Parser error...
}

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[myCoreDataObject class]];
[mapping addAttributeMappingsFromDictionary:@{
                                             ... my mappings ...
                                                     }];

NSDictionary *mappingsDictionary = @{ [NSNull null]: mapping };
RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:parsedData mappingsDictionary:mappingsDictionary];
NSError *mappingError = nil;


NSPersistentStoreCoordinator *psc = ((MPTAppDelegate *)[UIApplication sharedApplication].delegate).persistentStoreCoordinator;
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:psc];

RKFetchRequestManagedObjectCache *cache = [RKFetchRequestManagedObjectCache new];

RKManagedObjectMappingOperationDataSource *source =
[[RKManagedObjectMappingOperationDataSource alloc]
 initWithManagedObjectContext:context
 cache:cache];

mapper.mappingOperationDataSource = source;
BOOL isMapped = [mapper execute:&mappingError];
if (isMapped && !mappingError) {
    NSLog(@"Error");
}

Gives me the following error:

CoreData: error: Failed to call designated initializer on NSManagedObject class 'myCoreDataObject'

It is as if the mapping operation does not know I am using Core Data. How do I fix this issue?

Upvotes: 0

Views: 195

Answers (1)

Wain
Wain

Reputation: 119041

That's because your mapping is an instance of RKObjectMapping where it should be an instance of RKEntityMapping to integrate properly with Core Data. When you create the mapping, you then supply the Core Data entity rather than the representative Class.


Also, you should really let RestKit manage the Core Data stack for you. To do that, create the stack something like:

NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:model];

NSError *error;
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"XXXX.sqlite"];

NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];

[managedObjectStore createManagedObjectContexts];

Then you have a fully configured managedObjectStore to use.

(As it stands at the moment you don't have a managedObjectStore, so from the code in your comment you're just passing nil).

Upvotes: 1

Related Questions