How to make RESTKit call in iOS continue even if home button is pressed or phone goes into standby

左心房为你撑大大i 提交于 2020-01-25 11:13:31

问题


I currently have an async call to upload a photo, the photo is just a base64 encoded string, and it has been resized so it's not super huge, maybe 800x800 or something like that.

The problem I currently have is that even though the upload works, the user can't close the app, hit the home button, or otherwise leave the app while it runs. They CAN however work elsewhere in the app which I guess is a bonus, but how do I make this call continue even if they leave the app?

Thanks!

Here is the code I have (it's working), (well it's working if they just sit there)

NSMutableURLRequest *request = [gad.globalObjectManager requestWithObject:nil method:RKRequestMethodPOST path:@"/api/photo/SavePhoto" parameters:paramsSave];

[request setTimeoutInterval:6000];

AFHTTPRequestOperation *operation = [gad.globalClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Photo Upload" message:@"Your photo was successfuly uploaded" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Photo Upload" message:@"Your photo took long to upload or the harvest server is down, please try again when you are on a wi-fi connection" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }];


    [gad.globalClient enqueueHTTPRequestOperation:operation];

The reason I used a mutable request is to increase the timeout, I read other articles that said if I needed a larger timeout I had to go to AF Networking directly, and since RESTKit sits on top I had to do it this way.


回答1:


RestKit network operations are subclasses of AFNetworking operations so you have access anyway.

Call setShouldExecuteAsBackgroundTaskWithExpirationHandler: on your operation to configure it to run in the background.




回答2:


So the answer to this was delete the grid, rebuild the grid and try again. Seriously, this was some sort of XCode bug, it works fine with another UI



来源:https://stackoverflow.com/questions/25814162/how-to-make-restkit-call-in-ios-continue-even-if-home-button-is-pressed-or-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!