3

So the problem is that when I'm trying to load entity from here I'm not getting things to work. My Pathpatterns seems to be wrong. Here's my Mapping and Descriptor:

RKEntityMapping *statsMapping = [RKEntityMapping mappingForEntityForName:@"Stat" inManagedObjectStore:managedObjectStore];

[statsMapping addAttributeMappingsFromDictionary:@{
 @"sort_id" : @"sortID",
 @"id" : @"statID",
 @"deleted" : @"deletedFlag",
 @"created_at": @"createdAt",
 @"updated_at": @"updatedAt"
 }];
statsMapping.identificationAttributes = @[ @"statID" ];
[statsMapping addAttributeMappingsFromArray:@[ @"title"]];


RKEntityMapping *featuresMapping = [RKEntityMapping mappingForEntityForName:@"Feature" inManagedObjectStore:managedObjectStore];

[featuresMapping addAttributeMappingsFromDictionary:@{
 @"sort_id" : @"sortID",
 @"id" : @"featureID",
 @"deleted" : @"deletedFlag",
 @"created_at": @"createdAt",
 @"updated_at": @"updatedAt",
 }];
featuresMapping.identificationAttributes = @[ @"featureID" ];
[featuresMapping addAttributeMappingsFromArray:@[ @"title", @"value"]];
[statsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"features" toKeyPath:@"features" withMapping:featuresMapping]];

RKResponseDescriptor *statsDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:statsMapping
                                                                                  pathPattern: @"/api/cars/:carID/features.json"
                                                                                      keyPath:nil
                                                                                  statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptorsFromArray:@[newsDescriptor, catalogDescriptor, statsDescriptor]];

So when I use pathPattern:nil it works, but if no answer is returned by url it just tries to put another responsedescriptor to the response and gives me random data :)

The question is, if I have the car ID in the middle of the pattern, how should I declare it?

Thank you!

Edit1: This is how I do request:

    - (void)getStats:(NSNumber *)carID
    {
    [[RKObjectManager sharedManager] getObjectsAtPath:[NSString stringWithFormat:@"api/cars/%@/features.json", carID]
                                           parameters:@{@"auth_token" : [Constants authToken]}
                                              success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                                  RKLogInfo(@"Load complete: Stats loaded");
                                              }
                                              failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                                  RKLogError(@"Load failed with error: %@", error);
                                                  [self showError:error withDelay:3];
                                              }];
}
Vivienne Fosh
  • 1,751
  • 17
  • 24
  • Your path pattern of @"/api/cars/:carID/features.json" looks ok. How are you making the request? And when using that path pattern are you getting no mapping done at all? – Wain Jul 02 '13 at 12:12
  • As I said, when I use this descriptor with pathPattern:nil everything is ok, and mapped fine, but I don't want to use this way. – Vivienne Fosh Jul 02 '13 at 12:20
  • Turn on trace logging and see what it says: `RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);` – Wain Jul 02 '13 at 12:24
  • Been there, did that. It says: *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "updatedAt".' – Vivienne Fosh Jul 02 '13 at 18:22
  • http://pastebin.com/DWCa4dfx - whole tracelog – Vivienne Fosh Jul 02 '13 at 18:30

1 Answers1

3

From your trace log there is some problem with your managed objects, not your mappings. Your entities are obviously defined in the model and with the correct names and with the appropriate attributes. So it looks like either you created classes for the entities but wrongly or that the object manager isn't being provided with an / the correct object store reference.


Your log contains CoreData: error: Failed to call designated initializer on NSManagedObject class 'Stat' and [<Stat 0xa68b7c0> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "statID". which are both issues to do with the entity creation / referencing. It isn't clear how that happens based on the code posted.

Try changing your path pattern to remove the leading slash:

@"api/cars/:carID/features.json"

when defining your statsDescriptor as that can cause the pattern to not match.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I have doubts on that because of 2 facts: my entities work great if I do not use pathPattern. All other descriptors also work great, so it is not a store reference unfortunately. – Vivienne Fosh Jul 03 '13 at 07:49
  • Did that, now it says: failed to match: response path '/api/cars/2/features.json?auth_token=Mg7Kd7pb1CG9dBZmx4Q9' did not match the path pattern 'api/cars/:carID/features.json'., NSLocalizedDescription=No response descriptors match the response loaded., keyPath=null, NSErrorFailingURLKey=http://dealer-app.net/api/cars/2/features.json?auth_token=Mg7Kd7pb1CG9dBZmx4Q9, NSUnderlyingError=0xa2a7630 "No mappable object representations were found at the key paths searched."} – Vivienne Fosh Jul 03 '13 at 09:06
  • Then the slash is required and I'm currently at a loss to explain what you're seeing. Are you using the latest version of the master / development branch? – Wain Jul 03 '13 at 09:37
  • Sure I do, I just have worries about this /:carID/ guy. I replaced it with /2/ but nothing helps for now ( – Vivienne Fosh Jul 03 '13 at 19:54
  • Do you have other response descriptors which also use path patterns, or which use a `nil` path pattern? What are they? – Wain Jul 04 '13 at 20:54
  • well eventually I checked all paths and it turned out that it uses 2 '/' instead of one... How stupid of me – Vivienne Fosh Jul 05 '13 at 13:52