This question is about wrapping RestKit requests in custom objects, and whether or not I can guarantee that an object sending the RestKit request will also be used as a delegate when the response is returned.
I’m working with an abstruse REST API (Salesforce), and RestKit doesn’t appear to handle some of Salesforce’s peculiarities well — for example, when you make a call to the Salesforce REST API, you might get back a partial dataset, and then be given a “nextRecordsUrl” that you must follow to get the next chunk. Others working with Salesforce and REST have run into the same issue (ref https://groups.google.com/forum/#!msg/restkit/HJNjB7I6WVM/8u5n7nHoJQUJ). I created a class to wrap the calls to Salesforce and automatically follow these links, and process the returned data sets correctly. The class instance registers itself as the delegate for the REST call.
The class I wrote works great on its own (when there’s a single instance), but I’ll be creating other classes and have several instances that will need to handle their data independently. I’ve read through the documentation and code (it’s been a learning experience, I’ve only worked with ios for about a month now), but I can’t see if it’s guaranteed that a particular instance will respond as the delegate for its requests alone, or if it might accidentally pick up the responses for another instance.
For example, suppose I have instances A and B, both of which are making requests using RKObjectLoader, and which set themselves as delegates using loader.delegate = self;. Both are running simultaneously (asynchronous). Here’s a potential flow I see:
- A’s RKObjectLoader makes call, registers self as delegate for response
- RestKit framework sends request for A
- B’s RKObjectLoader makes call, registers self as delegate for response
- RestKit framework sends request for B
- … wait …
- RestKit framework gets response, and somehow determines that this response was from B’s request, calling B as the delegate
- B delegate methods get called
- RestKit framework gets response, and somehow determines that this response was from A’s request, calling A as the delegate
- A delegate methods get called
I couldn’t see how the RestKit framework would distinguish between responses from requests, and thereby ensure that the correct delegate instances get called. In other words, it appears to me that RestKit might get a response to a request, and then call any delegate it wants … but that seems counterintuitive, and the framework author appears to be very sharp.
So, my questions:
- If I have a class instance using RKObjectLoader and setting itself as the delegate, can I be guaranteed that it will be called when the response for that request comes in?
- Can you point me to the actual implementation in the RestKit framework that handles this, so I can learn how it’s done?
Thanks very much!
I haven’t received an answer to this question, but in case somebody has the same question and comes across this, here’s the answer: yes, RestKit does appear to match the correct delegate with the outgoing request.
I created a small class (with an (NSString*) identifier property that calls a service, and in the delegates I printed out the instance’s identifier with the result. I confirmed that instance A’s delegate method was handling instance A’s call, and B’s was handling B’s, with several simultaneous calls.