创建包含可观察的Observable

时间:2017-04-21 20:01:37

标签: swift rx-swift rxiosble

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

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

你知道为什么吗?

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

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强烈引用自我:

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()
        }
    }
}