- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
您好,我正在使用 Swift 构建应用程序。我需要按特定顺序处理通知。因此,我正在尝试使用 addOperations waitUntilFinished。
这是我做的:
let oldify = NSOperation()
oldify.completionBlock = {
println("oldify")
}
let appendify = NSOperation()
appendify.completionBlock = {
println("appendify")
}
let nettoyify = NSOperation()
nettoyify.completionBlock = {
println("nettoyify")
}
NSOperationQueue.mainQueue().maxConcurrentOperationCount = 1
NSOperationQueue.mainQueue().addOperations([oldify, appendify, nettoyify], waitUntilFinished: true)
使用这段代码,没有任何操作被执行。当我尝试这样做时:
NSOperationQueue.mainQueue().maxConcurrentOperationCount = 1
NSOperationQueue.mainQueue().addOperation(oldify)
NSOperationQueue.mainQueue().addOperation(appendify)
NSOperationQueue.mainQueue().addOperation(nettoyify)
操作得到执行但顺序不正确。
有谁知道我做错了什么?我对 NSOperations 的快速但全新的信心
最佳答案
几个问题:
您正在检查完成 block 处理程序的行为。正如 completionBlock
文档所说:
The exact execution context for your completion block is not guaranteed but is typically a secondary thread. Therefore, you should not use this block to do any work that requires a very specific execution context.
队列将自己管理操作,但不管理它们的完成 block (缺少确保操作在其 completionBlock
开始之前完成)。因此,最重要的是,不要对 (a) 何时运行完成 block ,(b) 一个操作的 completionBlock
与其他操作或其 completionBlock
block 的关系做任何假设等,也不是 (c) 它们在哪个线程上执行。
操作通常按照它们被添加到队列中的顺序执行。但是,如果您添加一个操作数组,该文档不会正式保证它们按照它们在该数组中出现的顺序排队。因此,您可能希望一次添加一个操作。
话虽如此,文档继续警告我们:
An operation queue executes its queued operation objects based on their priority and readiness. If all of the queued operation objects have the same priority and are ready to execute when they are put in the queue—that is, their
isReady
method returnsYES
—they are executed in the order in which they were submitted to the queue. However, you should never rely on queue semantics to ensure a specific execution order of operation objects. Changes in the readiness of an operation can change the resulting execution order. If you need operations to execute in a specific order, use operation-level dependencies as defined by theNSOperation
class.
要建立明确的依赖关系,您可以这样做:
let oldify = NSBlockOperation() {
NSLog("oldify")
}
oldify.completionBlock = {
NSLog("oldify completion")
}
let appendify = NSBlockOperation() {
NSLog("appendify")
}
appendify.completionBlock = {
NSLog("appendify completion")
}
appendify.addDependency(oldify)
let nettoyify = NSBlockOperation() {
NSLog("nettoyify")
}
nettoyify.completionBlock = {
NSLog("nettoyify completion")
}
nettoyify.addDependency(appendify)
let queue = NSOperationQueue()
queue.addOperations([oldify, appendify, nettoyify], waitUntilFinished: false)
顺便说一句,正如您将在上面看到的,您不应将操作与 waitUntilFinished
一起添加到主队列。随意将它们添加到不同的队列,但不要使用 waitUntilFinished
选项从串行队列调度,返回到自身。
关于ios - NSOperationQueue addOperations waitUntilFinished,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29992735/
我在 crashlytics 中得到了一些我完全不明白的崩溃报告,这是崩溃线程的崩溃日志: 我没有找到任何关于我的代码的提示,它也不是可重现的或只发生在特定设备上的东西。根据 Crashlytics
我需要从 JSON 下载数据,并将数据分配给 NSOperationQueue 之外的 NSData。这是我的代码: -(void)parsingInfo { NSURL *url = [NS
我有一个应用程序,其中一个长时间运行的进程(> 1 分钟)被放置在 NSOperationQueue(队列 A)上。当队列 A 操作运行时,UI 完全响应,完全符合预期。 但是,我有一种用户可以执行的
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 1年前关闭。 Improve this
我有几个后台任务需要处理,同时仍保持 UI 响应。我开始走上创建和管理线程的道路,但很快就遇到了 NSOperations。听起来是一个更好的解决方案。 。 . 但是,我似乎无法获得对 NSOpera
我想知道释放 ivar NSOperationQueue 的正确方法是什么,以防它仍有一些操作正在运行,这种情况通常在用户突然退出应用程序时发生。在一些示例中,我看到使用了 waitUntilAllO
我正在努力解决这个问题,所以我的问题是: 当我像这样实例化 NSOperationQueue 时: NSOperationQueue * operationQueue = [[NSOperationQ
我第一次尝试基于 Swift/NSOperationQueue 的设计,我正在尝试找出如何维护跨队列的数据完整性。 我处于设计过程的早期阶段,但该架构可能会涉及一个队列(称为sensorQ),用于处理
我正在寻找一种重新排序 NSOperationQueue 的方法。 我可以取消所有操作并按照我想要的顺序将它们扔回去,但我希望有一些更干净的东西。有什么想法吗? 最佳答案 NSOperationQue
我是iPhone新手。在哪里可以找到NSOperationQueue和NSOperation的示例?NSOperationQueue和NSOperation与线程相比有什么优势? 谢谢 最佳答案 阅读
正如 NSOperationQueue 类引用所述:在 iOS 中,操作队列不使用 Grand Central Dispatch 来执行操作。它们为非并发操作创建单独的线程,并从当前线程启动并发操作。
我创建了一个NSOperationQueue子类,将maxConcurrentOperations设置为1,并将addOperation方法重写为以下内容: -(void)addOperation:(
我一直在使用 NSOperationQueue,但我遇到了非常奇怪的内存问题。我已经尝试将问题减少到最简单的问题,在这里我得到了: 在初始化中: _queue = [[NSOperationQueue
我最近开始学习 Swift 中的多线程,我想使用 NSOperationQueue 和 NSOperation 类尝试一些基本的多线程。 在 .swift 文件的全局范围内(在 Xcode 中的命令行
我的任务是清理项目中一些现有的 Swift 代码,这些代码刚刚转换为 Swift 3。但是,我不断看到这个对我来说看起来很可疑的代码。 OperationQueue().addOperation(so
我正在开发一个应用程序,该应用程序从使用 performSelectorInBackground 启动的单独线程中的循环调用 AudioServicesPlayAlertSound(kSystemSo
我在我的应用程序中使用了 NSOperation 子类,它将在单个操作中执行以下 4 个任务,我希望所有这 4 个任务都在后台线程上运行,所以我将其封装到单个 NSOperation 类,这样我就可以
我在暂停当前正在执行的任务时遇到问题,我尝试设置 NSOperationQueue setSuspended=YES 暂停和 setSuspended=NO恢复进程。 根据 apple docs我不能
我正在使用 NSOperationQueue 执行并发下载并插入到核心数据中。 下面是代码,我正在使用 if(nil==queue) { queue=[[NSOperationQueue al
我正在为 ios 进行调整(显示来自网络的一些图像),并且我正在尝试使用 NSOperationQueue。首先,我在主线程上运行了一些代码来看看它是什么样子。显示每个图像持续了 ±5 秒(UI 卡住
我是一名优秀的程序员,十分优秀!