RestKit: change RKObjectManager base URL

Can I change the base URL of RKObjectManager after creation?

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

Is there a similar solution to my problem?

Thanks.

+6
source share
4 answers

Just spent some time figuring out how to do this in v0.20. From what I can say, you cannot directly change the base URL without entering the source code of AFNetworking. You can create a new HTTPClient and install it, but I found that it caused even more problems, apparently because RestKit does additional configuration in AFNetworking HTTPClient when configuring RKObjectManager , as well as directly configuring the client is missing.

I came up with this solution, which should create another RKObjectManager with a new baseURL and re-add the descriptors. You will also need to set serialization types and headers 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

+12
source

Here's how to change RestKit baseURL after init:

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

Notification from a RestKit document:

Changing baseURL has a side effect that causes the requestCache instance to be rebuilt. Caches are supported on a per-node basis.

I use it and it works great :)

+5
source

Be careful!

I used the accepted answer for RestKit 2.0, but unfortunately this led to difficult debugging of the failure in restkit mode. What worked was simply to encapsulate the entire creation of the descriptor and other parameters of the object object into the method and call it using the new object manager object before calling [RKObjectManager setSharedManager: newManager];

+2
source

I do not think there is a solution for this. RestKit uses the base URL inside and cannot be changed.

+1
source

Source: https://habr.com/ru/post/915204/


All Articles