0

I'm posting to the web services a string that represents store ID and I'm receiving data in the following format:

{
    "status": {
        "code": 201,
        "message": "Session created"
    },
    "session": "a1e0f68e82ca4d0095d4b2a9582c7e21",
    "store": {
        "id": 62,
        "code": "123",
        "name": "ABCD",
        "address1": "address line 1",
        "address2": "address line 2",
        "currencyCode": "USD",
    },
    "employees": [
        {
            ...
        }
    ]
}

I've passed on some response descriptors like so:

For session object:

RKObjectMapping *sessionMapping = [RKObjectMapping mappingForClass:[Session class]];
[sessionMapping addAttributeMappingsFromDictionary:@{@"session" : @"token"}];
RKResponseDescriptor *sessionDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:sessionMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:201]];

For store object:

RKEntityMapping *storeMapping = [RKEntityMapping mappingForEntityForName:@"Store"
                                                    inManagedObjectStore:[[DataModel sharedDataModel] objectStore]];
[storeMapping addAttributeMappingsFromDictionary:@{@"code" : @"code", @"id" : @"uuid", @"name" : @"name", @"address1" : @"address1", @"address2" : @"address2", @"currencyCode" : @"currencyCode"}];
storeMapping.identificationAttributes = @[@"code"];
storeMapping.deletionPredicate = [NSPredicate predicateWithFormat:@"deleted = true || open = false"];
RKResponseDescriptor *storeDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:storeMapping method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];                                                                             

And similar as above for employee objects.

Then this is how I run the operation:

[[RKObjectManager sharedManager] addResponseDescriptorsFromArray:@[sessionDescriptor, storeDescriptor, usersDescriptor]];

RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] appropriateObjectRequestOperationWithObject:nil method:RKRequestMethodPOST path:myPath parameters:parameters];
operation.targetObject = nil; //as per https://github.com/RestKit/RestKit/wiki/Object-mapping
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) 
{
    successBlock(mappingResult);
}  failure:nil];
[[RKObjectManager sharedManager] enqueueObjectRequestOperation:operation];

The issue is that while Session and Employees are built fine, I cannot get the store object to show up. Nothing is mapped!

Furthermore, the stores actually already all insist as objects from a previous call, but they only have a code and a name. This call is supposed to populate the remaining fields.

The second part of my question is that obviously in the response the employees and the store are both root-level objects. The store object also has employees as a property. So somehow I need to map what comes back in the employees array to the store object. Is there a way to do this with a descriptor or do I need to manually do this in the success completion block?

jancakes
  • 456
  • 4
  • 10

1 Answers1

1

Your store response descriptor should have the key path set to store. Also, the response descriptor is linked to the RKRequestMethodGET method, but you make the server request with RKRequestMethodPOST.

Note that you probably shouldn't be using deleted in your predicate (take caution with naming clashes with managed objects).

Look at foreign key mapping to connect relationships between non-nested content.

Community
  • 1
  • 1
Wain
  • 118,658
  • 15
  • 128
  • 151
  • Hm, thanks for the reply. I tried setting the key path, but that didn't work. Thanks for the link for how to link two entities though. Upvote for that :)) – jancakes Jun 25 '14 at 08:47