RxJava线程不等待结果

时间:2018-07-04 03:48:01

标签: android rx-java rx-java2 android-mvp

我有这种方法,试图从API中提取数据,然后更新文本视图。一切正常,但在“ end方法” log. .getRecipeName()使用RetroFit从API中提取后,getRecipeName不能完成。

我目前正在使用一次学习MVP,Dagger,RxJava和Butterknife Mindork's Github page on MVP Architecture

我将.subscribeOn和.observeOn注释掉,以查看结果差异,但没有任何变化。

@Override
public void onRandomButtonClicked() {

    getMvpView().showLoading();
    Log.e(TAG, "Random Method Open");
    getCompositeDisposable().add(getDataManager()
            .getRecipeName()
            //.subscribeOn(getSchedulerProvider().io())
            //.observeOn(getSchedulerProvider().ui())
            .subscribe(new Consumer<String>() {
                @Override
                public void accept(String s) throws Exception {
                    Log.e(TAG, "accept");
                    getMvpView().updateTextView(title);
                }
            }));

    Log.e(TAG, "end method");

}

这是我的getRecipeName()方法

@Override
public Observable<String> getRecipeName() {

    /*Create handle for the RetrofitInstance interface*/
    GetDataService service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class);
    Call<RecipeList> call = service.getRecipe();
    call.enqueue(new Callback<RecipeList>() {
        @Override
        public void onResponse(@NonNull Call<RecipeList> call, @NonNull retrofit2.Response<RecipeList> response) {


            Log.e("onResponse","Recipe is Successful = " + response.isSuccessful());
            //if response is false then skip to avoid null object reference
            if (response.isSuccessful()) {

                RecipeList drinkRecipe = response.body();

                List<Recipe> recipes = drinkRecipe.getDrinks();
                jokeText = String.valueOf(recipes.size());
                Recipe myRecipe = recipes.get(0);
                jokeText = myRecipe.getStrDrink();

                Log.e("On Response", "Result2: " + jokeText);
            }
            //jokeText = "null";

        }

        @Override
        public void onFailure(Call<RecipeList> call, Throwable t) {
            Log.e("On Response","Failure");
        }
    });

    //return jokeText;
    return Observable.fromCallable(new Callable<String>() {
        @Override
        public String call() throws Exception {
            return jokeText;
        }
    });
}

解决方案

因此,正如评论所述,RxJava Adapter是正确的方法。我将使用适配器将自己的工作代码发布在自己身上。我发现很难找到一个可行的例子。

//single api call using retrofit and rxjava
@SuppressLint("CheckResult")
private void getRandomButtonClick(){

    retrofit = RetrofitClientInstance.getRetrofitInstance();

    retrofit.create(GetDataService.class).getRecipe()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(this::handleResults, this::handleError );
}

private void handleResults(RecipeList recipeList) {
    int i = recipeList.getDrinks().size();
    Log.e(TAG, "size is: "+ i);
    Recipe recipe = recipeList.getDrinks().get(0);
    getMvpView().updateTextView(recipe.getStrDrink());
}

 private void handleError(Throwable t){
    Log.e("Observer", "");

}

我的改造客户端实例

 public static Retrofit getRetrofitInstance() {
    if (retrofit == null) {
        retrofit = new retrofit2.Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }
    return retrofit;
}

我的界面

public interface GetDataService {

//@Headers({})
@GET("random.php")
Observable<RecipeList> getRecipe();

我找到了一个很好的资源供我正确实施。 Retrofit Android

1 个答案:

答案 0 :(得分:1)

原因是因为您的可观察对象每次订阅都返回jokeText。调用后立即返回,并且不会等待您的网络操作。

一种可能的解决方案是使用RxJavaCallAdapter。链接到这里:https://github.com/square/retrofit/tree/master/retrofit-adapters/rxjava2

它将自动将您的API返回值转换为可观察值。无需手动调用改造请求。只需处理响应,然后从那里将其转换为所需的对象即可。

另一种方法是将整个序列包装在Observable.createObservable.fromAsync中。