6

我试图让 RestKit 和 CoreData 一起工作。我越来越近了,但我收到以下错误:

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".'

在我看来,它成功地找到了我的 Book 类,并且它确实具有 title 属性。我究竟做错了什么?


书籍.xcdatamodel

Book
  title: String

localhost:3000/books/initial我有一个返回以下(JSON)的网址

[{title:"one"}, {title:"two"}]

我正在使用 mogenerator 创建我的课程。我没有添加任何东西Book,但_Book显然已经定义了 title 属性。

最后,这是我用来加载请求的代码。

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

编辑://Send Request我在应用程序中找到的部件之前添加了以下几行RKTwitterCoreData,但我仍然得到同样的错误

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

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

2 回答 2

4

问题是映射中的路径不正确。我有http://localhost:3000/作为我的域,它应该在哪里http://localhost:3000,我有books/initial/作为它应该在的路径/books/initial/

请参阅RestKit 0.20 — CoreData:错误:无法在 NSManagedObject 类上调用指定的初始化程序

我也忘了创建持久存储。这是完整的工作示例:

// 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);
}];
于 2013-01-11T18:22:17.210 回答
2

我看不到您在哪里添加了持久存储。你忘了这样做吗?

于 2013-01-11T01:53:51.910 回答