我遇到问题了解或使用Dispatchgroup。我已经阅读了很多关于它们的内容,但是大多数示例/文档都非常模糊,或者与我想要做的不一样,但是每次我提到我的问题时,大家都会说“使用派送组”。DispatchGroup.wait不等待
这里就是我想要做的(注:顺序是至关重要的):
- 发送蓝牙写入特性。
- 设备接收值,并吐出的东西响应
- 读取蓝牙响应(通过读特性)
- 发送一条新的写入特性(不同的命令)
- 设备接收到新的命令,吐出NEW数据响应
重复两次(总共3个命令,总共3个不同的响应)。
我的代码:
func tPodInitialSetUp()
{
print ("* * * * * BEGIN SET-UP * * * * *")
let setupDispatchGroup = DispatchGroup()
setupDispatchGroup.enter()
self.writeValue(command: Data(CommandModeCmd)) //231: Put t-Pod in command mode, burst mode is OFF returns OK
setupDispatchGroup.leave()
setupDispatchGroup.wait()
setupDispatchGroup.enter()
deviceConnected?.readValue(for: deviceConnectedCh1n2Char!)
print("Sent command 231: returned: \(receivedString1)")
if receivedString1.lowercased() == "ok"
{
print("t-Pod burst mode is OFF")
}
setupDispatchGroup.leave()
setupDispatchGroup.wait()
setupDispatchGroup.enter()
self.writeValue(command: Data(loadProbeCalCmd)) //202: load calibration constants of probe, returns ok or 0
setupDispatchGroup.leave()
setupDispatchGroup.wait()
setupDispatchGroup.enter()
deviceConnected?.readValue(for: deviceConnectedCh1n2Char!)
print("Sent command 202: returned: \(receivedString1)")
if receivedString1.lowercased() == "ok"
{
print("Probe Constants loaded")
}
if receivedString1 == "0"
{
print("No probe connected")
}
setupDispatchGroup.leave()
setupDispatchGroup.wait()
setupDispatchGroup.enter()
self.writeValue(command: Data(probeSNCmd)) //205: load probe serial number
setupDispatchGroup.leave()
setupDispatchGroup.wait()
setupDispatchGroup.enter()
deviceConnected?.readValue(for: deviceConnectedCh1n2Char!)
print("Sent command 205: returned: \(receivedString1)")
if (receivedString1.count == 6)
{
print("received Probe SN: \(receivedString1)")
probeSN = receivedString1
}
setupDispatchGroup.leave()
setupDispatchGroup.notify(queue: .main)
{
tPodSN = String(describing: connectedDeviceName!.dropFirst(7))
print ("* * * SET-UP COMPLETE * * *")
self.writeValue(command: Data(resetCmd)) //200: resets t-Pod
self.writeValue(command: Data(beaconOffCmd)) //211: turns beacon off (temperature output)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5)
{
self.dataDisplaySubView.isHidden = false
print ("Adding observers!")
NotificationCenter.default.addObserver(self, selector: #selector(self.updateIncomingData), name: NSNotification.Name(rawValue: DATA_PARSED), object: nil) //Run every time you receive data from BLE
NotificationCenter.default.addObserver(self, selector: #selector(self.calculateTNU), name: NSNotification.Name(rawValue: TOGGLESWITCH_TOGGLED), object: nil) //Run in case the toggle switches change and data needs to be re-calculated
NotificationCenter.default.addObserver(self, selector: #selector(self.parseReceivedData), name: NSNotification.Name(rawValue: DEVICE_FINISHED_SENT_DATA), object: nil) //Run every time you receive the notification that the whole data has been sent
}
}
这就要求它有下面的代码并确认蓝牙写命令:
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor descriptor: CBDescriptor, error: Error?) {
guard error == nil else {
print("Error writing descriptor: " + (error?.localizedDescription)!)
return
}
print("Descriptor Value sent")
}
现在,这里是我的输出:
* * * * * BEGIN SET-UP * * * * *
***** WRITING *****
Wrote: 1 bytes
***** WRITING *****
Wrote: 1 bytes
Sent command 231: returned: **T-Pod-9Ch**
***** WRITING *****
Wrote: 1 bytes
Sent command 202: returned: **T-Pod-9Ch**
***** WRITING *****
Wrote: 1 bytes
Sent command 205: returned: **T-Pod-9Ch**
* * * SET-UP COMPLETE * * *
***** WRITING *****
Wrote: 1 bytes
***** WRITING *****
Wrote: 1 bytes
Characteristic Value sent
Adding observers!
Characteristic Value sent
Characteristic Value sent
Characteristic Value sent
Characteristic Value sent
Clearing TNU Array
现在,和你一样可以看到“特征值发送”是蓝牙功能在发送值时给出的确认,但是这个输出是在它完成运行整个代码之后创建的,所以基本上它把这些命令放在一些管道中,忘记了它们做了其他事情,那么发送的命令,因此我读的回应都是废话!正如你可以看到所有收到的字符串都是T-Pod-9Ch(这只是它的正常脉冲输出),我应该从命令中得到的预期响应是OK,OK和一个6位数字(按此顺序)。
请大家帮忙,我已经读了很多次关于派遣组应该如何工作,但我不能让他们做我想做的事情。
你进入,离开,和在一个单独的线程上等待组。这完全没有意义。每次你调用wait(),因为你已经离开了组,所以组的计数器是'0'。 –
你能详细说明一下吗?我真的不知道它应该如何格式化,然后从我所了解的每个单独的任务必须在enter/leave命令中,我需要它们等待它完成每个单独的命令。 –
调度队列用于在线程。通常你可以这样使用它们:1)在当前线程上调用'enter()',2)启动一些异步任务和/或在另一个线程或调度队列上运行一些代码。3)让async任务调用'leave()当完成时,4)重复执行任何其他需要完成的任务,并且5)当所有的'enter()'调用通过'leave()'平衡时,使用'notify()'使一些代码运行。调用。 –