Take the 2-minute tour ×

--UPDATE: I've decided to give AFNetworking a try. Even though RestKit has a really nice object mapping functionality, the way the networking calls were designed have made some things difficult for us.

I'm hoping for some advice on how to organize my project that's using RestKit.

I have several REST calls from a repository class and its results get passed to controllers. For example I have a getProfile method in the repository class that is getting called from our browse view controller. The view controller is set as the delegate to the get profile calls while the repository class is set to the delegate for the restkit calls.

The problem is if the browse controller makes several get profile requests, it is difficult to distinguish which result should go to which delegate function since all the restkit calls share the same delegate method objectLoaderDidFinishLoading. I then have 4 delegates that I have to match the results of the 4 asynchronous restkit requests.

Is there any way I can use blocks so that I can pass a function to execute as the asnynrhounous result comes back so that I can assign a proper delegate? The block support that I saw allowed a block to be used before the request was sent out in rest kit but I am interested in using it for when the asynchronous result is returned.

The alternative of examining the results or setting user data and sleuthing what delegate goes with what asynchronous results seems unreliable and bulky.

share|improve this question
Have you seen this? github.com/gowalla/AFNetworking –  Colin Oct 21 '11 at 22:57
Hi Colin, it looks interesting, but we've already invested a fair amount of work into RestKit and are using it's object mapping as well.. –  MonkeyBonkey Oct 26 '11 at 13:57
Hi Colin, I'm curious, does AFNetworking support json to object mapping? –  MonkeyBonkey Nov 6 '11 at 13:31
I believe it does, since it has a dependancy on RESTKit. If not, that wouldn't be hard to roll yourself; many libraries exist to do that. –  Colin Nov 7 '11 at 3:21

4 Answers 4

You can solve your issues with disambiguating between your profile requests by using the userData opaque pointer on RKObjectLoader. That will allow you to hang any object you want on the request, which can then be used to help distinguish between multiple profile requests. Also, if those profile requests are going to different resourcePaths then you can just use the wasSentToResourcePath: method on RKObjectLoader to distinguish between them.

share|improve this answer
I didn't want to use the userData because they would essentially contain the same identical info, e.g. if I called getProfile for the same profile from 4 different sources, they would all have the same user data. There are workable workarounds by keeping track of which user data goes where, but it seems bulky.. a lot of switch and if else statements for similar rest kit requests. –  MonkeyBonkey Oct 28 '11 at 19:59
Hmmm, userData would not have the same data if you associated it with the sources, which is what @jeffarena was getting at. Store a UUID as userData in RKObjectLoader and also use that same UUID as a dictionary key whose value is the source. That makes it easy to disambiguate the responses. –  Rob Jones Apr 21 '12 at 3:42

I just stumbled upon this question while trying to figure out this problem for my own REST interface. I'm glad I did, I'll probably use RestKit now.

I digress, back to your question. As you noted it doesn't seem like the block argument in the RKObjectManager is meant to be used this way. Instead, how about writing a class that implements RKObjectLoaderDelegate, takes in a block, and calls that block on any of the delegate calls.

Maybe something like this?

@interface MyObjectLoaderDelegate : NSObject <RKObjectLoaderDelegate>

@property (nonatomic, copy) void (^callback)(RKObjectLoader *loader, NSDictionary *objectDictionary, NSError *error)

- (id)initWithCallback:(void (^)(RKObjectLoader*, NSDictionary*, NSError*)aCallaback;


And on any implemented delegate method you can execute the block. Since blocks retain scoped variables you can run code against the calling delegate.

Whatcha think?

share|improve this answer
Interesting approach and I tried out a version of it, but ran into an error since I don't have a strong reference to the MyObjectLoaderDelegate. My repo class basically had a objectloaderdelegate factory that I use to set as the delegate for a rest kit call. Unfortunately restkit does not retain a reference to the delegate, and the custom loader delegate is no longer referenced on object load. I have been working on maintaining an array of blocks in the repository anyways so maybe I'll work down that path. –  MonkeyBonkey Oct 28 '11 at 19:57

I am not sure using blocks is the right way to solve your issue.

How about having a class GetProfile that implements RKObjectLoaderDelegate. So you call the request from within here and set itself to be the delegate. Then you have an objectLoader per request.

So in your view controller, each time you what GetProfile, you create an instance. And then when that instance messages your controller back (via delegates?) you know which it is.

I am just grappling with this issue as well, so am keen to hear feedback.

share|improve this answer
I made a similar comment to an earlier suggestion, but it seems like the weak reference to that objectlaoderdelegate class means that by the time the restkit call comes back and calls the delegate, the delegate reference is gone. RestKit does not retain references to the delegates which means I'll have to retain references to the custom delegates in my repo, or some other class... doable but very bulky I think –  MonkeyBonkey Oct 28 '11 at 20:01
up vote 0 down vote accepted

Switching over to AFNetworking seems to be the way to go... it was a much easier implementation for what I needed.

share|improve this answer
Personally, I don't think it's good faith that a question was asked about RestKit, people tried their best to answer and in the end the right answer was marked as "Switched over to AF Networking". The whole point of asking a SO question is for the community to help in a technical question? –  daemonsy May 31 '12 at 3:45
But the question was whether one can use blocks, and the answer was no. Using userdata for disambugiation is not maintainable in any large project so AFNetworking seems to be the only alternative. –  MonkeyBonkey May 31 '12 at 22:06

Your Answer


