続・AsyncBluetoothのNotifyがおかしい
先日修正したつもりだったが、まだおかしかった。
ちなみに先日の件はoobaさんによって修正PRが投げられ、すでにmergeさている🙏
今回発見した問題は、高速にNotifyによってデータを受け取っていると、AsyncBluetoothのせいで一部データをロストするというやばいやつ。
問題の詳細
具体的には、Peripheral.swift の
func peripheral(_ cbPeripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
....
}
この peripheral(_:didUpdateValueFor:error) メソッドが呼ばれた直後ではcharacteristic.valueに所望のデータが入っているのだが、
self.context.characteristicValueUpdatedSubject.send(
Characteristic(characteristic)
)
で送信され、
subscription = peripheral.characteristicValueUpdatedPublisher.sink(receiveValue: { [weak self] characteristic in
...
})
で受け取ったcharacteristicの中身を見ると、あとから didUpdateValueFor で届いたデータによって上書きされているということが多発した。
最後まで読んでいただきありがとうございます!もし参考になる部分があれば、スキを押していただけると励みになります。 Twitterもフォローしていただけたら嬉しいです。 https://twitter.com/shu223/