Handling responses for difference POST in the same controller in RestKit - response

I want to write a method, in my method I need post two POST methods to
my web service and get data json returned for every method. How can I
process response for separate post?
Ex: in my view, I have:
[[RKObjectManager sharedManager] postObject:joeBlow
delegate:self]; (1)
[[RKObjectManager sharedManager] postObject:joeBlow_1
delegate:self]; (2)
How can I handle response for (1) and (2) in the same controller.
Thanks

not sure if I understood it right , but maybe use:
- (void)objectLoader:(RKObjectLoader*)loader willMapData:(inout id *)mappableData ?

Related

Post path must match exactly with RKResponseDescriptor pathPattern

So the current service I'm using for updating an object follows the formatting of the following URL
http://www.baseurl.com/servicemethodAddObject/transactionId?clientTime=clientTime
So this is the code I put in there. Roughly.
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:eventResponseMapping method:RKRequestMethodGET pathPattern:#"/GetStuff" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
// I'd want to add items to the URL but the parameters parameter only goes in to the post body, If I add objects to the path #"?GetStuff/?parm=Stuff&parm2=stuff2" then the response will not be mapped properly
[objectManager postObject:object path:#"/GetStuff" parameters:parms success:nil failure:nil];
It seems that RestKit requires that the path used to post the object and the pathPattern used to create the RKDescriptor must be an exact match. Is there a way to get restKit to do a comparison that ignores the url parameters for it's object mapping?
Is this even the right thing to do? Posting an object and adding items in the URL.
You can build the path to add your parameters. Using pathPattern:#"/GetStuff" will match against built paths like:
#"/GetStuff?parm=Stuff&parm2=stuff2"
(you will have issues if you add or remove / characters that aren't in your path pattern)
https://github.com/RestKit/RestKit/issues/1877
Apparently the solution is to not do it.

What is the best way in RestKit to send off two requests then do a action only after both return?

I have a number of API calls I need to send off and then want to do something only when they all return, like [SVProgressHUD dismiss];
What is the best way to do that?
It depends how you're making the requests, but RKObjectManager offers a number of options:
The RKObjectManager gives you access to the operationQueue on which the requests are running so you can check the operationCount.
The RKObjectManager also lets you enqueueBatchOfObjectRequestOperations:... with a progress and a completion callback.
Yet more, the RKObjectManager also lets you search for pending requests using enqueuedObjectRequestOperationsWithMethod:....
Create a mutable NSArray and add some identifier to it when you send the request, and delete it when you get a response, and when the array is empty you are done.

RestKit 0.20.0-rc1 - Duplicate object after POST

I'm running into an issue while trying to synchronize a list of favourited teams for a given user between my iOS app and my server. The flow of events is as follows:
User favourites a team
New favouriteTeam object is created and saved to Core Data:
NSError *error;
[[self.currentUser managedObjectContext] save:&error];
[[RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext save:&error];
Array of modified favouriteTeams is POSTed to the server where they are timestamped and returned with any other modified or recently added (by another device) objects.
The problem I'm running into is that the item that is POSTed, since it is always returned is being duplicated instead of being overwritten (based on the identificationAttributes). None of the other objects returned (whether modified or newly created by another device) get duplicated... just the newly created device from the user's device.
Here's my Request/Response mapping code:
RKObjectManager *objectManager = [RKObjectManager sharedManager];
[objectManager setRequestSerializationMIMEType:RKMIMETypeJSON];
// POST Request Mapping
RKObjectMapping *favouriteTeamMapping = [RKObjectMapping requestMapping];
[favouriteTeamMapping addAttributeMappingsFromArray:#[#"uuid", #"teamName", #"displayOrder"]];
RKRequestDescriptor *favouriteTeamRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:favouriteTeamMapping objectClass:[FavouriteTeam class] rootKeyPath:#"favouriteTeams"];
[objectManager addRequestDescriptor:favouriteTeamRequestDescriptor];
// Response Mapping
RKEntityMapping *favouriteTeamResponseMapping = [RKEntityMapping mappingForEntityForName:#"FavouriteTeam" inManagedObjectStore:objectManager.managedObjectStore];
favouriteTeamResponseMapping.identificationAttributes = #[#"uuid"];
[favouriteTeamResponseMapping addAttributeMappingsFromArray:#[#"uuid", #"teamName", #"displayOrder", #"lastModified"]];
RKResponseDescriptor *favouriteTeamResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:favouriteTeamResponseMapping pathPattern:#"/api/favouriteteam/" keyPath:#"data.favouriteTeams" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptorsFromArray:#[favouriteTeamResponseDescriptor]];
// POST
[objectManager postObject:favTeamsArray path:#"/api/favouriteteam/" parameters:nil success:nil failure:nil];
I can't seem to figure out why this duplication is happening when the identificationAttributes (favouriteDrug "uuid" attribute) are set. The objects are identical (even the same uuid) in Core Data. What is causing this behaviour?
NOTE: This question is also posted on the RestKit Google Groups here.
Thanks!
UPDATE: It appears that this duplication doesn't occur the first time a team is favorited. Any of the following attempts to favorite a team result in this duplication.
I had a similar issue, and while I cannot really confirm if this addressed your problem, will post my answer here anyways.
My problem was that the first object would be created fine, but thereafter restkit would save duplicate objects in coredata, so this sounds like the problem you are seeing.
I was making the post call to create object by:
* creating a blank object in coredata and filling it with whatever attributes I need
* make a post call to server using restkit API and passing in the newly created object
What restkit does under the hood is to take the response, fill it into the new object I created, and save it into coredata WITHOUT checking if there is another object of the same unique id beforehand. This is why the first object creation was fine, but subsequent objects were duplicates.
The way I solved it was actually to pass in the raw values as params to the restkit post API call, and nil as the object. On the reply, restkit will then look through coredata first to see if an object of that ID exists and merge changes with that object, or create a new one.

using RestKit un-restfully

Is there any way to use RestKit in an unrestful way?
For example, If I set up an object mapping as such:
[manager.router routeClass:[BBMediaResourceCreate class]
toResourcePath:#"/mediaresources"
forMethod:RKRequestMethodPOST];
RestKit will expect that I post a BBMediaResourceCreate object and receive one back also.
My API however, for reasons I won't go into, is not RESTful compliant in some situations. Rather than receive the newly created resource, I'll get something more like:
{ Model: { Success:true} }
or something similar
Is there a way to map a RestKit post to post a resource of one type but expect a response of another type?
Thanks.
When using v0.10 you can simply set resourcePath:#"/"and respond to
- (void)objectLoaderDidLoadUnexpectedResponse:(RKObjectLoader *)objectLoader
or
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObject:(id)object
and handle the response in [objectLoader response] like you want. Keep in mind that posting to that resource then needs an explicit, manually set path.

How to Get Response from Simple RestKit call with only username and password?

Is there a way to get the response back from a extremely basic RestKit call, before making any GETs or POSTs? I'm just extremely confused because I have a bunch of RESTful calls I want to make, but they all require a user_id as a part of the call. How do I get the response from the initial server interaction, which I'm assuming will contain the user_id.
Do we need to build an GET API call that accepts username and password and returns the user_id, or is there a way to do that part through RestKit? I feel like it should be done through the RestKit client...
UPDATE:
We were missing a needed API call. DOH!
I dont understand exactly what you want to know:
"Is there a way to get the response back from a extremely basic RestKit call?"
- what is this RestKit call exactly? Every restKit call delegate response or error for you mostly used the following RKRequestDelegate methods:
- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
RKLogInfo(#"Yay! We Got a response");
}
- (void)request:(RKRequest*)request didFailLoadWithError:(NSError *)error {
RKLogInfo(#"Oh no! We encountered an error: %#", [error localizedDescription]);
}
"How do I get the response from the initial server interaction, which I'm assuming will contain the user_id."
What is your initial server interaction exactly and what is the server answer what you receive?
Of course, your server should make this user_id available for you.
Is it maybe a solution for the login process in your case:
Looks like the correct way to configure this is as follows..
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:#"http://apihost.com" ];
objectManager.client.username = #"your-user-name";
objectManager.client.password = #"your-password";

Resources