如何在 Swift 中暂停操作队列
How to pause operation queue in Swift
我要通过 iOS 应用程序在 IoT 设备上执行一些操作。所以我所有的操作都在 OperationsQueue 中进行串行操作。
这里我想一次执行一个操作,每个操作都需要等到我得到物联网设备的响应。
此处物联网设备响应需要时间才能发回。那么如何等待操作队列中的当前操作,直到我得到物联网的响应。
那么有什么方法可以暂停当前的 运行 操作,直到从 IoT 获得响应,然后我将恢复它,以便操作队列中的下一个操作开始。
我试过休眠操作但它需要时间,但我们不能保证物联网设备响应。
如有任何建议,我们将不胜感激。提前谢谢你。
基本思想是您不暂停(或等待或休眠),而是定义“并发”操作(参见并发操作的讨论 in the documentation) that doesn’t trigger the isFinished
KVO,直到设备响应。
一个简单的方法是编写并发操作 class,如 。然后你的物联网操作可以子class那个AsynchronousOperation
class,并且当设备响应时调用finish()
。
那么您的操作队列(大概有 1
的 maxConcurrentOperationCount
,或者可能正在使用依赖项),直到前一个操作完成后才会开始操作。
正如 Rob 所说,您可以从 IoT 操作中实现异步操作 class 和子class。对我来说,这似乎是实施您的案例的最首选方式。
作为替代方案,如果您需要仅在另一个线程中的某个异步事件完成后才继续该过程,您可以使用NSCondition。这是 obj-c 的一种机制,它提供了一种等待条件发生的简单方法。
示例如下:
let cond = NSCondition()
var available = false
var sharedString = ""
class WriterThread: Thread {
override func main() {
for _ in 0..<5 {
cond.lock()
sharedString = ""
available = true
cond.signal() // Notify and wake up the waiting thread/s
cond.unlock()
}
}
}
class PrinterThread: Thread {
override func main(){
for _ in 0..<5 { //Just do it 5 times
cond.lock()
while(!available) { //Protect from spurious signals
cond.wait()
}
print(SharedString)
sharedString = ""
available = false
cond.unlock()
}
}
}
let writet = WriterThread()
let printt = PrinterThread()
printt.start()
writet.start()
您可以在发送操作时使用 DispatchQueue
并调用 .suspend()
,并使用获取响应调用 .resume()
的代码。然后无论你想在继续之前等待响应,只需放置一个虚拟 queue.sync({ print("done waiting")})
它会自动等待直到 .resume()
在打印和继续之前被调用。
import Dispatch
var queue = DispatchQueue(label: "queue")
func sendOperationToIoTDevice(){
//send the operation
//...
queue.suspend()
}
...
//whatever code gets the response:
//get response
//...
queue.resume()
...
//main code
sendOperationToIoTDevice()
queue.sync { print("done waiting") } // will hang here until .resume() is called
我要通过 iOS 应用程序在 IoT 设备上执行一些操作。所以我所有的操作都在 OperationsQueue 中进行串行操作。
这里我想一次执行一个操作,每个操作都需要等到我得到物联网设备的响应。
此处物联网设备响应需要时间才能发回。那么如何等待操作队列中的当前操作,直到我得到物联网的响应。
那么有什么方法可以暂停当前的 运行 操作,直到从 IoT 获得响应,然后我将恢复它,以便操作队列中的下一个操作开始。
我试过休眠操作但它需要时间,但我们不能保证物联网设备响应。
如有任何建议,我们将不胜感激。提前谢谢你。
基本思想是您不暂停(或等待或休眠),而是定义“并发”操作(参见并发操作的讨论 in the documentation) that doesn’t trigger the isFinished
KVO,直到设备响应。
一个简单的方法是编写并发操作 class,如 AsynchronousOperation
class,并且当设备响应时调用finish()
。
那么您的操作队列(大概有 1
的 maxConcurrentOperationCount
,或者可能正在使用依赖项),直到前一个操作完成后才会开始操作。
正如 Rob 所说,您可以从 IoT 操作中实现异步操作 class 和子class。对我来说,这似乎是实施您的案例的最首选方式。
作为替代方案,如果您需要仅在另一个线程中的某个异步事件完成后才继续该过程,您可以使用NSCondition。这是 obj-c 的一种机制,它提供了一种等待条件发生的简单方法。
示例如下:
let cond = NSCondition()
var available = false
var sharedString = ""
class WriterThread: Thread {
override func main() {
for _ in 0..<5 {
cond.lock()
sharedString = ""
available = true
cond.signal() // Notify and wake up the waiting thread/s
cond.unlock()
}
}
}
class PrinterThread: Thread {
override func main(){
for _ in 0..<5 { //Just do it 5 times
cond.lock()
while(!available) { //Protect from spurious signals
cond.wait()
}
print(SharedString)
sharedString = ""
available = false
cond.unlock()
}
}
}
let writet = WriterThread()
let printt = PrinterThread()
printt.start()
writet.start()
您可以在发送操作时使用 DispatchQueue
并调用 .suspend()
,并使用获取响应调用 .resume()
的代码。然后无论你想在继续之前等待响应,只需放置一个虚拟 queue.sync({ print("done waiting")})
它会自动等待直到 .resume()
在打印和继续之前被调用。
import Dispatch
var queue = DispatchQueue(label: "queue")
func sendOperationToIoTDevice(){
//send the operation
//...
queue.suspend()
}
...
//whatever code gets the response:
//get response
//...
queue.resume()
...
//main code
sendOperationToIoTDevice()
queue.sync { print("done waiting") } // will hang here until .resume() is called