2013-01-10 4 views
5

Ich versuche, RestKit und CoreData zusammenzuarbeiten. Ich bin immer näher, aber ich bin immer folgende Fehlermeldung:Die Entität (null) ist nicht Schlüssel-Kodierung-kompatibel für den Schlüssel "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".' 

Es scheint mir, dass es erfolgreich mein Buch Klasse zu finden, und es hat eine Titel-Eigenschaft. Was mache ich falsch?


Books.xcdatamodel

Book 
    title: String 

Ich habe eine URL an localhost:3000/books/initial, die die folgenden (JSON) gibt

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

ich mogenerator bin mit meinen Klassen zu erstellen. Ich habe nichts zu Book hinzugefügt, aber _Book hat eindeutig die Titeleigenschaft definiert.

Schließlich ist hier der Code, den ich benutze, um die Anfrage zu laden.

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: Ich habe die folgenden Zeilen direkt vor dem //Send Request Teil, in dem RKTwitterCoreData App gefunden, aber ich habe immer noch die gleichen Fehler

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

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

Antwort

4

Das Problem war, dass Pfad in der Zuordnung nicht korrekt war. Ich hatte http://localhost:3000/ als meine Domain, wo es hätte sein sollen http://localhost:3000, und ich hatte books/initial/ als der Pfad, wo es /books/initial/ gewesen sein sollte.

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

Ich habe vergessen, auch den persistenten Speicher zu erstellen. Hier ist das vollständige Arbeitsbeispiel:

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

ich nicht sehen, wo du einen persistenten Speicher hinzugefügt hast. Hast du das vergessen?

+0

Das muss sein, was ich vermisse. Ich versuche es in der Twitter App zu finden. Alles, was ich habe, ist das obige + die Standard-Kerndatenvorlage. –

+0

Ich habe 'createManagedObjectContexts' hinzugefügt, aber ich sehe nicht, wo das Beispiel' addPersistentStore' verwendet. Es funktioniert immer noch nicht –

Verwandte Themen