我有一個返回observable的方法。創建可觀察到的Observable
在這種方法中,我訂閱了rxbluetooth
observable。它運作良好,除了onComplete
永遠不會被調用。
這裏是我的代碼:
private func getWifiConfig() -> Observable<String?> {
return Observable.create { [ weak self ] observer in
if let strongSelf = self {
tileConnect.peripheral.connect()
.retry(.delayed(maxCount: 30, time: 1), scheduler: MainScheduler.instance)
.flatMap { $0.discoverServices([ strongSelf.serviceUUID ]) }
.flatMap { Observable.from($0) }
.flatMap { $0.discoverCharacteristics([ strongSelf.wifiCharacteristicUUID ])}
.flatMap { Observable.from($0) }
.flatMap { $0.readValue() }
.retry(.delayed(maxCount: 30, time: 1), scheduler: MainScheduler.instance)
.subscribe(onNext: {
if let data = $0.value {
if let configDelimitedString = String(data: data, encoding: .utf8) {
observer.onNext(configDelimitedString)
}
}
}, onError: { error in
LOG_SENSOR_MANAGER("getWifiConfigFromCharacteristic. onError: \(error.localizedDescription)")
observer.onError(error)
}, onCompleted: {
LOG_SENSOR_MANAGER("getWifiConfigFromCharacteristic. onCompleted")
observer.onCompleted() // <---- NEVER CALLED
}, onDisposed: {
LOG_SENSOR_MANAGER("getWifiConfigFromCharacteristic. onDisposed") // <---- NEVER CALLED
})
.disposed(by: strongSelf.disposeBag)
}
return Disposables.create()
}
}
你有一個想法,爲什麼?
兩個想法:第一是以前'onCompleted()'第二它的錯誤每次出設置,並重新嘗試。 –
你看過https://github.com/Polidea/RxBluetoothKit了嗎? –
在你的'retry'和'subscribe'之間放置了一個'debug()'並且發佈了輸出。 –