简体   繁体   English

调用.cancel()时DispatchWorkItem不终止函数

[英]DispatchWorkItem not terminating function when .cancel() is called

I have a series of HTTP requests made sequentially using Alamofire in a list of functions called in a main function, runTask() that I want to have the ability to stop. 我在主函数runTask()中调用的函数列表中按顺序使用Alamofire进行了一系列HTTP请求,我希望能够停止它。 So, I set up the runTask() function call in a DispatchWorkItem for each of the task I need to run and store the work item in an array like so: 因此,我在DispatchWorkItem为每个需要运行的任务设置runTask()函数调用,并将工作项存储在数组中,如下所示:

taskWorkItems.append(DispatchWorkItem { [weak self] in
    concurrentQueue!.async {
        runTask(task: task)
    }
})

Then, I iterate of the array of work items and call the perform() function like so: 然后,我迭代工作项数组并调用perform()函数,如下所示:

for workItem in taskWorkItems {
    workItem.perform()
}

Finally, I have a button in my app that I want to cancel the work items when tapped, and I have the following code to make that happen: 最后,我的应用程序中有一个按钮,我想在点击时取消工作项,并且我有以下代码来实现:

for workItem in taskWorkItems {
    concurrentQueue!.async {
        workItem.cancel()

        print(workItem.isCancelled)
    }
}

workItem.isCancelled prints to true ; workItem.isCancelled打印到true ; however, I have logs set up in the functions called by runTask() and I still see the functions executing even though workItem.cancel() was called and workItem.isCancelled prints true . 然而,我在被调用的函数设置日志runTask()我仍然看到执行,即使功能workItem.cancel()被调用和workItem.isCancelled打印true What am I doing wrong and how can I stop the execution of my functions? 我做错了什么,如何停止执行我的功能?

TLDR: calling cancel will stop tasks from executing if they have yet to be run, but won't halt something that's already executing. TLDR:调用cancel将阻止任务执行,如果它们尚未运行,但不会停止已经执行的任务。

Since the apple docs on this are threadbare... 由于这方面的苹果文档是破旧的......

https://medium.com/@yostane/swift-sweet-bits-the-dispatch-framework-ios-10-e34451d59a86 https://medium.com/@yostane/swift-sweet-bits-the-dispatch-framework-ios-10-e34451d59a86

A dispatch work item has a cancel flag. If it is cancelled before running, the dispatch queue won't execute it and will skip it. If it is cancelled during its execution, the cancel property return True. In that case, we can abort the execution

//create the dispatch work item
var dwi2:DispatchWorkItem?
dwi2 = DispatchWorkItem {
    for i in 1...5 {
        print("\(dwi2?.isCancelled)")
        if (dwi2?.isCancelled)!{
            break
        }
        sleep(1)
        print("DispatchWorkItem 2: \(i)")
    }
}
//submit the work item to the default global queue
DispatchQueue.global().async(execute: dwi2!)

//cancelling the task after 3 seconds
DispatchQueue.global().async{
    sleep(3)
    dwi2?.cancel()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 取消循环外的 DispatchWorkItem - Swift - Cancel DispatchWorkItem outside loop - Swift 触摸应该取消 function 因为 UITableView 没有被调用 - Touches should cancel function for UITableView is not called 如何取消单元测试调用的功能 - how to cancel the function that unit test called times 取消操作时未调用NSOperation dealloc - NSOperation dealloc not called when cancel operation 调用NSURLConnection取消时是否触发didFailWithError? - Is didFailWithError triggered when NSURLConnection cancel is called? AVPlayer.status 包装在具有延迟的 DispatchWorkItem 中时不运行 - AVPlayer.status doesn't run when wrapped in a DispatchWorkItem with a Delay 何时调用 UICollectionViewDelegateFlowLayout function sizeForItemAt - When is UICollectionViewDelegateFlowLayout function sizeForItemAt called 当用户点击或滚动UITableView时如何取消功能? - How to cancel a function when a user taps on or scrolls through a UITableView? iOS-不需要时调用Superview手势识别器,无法取消子视图触摸事件 - iOS - Superview gesture recognizer getting called when not wanted, can't cancel child view touch event 如何在Swift中暂停/恢复DispatchWorkItem - How to pause/resume a DispatchWorkItem in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM