Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestKit: change base URL of RKObjectManager

Is it possible to change base URL of RKObjectManager after creation?

I have login box and from nickname I decide which URL for API I should use. If I create RKObjectManager after filling nick/password, I can make only single call from RestKit ( https://groups.google.com/forum/?fromgroups#!topic/restkit/wFNhpCW-URA ). If I create RKObjectManager viewDidLoad function - I cannot change URL.

Is there same solution to my problem?

Thanks.

like image 689
kraag22 Avatar asked May 08 '12 14:05

kraag22


2 Answers

Just spent a while figuring out how this can be done in v0.20. From what I can tell, you can't directly change the base URL without getting into the AFNetworking source code. You can create a new HTTPClient and set it, but I found that this caused even more problems, presumably because RestKit is doing some additional configuration on AFNetworking's HTTPClient when you setup the RKObjectManager, and by setting the client directly you are missing out on that.

I came up with this solution, which is to create another RKObjectManager with the new baseURL and re-add the descriptors. You'll also need to set your serialization and header types again.

NSString *urlString = @"http://www.something.com/api";
RKObjectManager *newManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:urlString]];
[newManager setRequestSerializationMIMEType:RKMIMETypeJSON];
[newManager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];
[newManager addResponseDescriptorsFromArray:[RKObjectManager sharedManager].responseDescriptors];
[newManager addRequestDescriptorsFromArray:[RKObjectManager sharedManager].requestDescriptors];
[RKObjectManager setSharedManager:newManager];

Related documentation: Using Multiple Base URLs in RestKit

like image 184
Kyle Clegg Avatar answered Oct 26 '22 23:10

Kyle Clegg


Here is the way to change the RestKit baseURL after init:

[RKObjectManager sharedManager].client.baseURL = [RKURL URLWithString:newBaseUrlString];

Notice from RestKit doc:

Changing the baseURL has the side-effect of causing the requestCache instance to be rebuilt. Caches are maintained a per-host basis.

I use it and it works fine :)

like image 40
Antoine Beloeuvre Avatar answered Oct 27 '22 01:10

Antoine Beloeuvre