2016-06-30 24 views
0

私はGoogleの意識のAPIをいくつか取り乱しています。今ではRxJavaの理解が私を制限しています。Returnコールバックで観測可能rxjava

最後に達成したいこと: 私は天気と場所をApiから取得し、それらを1つのオブジェクトに結合して更新のために渡すことができます。

しかし、戻り値の型がvoidであることからapiコールバックからObservableをどのように返すか、api.getWeatherとapi.getLocationからweatherオブジェクトとlocationオブジェクトをマージする方法はわかりません私のプロジェクトで私の他のもののため

public void requestUserCurrentInfo() { 
    Subscription userInfo = getWeatherLocation().subscribeOn(Schedulers.io()) 
      .observeOn(AndroidSchedulers.mainThread()).subscribe(userinfo -> 
        Log.d(TAG,userinfo.something())); 
} 

public Observable<UserInfo> getWeatherLocation() { 
    try { 
     Awareness.SnapshotApi.getWeather(client) 
       .setResultCallback(weather -> { 
        if (!weather.getStatus().isSuccess()) { 
         Log.d(TAG, "Could not get weather"); 
         return; 
        } 
        //How do I do here? 
        return weather.getWeather(); 
       }); 


     Awareness.SnapshotApi.getLocation(mGoogleApiClient) 
      .setResultCallback(retrievedLocation -> { 
       if(!retrievedLocation.getStatus().isSuccess()) return; 
       Log.d("FRAG", retrievedLocation.getLocation().getLatitude() + ""); 
      }); 


    } catch (SecurityException exception) { 
     throw new SecurityException("No permission " + exception); 

    } 

} 

、私はすべてのステップが観察< SmhiResponseを返すため、私はこのようにそれを得ることができ、リポジトリパターン以下のREST APIを介していくつかのものを得る>

getWeatherSubscription = getWeatherUsecase.execute().subscribeOn(Schedulers.io()) 
      .observeOn(AndroidSchedulers.mainThread()).subscribe(
        smhiResponseModel -> {Log.d(TAG,"Retrieved weather"); locationView.hideLoading();}, 
        err -> {Log.d(TAG,"Error fetching weather"); locationView.hideLoading();} 
      ); 

答えて

1

を観測あなたは、コールバックから、観察を返しますが(未テスト)それらを組み合わせ可能にするために観測にあなたのコールバックをラップしていない:

Observable<WeatherResult> weatherObservable = Observable.create(subscriber -> { 
     Awareness.SnapshotApi.getWeather(client) 
       .setResultCallback(weather -> { 
        if (!weather.getStatus().isSuccess()) { 
         subscriber.onError(new Exception("Could not get weather.")); 
         Log.d(TAG, "Could not get weather"); 
        } else { 
         //How do I do here? 

         subscriber.onNext(weather); 
         subscriber.onCompleted(); 
        } 
       }); 
    }); 

    Observable<LocationResult> locationObservable = Observable.create(subscriber -> { 
     Awareness.SnapshotApi.getLocation(mGoogleApiClient) 
       .setResultCallback(retrievedLocation -> { 
        if(!retrievedLocation.getStatus().isSuccess()) { 
         subscriber.onError(new Exception("Could not get location.")); 
        } else { 
         Log.d("FRAG", retrievedLocation.getLocation().getLatitude() + ""); 
         subscriber.onNext(retrievedLocation); 
         subscriber.onCompleted(); 
        } 
       }); 
    }); 

は今、それらを組み合わせて.combineLatest()または.zip()経由:

Observable<CombinedResult> combinedResults = Observable.zip(weatherObservable, locationObservable, 
      (weather, location) -> { 
       /* somehow combine weather and location then return as type "CombinedResult" */ 
      }); 

それらのどれもが実行されなかっますそれ以外の場合は、登録することを忘れないでください:

combinedResults.subscribe(combinedResult -> {/*do something with that stuff...*/}); 
+0

ありがとう魅力があり、このように書くと非常に意味をなさない – buddhabath

+0

Zこの場合、どのようにして購読解除を管理するのですか? – buddhabath

+0

終了すると、購読者に対してonErrorまたはonCompletedが実行されます。あなたはすでにすべての要素(実際にはただ一つ)を受け取っているか、エラーがスローされたため、とにかく何かをしてください。 –

0
Observable.combineLatest(getWeather(), getLocation(), new Func2<List<Object_A>, List<Object_B>, Object>() { 
      @Override 
      public Object call(Object o, Object o2) { 
       combine both results and return the combine result to observer 
      } 
     }) 

getweather()とのgetLocation()の戻りが

+0

あなたが意味するかわからないが、APIからの実際の方法が見えます Awareness.SnapshotApi.getWeather(mGoogleApiClient) .setResultCallback(新しいResultCallback (){@Override ます。public voidはonResult(のような@NonNull WeatherResult weatherResult){ if(!weatherResult.getStatus()。isSuccess()){ return; } 天気weather = weatherResult.getWeather(); } }); – buddhabath

+0

私はgetweather()とgetlocation()がそれぞれobservableを返さなければならないことを意味し、combinelatestを使用してこれらの2つのobservableを1つのobservableにマージします –

+0

これまでのところあなたと一緒ですが、気象や位置を観測として返そうとすると文句を言います。 Observable.just(天気予報)またはObservable.create ...を使って、どうすればいいですか? – buddhabath

関連する問題