DispatchQueues会互相等待吗?

时间:2017-09-26 19:37:23

标签: swift xcode bluetooth-lowenergy

我有一个函数我试图在Swift(X-Code)中制作。我基本上是通过蓝牙发送一些命令,我​​对主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")
            }
        }
    }

我希望它基本上可以执行以下操作(按此顺序):
writeValue
等.4秒 读取响应/检查响应
writeValue(完成阅读/检查反应后)
阅读回复/检查回复

我担心如果代码就像现在一样,它会在等待其他代码时超出writeValues,并将它们放在异步运行的不同线程上。
另外,让我感到困惑的是,我在开始时宣布了delayForResponse,并说这会在每次被调用时发生变化吗?就像我在12:00:00:00那样做它现在+4秒(所以它应该在12:00:00:40调用,但那么将在12:00:00发生的事情: 41当它运行第二部分(又有另一个呼叫延迟转换响应)时,它会突然说'#34;什么?这应该是已经完成的.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)
} 

1 个答案:

答案 0 :(得分:3)

您检查过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)
    }
}
相关问题