Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何取消后台运行的线程?(iOS)_Ios_Swift_Multithreading_Asynchronous_Background - Fatal编程技术网

如何取消后台运行的线程?(iOS)

如何取消后台运行的线程?(iOS),ios,swift,multithreading,asynchronous,background,Ios,Swift,Multithreading,Asynchronous,Background,这是我使用调度队列的函数,我想在后台运行时取消它。我该怎么做 extension DispatchQueue { static func background(delay: Double = 0.0, background: (()->Void)? = nil, completion: (() -> Void)? = nil) { DispatchQueue.global(qos: .background).async { backg

这是我使用调度队列的函数,我想在后台运行时取消它。我该怎么做

 extension DispatchQueue {
    static func background(delay: Double = 0.0, background: (()->Void)? = nil, completion: (() -> Void)? = nil) {
        DispatchQueue.global(qos: .background).async {
            background?()
            if let completion = completion {
                DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: {
                    completion()
                })
            }
        }
    }       
 }

 DispatchQueue.background(background: {
        do {
        }
        catch let error {
            // Error handling
        }
    }, completion:{
 })

您一定要查看有关以下内容的苹果官方文档:

还有这个


希望它能帮助您建立关于iOS和更高级别API的知识库。

您一定要查看有关以下内容的苹果官方文档:

还有这个


希望它能帮助您建立有关iOS和更高级别API的知识库。

您可以与DispatchGroup一起使用DispatchWorkItem

    // create a work item with the custom code
    let workItem = DispatchWorkItem {
          // Insert your code here
    }


   //Create dispatch group
    let dispatchGroup = DispatchGroup()

   // execute the workItem with dispatchGroup
    DispatchQueue.global().async(group: dispatchGroup, execute: workItem)

   //Handle code after the completion of global queue
    dispatchGroup.notify(queue: DispatchQueue.global()) {
            print("global queue execution completed")
        }

   //when the App goes to background cancel the workItem
     workItem.cancel()

您可以将DispatchWorkItem与DispatchGroup一起使用

    // create a work item with the custom code
    let workItem = DispatchWorkItem {
          // Insert your code here
    }


   //Create dispatch group
    let dispatchGroup = DispatchGroup()

   // execute the workItem with dispatchGroup
    DispatchQueue.global().async(group: dispatchGroup, execute: workItem)

   //Handle code after the completion of global queue
    dispatchGroup.notify(queue: DispatchQueue.global()) {
            print("global queue execution completed")
        }

   //when the App goes to background cancel the workItem
     workItem.cancel()

如果要取消后台线程,请使用nsoperationqueue而不是GCDi如果要取消后台线程,请使用nsoperationqueue而不是GCDh如何将此调度队列转换为操作队列?如何将此调度队列转换为操作队列?我喜欢您的回答,但如何知道DispatchQueue.global(qos:.background).async(execute:workItem!)在进入DispatchQueue.main.async(execute:workItem)?我喜欢你的答案,但我怎么知道DispatchQueue.global(qos:.background).async(execute:workItem!)在进入DispatchQueue.main.async(execute:workItem!)之前已经完成了执行?