2017-09-26 82 views
1

我有一個函數,我試圖在Swift(X-Code)中做出。我基本上通過藍牙發送一些命令,並且我對主Asyncafter的工作方式有疑問。這裏是我的設置代碼:DispatchQueues會相互等待嗎?

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
等待0.4秒
讀取響應/檢查響應
writeValue(讀完後閱讀/檢查RESPONSE)
讀響應/檢查響應

恐怕如果代碼是因爲它是現在它只會溢出,同時等待其他的人也就有了在writeValues m在異步運行的單獨線程上運行。
另外,讓我感到困惑的是,我在開始時聲明瞭delayForResponse,並且說每次調用它時都會改變它?就像我在12:00:00:00時那樣,它會使它現在變爲+ .4秒(所以它應該在12:00:00:40之間調用,但是在12:00:00會發生什麼: 41,當它運行的第二部分(即有另一個標註到delayForResponse)會突然說:「什麼?這應該0.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) 
} 

回答

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) 
    } 
} 
+1

打我吧。這是我會做的。 –

+0

嗨,我讀了一些關於調度組的消息,但它似乎並行運行它們,我不想並行運行,我希望它們運行SEQUENTIAL。主要原因是因爲你可以從我的代碼中讀取我從receviedString1中讀取的內容。這有BLE外圍設備的響應,如果我在讀取其響應之前發送了另一個命令,我可能正在讀取其他內容的響應,這是我想要避免的 –

+0

檢查此https://stackoverflow.com/questions/37805885/如何創建調度隊列在迅速3 – casillas