我需要运行多个函数,并且函数的执行顺序非常重要。我有两个需要运行的非异步函数,然后需要运行两个异步函数。所有函数必须在前一个函数完成后执行。
到目前为止我有以下内容。但是,异步函数似乎并不遵循我所列出的依赖关系。队列开始后,两个异步函数都会立即执行。
我有什么遗漏的吗?任何帮助表示赞赏!:)
let queue = OperationQueue()
let operation1 = BlockOperation {
nonAsyncFunc1()
}
let operation2 = BlockOperation {
nonAsyncFunc2()
}
let operation3 = BlockOperation {
asyncFunc1()
}
let operation4 = BlockOperation {
asyncFunc2()
}
operation2.addDependency(operation1)
operation3.addDependency(operation2)
operation4.addDependency(operation3)
self.queue.addOperation(operation1)
self.queue.addOperation(operation2)
self.queue.addOperation(operation3)
self.queue.addOperation(operation4)
Run Code Online (Sandbox Code Playgroud) 我确信我的逻辑有问题,只是无法弄清楚它是什么。
有一个“Service”类,它有一个操作队列:
class Service {
let queue: OperationQueue = {
var queue = OperationQueue()
queue.name = "my.operationQueue"
queue.maxConcurrentOperationCount = 1
return queue
}()
func add(operation: Operation) {
queue.addOperation(operation)
}
}
Run Code Online (Sandbox Code Playgroud)
该操作是异步的,因此它会覆盖状态和函数start:
class MyOp: Operation {
private var state: State = .ready
private var id: Int
init(id: Int) {
self.id = id
}
override var isAsynchronous: Bool {
return true
}
override var isReady: Bool {
return state == .ready
}
override var isExecuting: Bool {
return state == …Run Code Online (Sandbox Code Playgroud)