簡體   English   中英

從導航堆棧中彈出該視圖后,如何使后台任務完成

[英]How do make background task complete when that view has been popped from the navigation stack

_queue是NSOperationQueue對象。 我使用以下方法將圖像上傳到服務器:

[_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];
}];

這可能需要花費幾秒鍾的時間,如果我按導航控制器上的“后退”按鈕,連接將關閉,並且和圖像將不會上傳。 即使從導航堆棧中彈出視圖控制器,如何使此后台任務發生?

我知道sendSynchronousRequest已被棄用,我將最終解決。

大概_queue是視圖控制器的成員變量嗎? 如果是這樣,那么作為使事情開始起作用的快速解決方案,您可以將其更改為靜態成員變量(以更改其生命周期),但是將其移至模型類並讓模型上載圖像將是更可取的代表您的視圖控制器
這將導致更好的設計,尤其是一旦它變得異步時-對此場景進行映像:

- 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?

如果您將上傳圖片作為模型的責任,則情況如下:

- 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM