简体   繁体   中英

OperationQueue stop operations for a little time

I use swift 3.0.2. Xcode 8.3.1

I tried to use isSuspended = true property but it doesn't stop operations.

When I cancel one operation1 all other operations that are dependent to operation1 immediately start.

I want them to wait until I tell them.

Help me pleasee!

Edit1:

Meaning it doesn't stop operations:

I have operationqueue oq and three operations: op1 , op2 , op3

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

All three operations need 10 seconds to execute.

After I add third operation I set 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. But why op2 starts???

This is my 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:

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. 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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM