简体   繁体   English

OperationQueue停止操作一会儿

[英]OperationQueue stop operations for a little time

I use swift 3.0.2. 我使用的是Swift 3.0.2。 Xcode 8.3.1 Xcode 8.3.1

I tried to use isSuspended = true property but it doesn't stop operations. 我尝试使用isSuspended = true属性,但它不会停止操作。

When I cancel one operation1 all other operations that are dependent to operation1 immediately start. 当我取消一个operation1所有其他依赖于operation1立即启动。

I want them to wait until I tell them. 我要他们等到我告诉他们。

Help me pleasee! 帮帮我吧!

Edit1: 编辑1:

Meaning it doesn't stop operations: 表示它不会停止操作:

I have operationqueue oq and three operations: op1 , op2 , op3 我有操作oq和三个操作: op1op2op3

oq.addOperation(op1)
op2.addDependency(op1)
oq.addOperation(op2)
op3.addDependency(op2)
oq.addOperation(op3)

All three operations need 10 seconds to execute. 所有这三个操作都需要10秒才能执行。

After I add third operation I set isSuspended = true 添加第三次操作后,我将isSuspended = true设置isSuspended = true

My log:
op1 started
(-- set `isSuspended` to true)
(after 10 seconds)
op2 started
(after 10 seconds)
op3 started

I thought that op1 is executing when I set isSuspended to true, it is OK. 我以为当我将isSuspended设置为true时,正在执行op1,就可以了。 But why op2 starts??? 但是为什么op2开始呢???

This is my File.swift : 这是我的File.swift

import Foundation

class PendingOps {
//    lazy var downloadInProgress = [Int: Operation]()
    // current tracklist

    var downloadQueue: OperationQueue {
        let queue = OperationQueue()
        queue.qualityOfService = .background
        queue.name = "Offline queue"
        queue.maxConcurrentOperationCount = 1
        return queue
    }
}


class op: Operation {
    var nam:String = ""

    init(nam: String) {
        self.nam = nam
    }

    override func main() {
        print("\(self.nam) started");

        sleep(10)

        print("\(self.nam) ended");
    }
}

And this is ViewController's viewDidLoad() function: 这是ViewController's viewDidLoad()函数:

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let test = PendingOps()

        let o1 = op(nam: "o1")
        let o2 = op(nam: "o2")
        o2.addDependency(o1)
        let o3 = op(nam: "o3")
        o3.addDependency(o2)

        test.downloadQueue.addOperation(o1)

        test.downloadQueue.isSuspended = true        
        print(test.downloadQueue.isSuspended)

        test.downloadQueue.addOperation(o2)
        test.downloadQueue.addOperation(o3)

    }

Your computed property downloadQueue creates a an independend queue on each call. 计算所得的属性downloadQueue在每个调用上创建一个独立队列。 Ie you are suspending the first created queue but the second and third queues are not influenced. 也就是说,您正在挂起第一个创建的队列,但是第二个和第三个队列不受影响。

To fix it, create the queue only once, maybe in init() 要修复它,只创建一次队列,也许在init()

class PendingOps {
    var downloadQueue: OperationQueue!

    init() {
        let queue = OperationQueue()
        queue.qualityOfService = .background
        queue.name = "Offline queue"
        queue.maxConcurrentOperationCount = 1
        downloadQueue = queue
    }
}

Playground sample: 游乐场样本:

class PendingOps {
    var downloadQueue: OperationQueue!

    init() {
        let queue = OperationQueue()
        queue.qualityOfService = .background
        queue.name = "Offline queue"
        queue.maxConcurrentOperationCount = 1
        downloadQueue = queue
    }
}

class op: Operation {
    var nam:String = "?"

    init(nam: String) {
        self.nam = nam
    }

    override func main() {
        print("\(self.nam) started");

        sleep(1)

        print("\(self.nam) ended");
    }
}

func didLoad() {
    let test = PendingOps()

    let o1 = op(nam: "o1")
    let o2 = op(nam: "o2")
    o2.addDependency(o1)
    let o3 = op(nam: "o3")
    o3.addDependency(o2)

    test.downloadQueue.addOperation(o1)

    test.downloadQueue.isSuspended = true
    print(test.downloadQueue.isSuspended)

    test.downloadQueue.addOperation(o2)
    test.downloadQueue.addOperation(o3)

}

didLoad()

PlaygroundPage.current.needsIndefiniteExecution = true

prints: 印刷品:

o1 started
true
o1 ended

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

相关问题 取消OperationQueue上的所有操作 - Cancelling all operations on OperationQueue 限制OperationQueue中的计数操作 - Restrict count operations in OperationQueue 如何停止DispatchGroup或OperationQueue等待? - How to stop DispatchGroup or OperationQueue waiting? 异步操作可以与 OperationQueue 上的“进度”一起使用吗? - Can asynchronous operations be used with `progress` on OperationQueue? 具有Operations的计时器和睡眠同步的Serial OperationQueue - Serial OperationQueue with Operations synchronizing timer and sleep 无法在OperationQueue swift中取消执行操作 - Can't cancel executing operations in OperationQueue swift 如果我调用cancelAllOpeartions,OperationQueue不会从队列中删除操作 - OperationQueue is not removing the operations from queue if I call cancelAllOpeartions Swift:将操作添加到 DispatchQueue/OperationQueue 但稍后启动队列 - Swift: Add operations to DispatchQueue/OperationQueue but launch the queue later 如何确保OperationQueue中的操作一个接一个地完成 - How to assure that operations in an OperationQueue are finished one after another Swift:OperationQueue-等待异步调用完成,然后再开始下一次(一次最多运行2个URLRequest) - Swift: OperationQueue - wait for asynchronous calls to finish before starting next (have maximum of 2 URLRequests running at a time)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM