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

痞子三分冷 提交于 2019-12-10 15:09:59

问题


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];

回答1:


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);
}];



回答2:


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



来源:https://stackoverflow.com/questions/14268580/the-entity-null-is-not-key-value-coding-compliant-for-the-key-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!