2013-01-10 9 views
5

Sto cercando di far funzionare RestKit e CoreData. Mi sto avvicinando, ma sto ottenendo il seguente errore:L'entità (null) non è la chiave di codifica del codice compatibile per la chiave "titolo"

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

Mi sembra che si tratta di trovare la mia classe Libro con successo, e lo fa avere una proprietà titolo. Che cosa sto facendo di sbagliato?


Books.xcdatamodel

Book 
    title: String 

Ho un URL a localhost:3000/books/initial che restituisce il seguente (JSON)

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

sto usando mogenerator per creare le mie classi. Non ho aggiunto nulla a Book, ma con _Book è chiaramente definita la proprietà del titolo.

Infine, ecco il codice che utilizzo per caricare la richiesta.

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: ho aggiunto le seguenti righe a destra prima parte //Send Request, trovato nel RKTwitterCoreData app, ma ho ancora ottenere lo stesso errore

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

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

risposta

4

Il problema era che il percorso non era corretto nella mappatura. Avevo il http://localhost:3000/ come dominio, dove avrebbe dovuto essere http://localhost:3000 e avevo books/initial/ come percorso in cui avrebbe dovuto essere /books/initial/.

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

Ho anche dimenticato di creare l'archivio persistente. Ecco l'esempio operativo completo:

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

non vedo dove hai aggiunto un archivio persistente. Ti sei dimenticato di farlo?

+0

Questo deve essere quello che mi manca. Sto cercando di trovarlo nell'app Twitter. Tutto quello che ho è sopra + il modello di dati di base predefinito. –

+0

Ho aggiunto 'createManagedObjectContexts' ma non vedo dove l'esempio utilizza' addPersistentStore'. Ancora non funziona –

Problemi correlati