在回调rxjava中返回Observable



我在谷歌感知api上搞得一团糟,现在我对RxJava的理解限制了我

我最终想要实现的目标:我想从Api获得天气和位置,并将它们合并为一个对象,我可以将其传递到我的视图中进行更新。

然而,我不确定如何从这里的api回调中返回Observable,因为它具有void返回类型,以及如何从api.getWeather和api.getLocation 中实现天气和位置对象的合并

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

对于我的项目中的其他事情,我通过遵循存储库模式的RESTapi获得一些东西,然后我可以这样获得它,因为每个步骤都返回Observable<SmiResponse>

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

您不会从回调中返回可观察项,而是将回调打包为可观察项以使其可组合(未经测试(:

    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...*/});
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((返回可观察性

相关内容

  • 没有找到相关文章

最新更新