How to recursively load nested objects identified by foreign-key in RestKit?

45 views Asked by At

I have a bit of a unique problem. I've been reading RestKit docs for days and I'm pretty sure RK can do it, I'm just not sure how to set things up. When I first login my API returns a Contacts JSON that looks like below and I need to load the Group and Team that are referenced but NOT nested. If at all possible, I really need those loads to happen before the CoreData context gets saved.

Is it possible to configure RestKit to recurse these references automatically? If not, any suggestions on a good general approach?

{
    "contactid": "003i0000016aiI8AAI",
    "signed_waiver": "1",
    "current_groupid": "T1Gj6mxcoXposmVUAA",
    "current_teamid": "T1Gj6mxcoXposmVU11",
    "createdby": "003i000001M4Y6fAAF",
    "created": "Fri, 16 Jan 2015 21:14:07 +0000",
    "updatedby": "003i0000016aiI8AAI",
    "updated": "Sat, 13 Jun 2015 15:13:35 +0000"
}

If the waiver, current_groupid, or current_teamid is not set I need to display an on boarding wizard, if they then I proceed directly into the app which then requires loading details about the group and team (name, geometry, members, etc.). I can't change the server API to nest this structure directly.

I've been able to get my CoreData entity Contact to map and save. I struggled mightily with the current_groupid relationship and finally got it to not crash when I found 1787 and followed its advice. However, it's still getting saved as nil so I'm still doing something wrong...

    let mapping = RKEntityMapping(forEntityForName: "Contact", inManagedObjectStore: rkManagedObjectStore)
    mapping.identificationAttributes = ["apiID"]
    mapping.addAttributeMappingsFromDictionary([
        "contactid":        "apiID",
        "signed_waiver":    "signedWaiver",
        "current_groupid":  "rkCurrentGroupId",
        ])
    mapping.addConnectionForRelationship("currentGroup", connectedBy: ["rkCurrentGroupId": "apiID"])

For clarity, my Contact entity has String attributes for all of the above and a relationship for currentGroup that connects to Group.currentCanvassers.

1

There are 1 answers

0
Wain On BEST ANSWER

No, it can't do it. RestKit doesn't automatically trigger any downloads, that's up to your code. RestKit takes your request and encodes it, then handles the response to decode it, performing mapping as required in both cases.

You could trigger automatic downloads in your code handling the completion or, more messily, in some of the other callbacks, but you can't make the save wait for them to complete.