gpt4 book ai didi

swift - 创建包装 observable 的 Observable

转载 作者:行者123 更新时间:2023-11-30 12:31:06 25 4
gpt4 key购买 nike

我有一个返回可观察值的方法。

在此方法中,我订阅了 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()
}
}

你知道为什么吗?

最佳答案

你可以试试这个:

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)
observer.onCompleted() // <- Call it here
}
}
}, onError: { error in
LOG_SENSOR_MANAGER("getWifiConfigFromCharacteristic. onError: \(error.localizedDescription)")
observer.onError(error)
})
.disposed(by: strongSelf.disposeBag)
}

return Disposables.create()
}
}

编辑:改善外观和感觉。您可以使用 guard 对 self 进行强引用:

private func getWifiConfig() -> Observable<String?> {

return Observable.create { [ weak self ] observer in

guard let `self` = self else { return } // strong reference to self. Now you can use self as strongSelf

// subscription
let subscription = tileConnect.peripheral.connect()
.retry(.delayed(maxCount: 30, time: 1), scheduler: MainScheduler.instance)
.flatMap { $0.discoverServices([ self.serviceUUID ]) }
.flatMap { Observable.from($0) }
.flatMap { $0.discoverCharacteristics([ self.wifiCharacteristicUUID ])}
.flatMap { Observable.from($0) }
.flatMap { $0.readValue() }
.retry(.delayed(maxCount: 30, time: 1), scheduler: MainScheduler.instance)
.subscribe(onNext: {
if let data = $0.value,
let configDelimitedString = String(data: data, encoding: .utf8) {

observer.onNext(configDelimitedString)
observer.onCompleted() // <- Call it here
}
}, onError: { error in
LOG_SENSOR_MANAGER("getWifiConfigFromCharacteristic. onError: \(error.localizedDescription)")
observer.onError(error)
})


// Dispose the subscription
return Disposables.create {
subscription.dispose()
}
}
}

关于swift - 创建包装 observable 的 Observable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43551100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com