I'm trying to get RestKit and CoreData to work together. I'm getting closer, but I'm getting the following error:
CoreData: error: Failed to call designated initializer on NSManagedObject class 'Book'
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason:
'[<Book 0x8454560> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "title".'
It seems to me that it's finding my Book class successfully, and it DOES have a title property. What am I doing wrong?
Books.xcdatamodel
Book
title: String
I have a url at localhost:3000/books/initial
that returns the following (JSON)
[{title:"one"}, {title:"two"}]
I'm using mogenerator to create my classes. I haven't added anything to Book
, but _Book
clearly has the title property defined.
Finally, here's the code I use to load the request.
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://localhost:3000/"]];
RKManagedObjectStore* objectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:self.model];
objectManager.managedObjectStore = objectStore;
// Mappings
RKEntityMapping *bookMapping = [RKEntityMapping mappingForEntityForName:@"Book" inManagedObjectStore:objectStore];
[bookMapping addAttributeMappingsFromArray:@[@"title"]];
RKResponseDescriptor * responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:bookMapping pathPattern:@"books/initial/" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
// Send Request
[objectManager getObjectsAtPath:@"/books/initial/" parameters:nil success:^(RKObjectRequestOperation * operation, RKMappingResult *mappingResult) {
NSLog(@"SUCCESS");
} failure: ^(RKObjectRequestOperation * operation, NSError * error) {
NSLog(@"FAILURE %@", error);
}];
EDIT: I added the following lines right before the //Send Request
part, found in the RKTwitterCoreData
app, but I still get the same error
// Other Initialization (move this to app delegate)
[objectStore createPersistentStoreCoordinator];
[objectStore createManagedObjectContexts];
objectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:objectStore.persistentStoreManagedObjectContext];