0

I'll start by saying that I'm really new (about two days now) to iPhone dev and Objective-C. I'm still getting used to the syntax, memory management, etc.

I'm trying to use RestKit to interact with a server which allows JSON REST requests. After issuing a GET request I get data of the form:

GET : /api/beast/1/
{
    'species' : 'elephant',
    'resource_uri' : 'api/beasts/1/',
    'owner' : '/api/beastmaster/3/',
    'name' : 'Stampy'
}


GET : /api/beastmaster/3/
{
    'resource_uri' : '/api/beastmaster/3/'
    'first_name' : 'Bart',
    'last_name' : 'Simpson'
}

The thing is that the owner property of the beast objects is sometimes populated with the resource URI string and sometimes with an actual full json representation of the object, as follows:

{
    'species' : 'elephant',
    'resource_uri' : 'api/beasts/1/',
    'owner' :     {
        'resource_uri' : '/api/beastmaster/3/'
        'first_name' : 'Bart',
        'last_name' : 'Simpson'
    },
    'name' : 'Stampy'
}

What I want to do is to provide an easy to use interface to request the owner property asynchronously, it should check whether it already has a full representation of the object, and in that case execute the callback immediately or, if it doesn't, issue the appropriate GET request and execute the callback when the response arrives.

If this was JavaScript, some ways to achieve this may be:

//Alternative 1
beast.getOwner(function(owner){
   console.log("Owner is: " + owner);
});

//Alternative 2
 beast.get("owner", {
     'success' : function(){...},
     'error' : function(){...}
});

Most RestKit examples I've seen implement the protocol to handle the response on the same object that executes the request. I don't like this because in this case one class may require various related object properties (which would be obtained asynchronously).

What would be the best way to achieve the desired behaviour providing a simple and clear interface for the other programmers which would be using the model classes to develop the rest of the app? Maybe using blocks?

BenMorel
  • 34,448
  • 50
  • 182
  • 322

1 Answers1

0

Well, as far as I can see from your JSON response, there has been a design irregularity. It seems complex enough to put 1-2 hour extra research and 1-2 hour extra coding just for that. My fast solution is to change the model view to have a consistent value whether owner : "uri" or owner : { ownerObjectProperty: value }.

The slow solution would be, you would have 2 values in your mapped object. 1st one would be NSString *rawValueForOwner, 2nd is OwnerObject* ownerObject.

You would map owner JSON value into rawValueForOwner mapped object via protocols. Parsing the value and deducting if it starts with "{" then parse that rawValueForOwner into an object via RestKit manual parsing methods. If it doesn't start with "{" then make an another call to your RESTful server to fetch the object then again link beast and owner into each other manually.

I would have taken the high way :)

This link contains how to parse JSON string into an object... Deserializing local NSString of JSON into objects via RestKit (no network download)

Community
  • 1
  • 1
Hayati Guvence
  • 718
  • 2
  • 6
  • 22
  • It's not EXACTLY as I've described it. A given URL will always return the json in the same format, the thing is that it only goes one level deep. So if a `beastmaster` has, say... a `favoriteBeast` property. Then _that_ representation of the `beast` will show the `owner` as an URI. But if the top-level object is a `beast`, then the `owner` property will be a full json object. This is to avoid infinite circular references on the json. BTW, thanks for the link, that's going to be helpful. – Juan Enrique Muñoz Zolotoochin Mar 13 '12 at 00:55