문제

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];
도움이 되었습니까?

해결책

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];

다른 팁

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];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top