Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The entity (null) is not key value coding-compliant for the key "title"

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];
like image 331
Sean Clark Hess Avatar asked Jan 10 '13 22:01

Sean Clark Hess


2 Answers

The problem was that path was not correct in the mapping. I had http://localhost:3000/ as my domain, where it should have been http://localhost:3000, and I had books/initial/ as the path where it should have been /books/initial/.

See RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class

I also forgot to create the persistent store. Here's the full working example:

// Core Data Example
// Initialize RestKIT
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];

// Other Initialization (move this to app delegate)
[objectStore createPersistentStoreCoordinator];

NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"RKTwitter.sqlite"];
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"RKSeedDatabase" ofType:@"sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [objectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);

[objectStore createManagedObjectContexts];

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

// Send Request
[objectManager getObjectsAtPath:@"/books/initial/" parameters:nil success:^(RKObjectRequestOperation * operation, RKMappingResult *mappingResult) {
    NSLog(@"SUCCESS");
} failure: ^(RKObjectRequestOperation * operation, NSError * error) {
    NSLog(@"FAILURE %@", error);
}];
like image 88
Sean Clark Hess Avatar answered Sep 19 '22 00:09

Sean Clark Hess


I don't see where you added a persistent store. Did you forget to do so?

like image 22
Blake Watters Avatar answered Sep 21 '22 00:09

Blake Watters