2013-01-10 10 views
5

RestKitとCoreDataを連携させようとしています。私が近づいていますが、私は次のエラーを取得しています:エンティティ(null)がキー "title"のキーコードに準拠していません

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クラスを見つけることだし、それがタイトルのプロパティを持っているように私には思えます。私は間違って何をしていますか?


Books.xcdatamodel

Book 
    title: String 

私は、次の(JSON)を返しlocalhost:3000/books/initialのURLを持っている

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

私は私のクラスを作成するためにmogeneratorを使用しています。私はBookには何も追加されていないが、_Bookは、明確に定義されたタイトルのプロパティがあります。

最後に、リクエストをロードするコードを示します。

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:私は右RKTwitterCoreDataアプリで見つかった//Send Request一部、前に次の行を追加しましたが、私はまだ私は見ていない

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

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

答えて

4

問題は、マッピングでパスが正しくないことでした。私はhttp://localhost:3000であったはずの私のドメインとしてhttp://localhost:3000/を持っていて、それは/books/initial/だったはずのパスとしてbooks/initial/を持っていました。

は、私はまた、永続ストアを作成するのを忘れRestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class

を参照してください。

// 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

同じエラーが出ます永続ストアを追加した場所です。そうすることを忘れましたか?

+0

これは私が逃しているものでなければなりません。私はそれをTwitterアプリで見つけようとしています。私が持っているのは、上記+デフォルトのコアデータテンプレートです。 –

+0

私は 'createManagedObjectContexts'を追加しましたが、一例は、' addPersistentStore'を使用してどこ私は表示されません。それはまだ動作しません –