我需要暫停插入NSOperationQueue的正在運行的NSOperation。目前我正在取消所有操作並重新啓動它們。但是這會導致流程的重複。我試着用NSOperationQueue的setSuspended標誌。但它並未暫停操作。有什麼出路嗎?如何在NSOperationQueue中暫停NSOperation?
14
A
回答
10
看到這一點:Link
這裏從apple docs:
暫停,恢復隊列 如果要發放臨時停止操作的執行,你可以暫停相應的操作隊列使用setSuspended:方法。
暫停隊列不會導致已執行的操作在其任務中間暫停。它只是防止新的操作被安排執行。您可能會暫停隊列以響應用戶請求暫停任何正在進行的工作,因爲預期用戶可能最終想要恢復該工作。
3
我還沒有試過,但我可能會從這裏開始:
- 添加
isPaused
標誌您NSOperation
子 - 實現對操作的數據複製/移動方法
- 如果暫停,請(請注意
-main
中的此更改) - 創建將暫停操作狀態移至新操作的新操作
- 調整優先級隊列重新排序操作順序,如果你喜歡
- 克隆從
-main
如果-main
注意,這將只是暫停其添加到隊列
現在,如果您正在觀察或有特殊的完成,那麼您將會遇到一些其他問題。對於簡單的情況,似乎這種方法似乎工作正常。
相關問題
- 1. NSOperationQueue,NSOperation
- 2. 我想立即停止在NSOperationQueue NSOperation
- 3. 如何在Objective-C中使用NSOperation&NSOperationQueue?
- 4. 如何使用NSOperation和NSOperationQueue
- 5. NSOperation存在於NSOperationQueue
- 6. NSOperation和NSOperationQueue
- 7. NSOperationQueue INSIDE an NSOperation
- 8. NSOperation:如何暫停和恢復
- 9. 保存暫停NSOperationQueue文件
- 10. NSOperationQueue暫停和恢復?
- 11. NSOperationQueue會隨機暫停嗎?
- 12. NSOperation和NSOperationQueue maxConcurrentOperationCount = 1
- 13. NSOperation和NSOperationQueue回調
- 14. 取消NSOperation從NSOperationQueue
- 15. 暫停正在執行的任務NSOperationQueue
- 16. NSOperation在睡眠模式後暫停
- 17. 把一個NSOperationQueue與NSOperation放在一個單獨的NSOperationQueue上搶佔NSOperation?
- 18. 的NSOperation和NSOperationQueue取消
- 19. NSOperation死鎖和塊NSOperationQueue
- 20. NSOperation和NSOperationQueue不起作用
- 21. C++等價於NSOperation和NSOperationQueue
- 22. NSOperation&NSOperationQueue凍結應用
- 23. 從NSOperation內取消NSOperationQueue
- 24. 如何使NSOperation無法在iPhone中添加到NSOperationQueue中
- 25. iOS - 如何檢查NSOperation是否在NSOperationQueue中?
- 26. NSOperation完成後,不從NSOperationQueue中刪除?
- 27. 爲什麼我的NSOperationQueue在暫停時停止執行?
- 28. 如何使用以及在何處使用的NSOperation和NSOperationQueue>
- 29. 如何在NSOperationQueue完成之前阻止NSOperation?
- 30. 如何在zsh中暫停?
謝謝!我試圖從你的鏈接實施。我在NSOperation內部添加了一個名爲'isPaused'的新變量,每當我需要暫停操作時,我將它的值設置爲YES。在NSOperation內部,我會定期檢查'isPaused'的價值。每當我發現它是'是',我都會跳出循環。但我在這裏遇到嚴重問題。我正嘗試從主線程設置'isPaused'的值。這可能會導致一些競爭條件。有什麼辦法擺脫比賽條件?有沒有辦法在同一個線程(而不是主線程)中調用方法(暫停和恢復)? – Advaith 2012-08-16 09:28:43
@Advaith:如果兩個線程在讀取或寫入'isPaused'之前鎖定一個互斥鎖,則不必擔心比賽。 – 2012-08-19 19:23:45