0

I have a managed object MyEntity that I use without any problem throughout my application.

@interface MyEntity : NSManagedObject

@property (nonatomic, retain) NSString *code;
@property (nonatomic, retain) NSNumber *def_value;
@property (nonatomic, retain) NSString *label;
@property (nonatomic, retain) NSString *link_label;
@property (nonatomic, retain) NSNumber *order;

@end

I have a web service http://my_server/MyEntity which returns me:

[
    {
        CODE: "TYPE",
        LABEL: "Administrative",
        ORDER: "3",
        DEF_VALUE: "0",
        LINK_LABEL: ""
    },
    {
        CODE: "TOPIC",
        LABEL: "NDF",
        ORDER: "1",
        DEF_VALUE: "0",
        LINK_LABEL: "Administrative"
    },
    {
        CODE: "TOPIC",
        LABEL: "Report",
        ORDER: "2",
        DEF_VALUE: "1",
        LINK_LABEL: "Administrative"
    },
    {
        CODE: "TOPIC",
        LABEL: "Other",
        ORDER: "3",
        DEF_VALUE: "0",
        LINK_LABEL: "Administrative"
    }, ...
]

I'm trying to map and fetch this list

NSURL *baseUrl = [NSURL URLWithString: @"http://my_server"];
NSString *objName = @"MyEntity";
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
[RKManagedObjectStore setDefaultStore:managedObjectStore];
[managedObjectStore createPersistentStoreCoordinator];

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL: baseUrl];
RKEntityMapping *objMap = [RKEntityMapping mappingForEntityForName:objName inManagedObjectStore:managedObjectStore];
[objMap addAttributeMappingsFromDictionary: @{
     @"CODE": @"code",
     @"LABEL": @"label",
     @"ORDER": @"order",
     @"DEF_VALUE": @"def_value",
     @"LINK_LABEL": @"link_label",
 }];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objMap pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[objectManager addResponseDescriptor:responseDescriptor];
[objectManager getObjectsAtPath:objName parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"ok");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"ko");
}];

It's giving me this error

I restkit.network:RKHTTPRequestOperation.m:185 GET'http://my_server/MyEntity'(200 OK) CoreData: error: Failed to call designated initializer on NSManagedObject class 'MyEntity'
Terminating app due to uncaught exception 'NSUnknownKeyException', reason:
'[ valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "link_label".'

I saw this error on 2 or 3 SO questions but each with a different problem and seem not any of mine.

Can someone help me to debug that ?

Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
  • The earlier error, `Failed to call designated initializer on NSManagedObject class 'MyEntity'`, is a more immediate concern and is probably the cause of the key-value coding exception. – Tom Harrington May 21 '13 at 16:28

1 Answers1

2

You need to fully configure the objectManager:

objectManager.managedObjectStore = managedObjectStore;

You also aren't completing the Core Data stack initialisation:

[managedObjectStore createManagedObjectContexts];
Wain
  • 118,658
  • 15
  • 128
  • 151
  • This `cannot create managed object contexts: The persistent store coordinator does not have any persistent stores` – Pierre de LESPINAY May 22 '13 at 08:16
  • Called after createPersistentStoreCoordinator, yes? – Wain May 22 '13 at 08:45
  • I call `createPersistentStoreCoordinator` then `createManagedObjectContexts` – Pierre de LESPINAY May 22 '13 at 09:44
  • Have you debugged to ensure the managedObjectModel and everything after actually exists? – Wain May 22 '13 at 09:52
  • I use this model since months without needing 10 lines of configuration each time I access the data. Anyway this question was about "Not key value coding-compliant" so maybe I'll ask another question. Actually I think I'm going to implement my own object mapper, because RestKit seems too complex to me. Thank you for your answer. – Pierre de LESPINAY May 22 '13 at 09:59