2013-01-10 18 views
5

Staram się, aby RestKit i CoreData współpracowały ze sobą. Jestem coraz bliżej, ale ja otrzymuję następujący błąd:Encja (null) nie jest kluczem do kodowania klucza zgodnego z kluczem "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".' 

Wydaje mi się, że to znalezienie moją klasę Book powodzeniem, a to ma właściwość tytułowy. Co ja robię źle?


Books.xcdatamodel

Book 
    title: String 

Mam URL w localhost:3000/books/initial która zwraca następujące (JSON)

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

używam mogenerator stworzyć moje zajęcia. Nie dodałem nic do Book, ale _Book wyraźnie ma zdefiniowaną właściwość tytułu.

Wreszcie, oto kod, którego używam do załadowania żądania.

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 dodaje następujące linie tuż przed //Send Request części, znalezionego w aplikacji RKTwitterCoreData, ale wciąż ten sam błąd

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

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

Odpowiedz

4

Problem polegał na tym, że ścieżka nie była poprawna w odwzorowaniu. Miałem http://localhost:3000/ jako moją domenę, gdzie powinno być http://localhost:3000 i miałem books/initial/ jako ścieżkę, w której powinno być /books/initial/.

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

Ja też zapomniałem stworzyć trwałe sklep. Oto pełny przykład:

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

nie widzę gdzie dodano magazyn trwały. Zapomniałeś to zrobić?

+0

To musi być to, czego mi brakuje. Próbuję znaleźć go w aplikacji Twitter. Wszystko, co mam, to powyższy + domyślny szablon danych podstawowych. –

+0

Dodałem 'createManagedObjectContexts' ale nie widzę gdzie przykład używa' addPersistentStore'. Nadal nie działa –