gpt4 book ai didi

ios - 从嵌套 NSOperations 调用 cancelAllOperations

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

我正在努力应对以下情况,请耐心等待,因为我已尽力尽可能清楚地解释这一点:

我有一个类 CoummintyOperation,它是 GroupOperation 的子类。 CommuityOperation 被调用并添加到 NSOperationQueue 中。CommunityOperation 依次调用一大堆 NSOperations,这些 NSOperations 也是 GroupOperation 的子类,并依次调用其中的 NSOperations。我已在 CommunityOperation 类中添加了 GroupOperations 的依赖项。我正在处理的问题是,如果嵌套操作失败,我需要取消 CommunityOperations 类中的所有 NSOperations,但我无权访问 CommunityOperation 添加到的操作队列,以便调用 cancelAllOperations 方法。

任何人都可以帮我解释一下如何调用此方法、取消此类中的所有操作(以及所有嵌套操作)并向用户显示错误消息。

提前致谢

这里有一些示例代码可以帮助解释我的问题:CommunityOperation 从另一个类(不包括)放入操作队列

public class CommunityOperation: GroupOperation {

var updateOperation: UpdateOperation!
var menuOperation: MenuOperation!
var coreDataSaveOperation: CoreDataSaveOperation!

var hasErrors = false

public init() {

super.init(operations: [])

updateOperation = UpdateOperation()
menuOperation = MenuOperation()
coreDataSaveOperation = CoreDataSaveOperation()
coreDataSaveOperation.addDependencies([updateOperation, menuOperation])

self.addOperation(updateOperation)
self.addOperation(menuOperation)
self.addOperation(coreDataSaveOperation)
}
}

MenuOperation 类,其中也嵌套了操作:

class UpdateMenuOperation: GroupOperation {

let downloadGroupsOperation: DownloadGroupsOperation
let downloadMembersOperation: DownloadMembersOperation

init() {
downloadGroupsOperation = DownloadGroupsOperation()
downloadMembersOperation = DownloadMembersOperation(])

super.init(operations: [downloadGroupsOperation,
downloadMembersOperation
])
}
}

DownloadGroupOperation 类又是 GroupOperation 的子类。它有两个操作 - 第一个是下载数据,第二个是解析数据:

class DownloadTopGroupsOperation: GroupOperation {
let downloadOperation: DownloadOperation
let importTopGroupsOperation: ImportOperation

init() {
downloadOperation = DownloadOperation()
importOperation = ImportOperation()
importOperation.addDependency(downloadOperation)
importOperation.addCondition(NoCancelledDependencies())

super.init(operations: [downloadOperation, importOperation])
}
}

最后(哇)DownloadOperation 类使用 NSURLSession 和方法 downloadTaskWithURL,在该方法的完成处理程序中,如果返回错误,我想在主 operatioQueue 上调用 cancelAllOperations:

class DownloadOperation: GroupOperation {
init() {
super.init(operations: [])
if self.cancelled {
return
}
let task = session.downloadTaskWithURL(url) { [weak self] url, response, error in
self?.downloadFinished(url, response: response as? NSHTTPURLResponse, error: error)
}
}

func downloadFinished(url: NSURL?, response: NSHTTPURLResponse?, error: NSError?) {

if error {
*cancel allOperations on queue*
}
}
}

最佳答案

它的工作方式应该有点不同。我会在每次 NSOperation 执行结束时检查 GroupOperation 中的 isCancelled 。如果操作被取消,则取消当前的GroupOperation,依此类推。最后,您的 CommunityOperation 也应该被取消。

以下是建议解决方案的粗略实现:

extension GroupOperation {

func addCancellationObservers() {
self.operations.forEach() { $0.willCancelObservers.append() { [unowned self] operation, errors in
self.cancel() // cancel the group operation. will force it to cancel all child operations
}
}
}

}

然后从您拥有的每个组操作的 init 方法中调用 addCancellationObservers

关于ios - 从嵌套 NSOperations 调用 cancelAllOperations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37811186/

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