How to make RESTKit call in iOS continue even if h

2019-09-07 11:03发布

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.

2条回答
等我变得足够好
2楼-- · 2019-09-07 11:31

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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-09-07 11:32

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

查看更多
登录 后发表回答