gpt4 book ai didi

swift - DispatchQueue会互相等待吗?

转载 作者:搜寻专家 更新时间:2023-11-01 06:31:42 25 4
gpt4 key购买 nike

我有一个函数要用 Swift (X-Code) 制作。我基本上是通过蓝牙发送一些命令,我​​对 Main Asyncafter 的工作原理有疑问。这是我的设置代码:

func tPodSetUp()
{
var delayForResponse = DispatchTime.now() + 0.4 //seconds to wait for response
writeValue(data: "231") //Put t-Pod in command mode, burst mode is OFF returns OK
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if receivedString1.lowercased() == "ok"
{
print("t-Pod burst mode is OFF")
}
}

writeValue(data: "202") //load calibration constants of probe, returns ok or 0
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if receivedString1.lowercased() == "ok"
{
print("calibration Loaded")
}
if receivedString1 == "0"
{
print("No probe connected")
}
}
}

我希望它基本上执行以下操作(按此顺序):
写值
等待 .4 秒
读取响应/检查响应
writeValue(在它完成读取/检查响应之后)
读取响应/检查响应

我担心,如果代码是现在这样,它会在等待其他值时超过 writeValues,并将它们放在异步运行的单独线程上。
另外,让我感到困惑的是,我在一开始就声明了 delayForResponse,并说每次调用时都会改变吗?就像如果我在 12:00:00:00 执行它,它现在将完成 + .4 秒(所以它应该在 12:00:00:40 被调用,但是在 12:00:00 会发生什么: 41 当它运行第二部分时(有另一个对 delayForResponse 的标注)它会突然说“什么?这本应该在 .01 秒前完成的!?

编辑 这是根据一些反馈对代码进行的另一种处理,这会按照我的想法进行吗?

let setupQueue = DispatchQueue(label: "setupQueue")
let delayForResponse = DispatchTime.now() + 0.4 //seconds to wait for response

setupQueue.sync {
writeValue(data: String(UnicodeScalar(UInt8(231)))) //231: Put t-Pod in command mode, burst mode is OFF returns OK
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if receivedString1.lowercased() == "ok"
{
print("t-Pod burst mode is OFF")
}
}

writeValue(data: String(UnicodeScalar(UInt8(202)))) //202: load calibration constants of probe, returns ok or 0
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if receivedString1.lowercased() == "ok"
{
print("t-Pod burst mode is OFF")
}
if receivedString1 == "0"
{
print("No probe connected")
}
}

writeValue(data: String(UnicodeScalar(UInt8(204)))) //204: load t-Pod serial number
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if (receivedString1.count > 5)
{
print("received t-Pod SN: \(receivedString1)")
tPodSN = receivedString1
}
}

writeValue(data: String(UnicodeScalar(UInt8(205)))) //205: load probe serial number
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if (receivedString1.count > 3)
{
print("received Probe SN: \(receivedString1)")
probeSN = receivedString1
}
}

//AFTER FINISHING SETUP, RESET TPOD AND TURN BEACON OFF

writeValue(data: String(UnicodeScalar(UInt8(202)))) //200: resets t-Pod
writeValue(data: String(UnicodeScalar(UInt8(202)))) //211: turns beacon off (temperature output)
}

最佳答案

你检查过DispatchGroup了吗??

func myFunction() {
var a: Int?

let group = DispatchGroup()
group.enter()

DispatchQueue.main.async {
a = 1
group.leave()
}

// does not wait. But the code in notify() is run
// after enter() and leave() calls are balanced

group.notify(queue: .main) {
print(a)
}
}

关于swift - DispatchQueue会互相等待吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46434491/

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