_queue is a NSOperationQueue object. I upload an image to the server using the following:
[_queue addOperationWithBlock:^{
//POST request used to upload photo to server
//request has already been successfully configured before this step
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
}];
This can take a couple seconds and if I press the back button on the navigation controller the connection closes and the and image will not be upload. How can I make this background task occur even if the view controller is popped from the navigation stack?
I know sendSynchronousRequest has been deprecated, I will fix that eventually.
Presumably _queue is a member variable of the view controller? If so then as a quick fix to get things initially working you could change it to be a static member variable (to change its lifetime), but it would be much preferable to move it to a model class and have the model upload the image on behalf of your view controller
This is leading to a better design, especially once it becomes asynchronous - image this scenario:
- view controller A starts the upload
- user navigates to view controller B
- upload fails and you need to notify the user of the failure or retry the upload
- now what? A started the upload but it no longer exists how do you notify the user or retry the upload?
If you make it the model's responsibility to upload the image then this is the situation:
- view controller A starts the upload
- user navigates to view controller B
- upload fails and you need to notify the user or retry
- model broadcasts a notification the upload has failed or just retires the upload
- a meta view controller listens for the notification and displays a dialog to the user informing of the failure