gpt4 book ai didi

ios - 如何在 GCD 中添加 NSOperationQueue 等操作之间的依赖关系

转载 作者:行者123 更新时间:2023-11-30 11:07:13 25 4
gpt4 key购买 nike

下面是使用 NSOperation 队列加载 4 个图像的 4 个操作。这里操作2依赖于操作1,操作3依赖于操作2。

有谁知道我们可以在 GCD 中实现同样的目标吗?

@IBAction func didClickOnStart(sender: AnyObject) {

queue = NSOperationQueue()
let operation1 = NSBlockOperation(block: {
let img1 = Downloader.downloadImageWithURL(imageURLs[0])
NSOperationQueue.mainQueue().addOperationWithBlock({
self.imageView1.image = img1
})
})

operation1.completionBlock = {
print("Operation 1 completed")
}
queue.addOperation(operation1)

let operation2 = NSBlockOperation(block: {
let img2 = Downloader.downloadImageWithURL(imageURLs[1])
NSOperationQueue.mainQueue().addOperationWithBlock({
self.imageView2.image = img2
})
})

operation2.completionBlock = {
print("Operation 2 completed")
}
queue.addOperation(operation2)


let operation3 = NSBlockOperation(block: {
let img3 = Downloader.downloadImageWithURL(imageURLs[2])
NSOperationQueue.mainQueue().addOperationWithBlock({
self.imageView3.image = img3
})
})

operation3.completionBlock = {
print("Operation 3 completed")
}
queue.addOperation(operation3)

let operation4 = NSBlockOperation(block: {
let img4 = Downloader.downloadImageWithURL(imageURLs[3])
NSOperationQueue.mainQueue().addOperationWithBlock({
self.imageView4.image = img4
})
})

operation4.completionBlock = {
print("Operation 4 completed")
}
queue.addOperation(operation4)
}

这里我们可以添加操作之间的依赖关系

operation2.addDependency(operation1)
operation3.addDependency(operation2)

最佳答案

你不能(或者只是不这样做,因为它比使用 OperationQueueOperation 子类要工作得多)。

关于ios - 如何在 GCD 中添加 NSOperationQueue 等操作之间的依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52576610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com