Question

I have an objectMapping for my entity User

RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping mapKeyPath:@"first_name" toAttribute:@"firstName"];
[userMapping mapKeyPath:@"last_name" toAttribute:@"lastName"];
[manager.mappingProvider setMapping:userMapping forKeyPath:@"users"];
[manager.mappingProvider setSerializationMapping:[userMapping inverseMapping] forClass:[User class]];

And I want to save his profile when the application goes background:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[RKObjectManager sharedManager] putObject:currentUser delegate:self];
}

But it won't let me run the request while closing down the app (I'm guessing no new threads are allowed). So I would like to do this synchronously.

However, I didn't manage to do this with RestKit. Is there some misunderstanding on my side ? I would like to have:

[[RKObjectManager sharedManager] putObjectSynchronously:currentUser];
Was it helpful?

Solution

If you need to send synchronously, you're not going to be able to use the convenience methods on RKObjectManager, like putObject, because these convenience methods all send the request asynchronously on your behalf. Instead, you can try something like the following:

RKObjectLoader* loader = [[RKObjectManager sharedManager] objectLoaderForObject:currentUser method:RKRequestMethodPUT delegate:nil];
RKResponse* response = [loader sendSynchronously];

OTHER TIPS

It can be done using RestKit and the convenience methods on RKObjectManager. The tricks is to use blocks.

In your case you would make your request using the block, set the normal onDidFail, onDidLoadObjects, onDidLoadResponse, etc. methods on "loader" rather than as delegate methods in the class, and then close the app within the block. For example, the code within loader.onDidLoadObjects method will not be executed until the putObject has been completed, so that's likely where you would want to close the screen or app. This would ensure the putObject completes before moving on.

See more in my answer - Make a synchronous HTTP call with RestKit.

what about:

RKRequest *request = [client post:@"/service" params:params delegate:restDelegate];

RKResponse *response = [request sendSynchronously];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top