Ignore response when doing a postObject in RestKit for iOS
Asked Answered
M

3

8

I am using RestKit for connecting to our WCF Data Services.

I am having issues with adding an entity using RKObjectManager's postObject function due to response mapping issues.

When an entity is added, WCF Data Services returns a 201 status code and the newly added entity(as response).

Is it possible to ignore the response and just use the status code returned to check if the add succeeded?

Ponnu

Melleta answered 26/10, 2011 at 0:49 Comment(0)
S
1

Why would you want to ignore the newly added entity returned from the server? Mapping that result is useful to keep in sync your local representation with the server's one. The server may have overwritten some field of your object like the object id and you want to keep track of it.

If you have mapping error it's probably because the response to the POST action returns a representation of your object which differs from the one returned with a GET. Have you tried using:

- (RKObjectLoader*)postObject:(id<NSObject>)object mapResponseWith:(RKObjectMapping*)objectMapping delegate:(id<RKObjectLoaderDelegate>)delegate

instead and specify a more suitable mapping for the data returned?

Solatium answered 5/2, 2012 at 13:26 Comment(0)
N
0

The problem here can be to alter the REST service, so instead a simple solution would be to ignore the callback to didFailWithError in case of postObject calls to a certain resource path.

- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error {
if ([objectLoader wasSentToResourcePath:@"/rest/api/returns/201" method:RKRequestMethodPOST] && [[objectLoader response] statusCode]==201) {
    NSLog(@"Object created");
} else {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Communication error"
                                                        message:[NSString stringWithFormat:@"Received status code %d: %@",                                                                                               objectLoader.response.statusCode,                                                                                               error.localizedDescription]                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}
Nannie answered 17/3, 2012 at 7:46 Comment(0)
M
0

Create a trivial RKObjectMapping that does not care about any parameters in response.

[RKObjectMapping mappingForClass: [NSNull class]];
Metalinguistic answered 3/10, 2014 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.