2017-04-21 99 views
0

我有一個返回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() 
    } 
    } 

你有一個想法,爲什麼?

+0

兩個想法:第一是以前'onCompleted()'第二它的錯誤每次出設置,並重新嘗試。 –

+0

你看過https://github.com/Polidea/RxBluetoothKit了嗎? –

+0

在你的'retry'和'subscribe'之間放置了一個'debug()'並且發佈了輸出。 –

回答

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

這沒有奏效。警衛讓自己......沒有被執行。你能解釋一下發生了什麼嗎? –

+0

用'guard let self = self else {return}'你可以訪問'self'而不是'self?'或'strongSelf'。你用保留字「self」創建一個強烈的'self'引用。 – XFreire