2013-01-10 27 views
5

कुंजी कुंजी कोडिंग-अनुरूप नहीं है, मैं एक साथ काम करने के लिए 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".' 

यह है कि यह मेरी पुस्तक वर्ग सफलतापूर्वक खोजने है, और यह करता है एक शीर्षक संपत्ति है मुझे लगता है। मैं क्या गलत कर रहा हूं?


Books.xcdatamodel

Book 
    title: String 

मुझे लगता है कि निम्नलिखित (JSON) रिटर्न localhost:3000/books/initial पर एक यूआरएल

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

संपादित करें: मैं सही //Send Request हिस्सा है, RKTwitterCoreData एप्लिकेशन में पाया से पहले निम्नलिखित लाइनों जोड़ा है, लेकिन मैं अभी भी एक ही त्रुटि मिलती है

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

यह वही होना चाहिए जो मुझे याद आ रहा है। मैं इसे ट्विटर एप में ढूंढने की कोशिश कर रहा हूं। मेरे पास उपरोक्त + डिफ़ॉल्ट कोर डेटा टेम्पलेट है। –

+0

मैंने 'createManagedObjectContexts' जोड़ा लेकिन मुझे नहीं पता कि उदाहरण 'addPersistentStore' का उपयोग कहां करता है। यह अभी भी काम नहीं करता है –