LiveData onChanged方法只在第一次调用

时间:2018-04-22 09:14:45

标签: android retrofit2 android-livedata

我有一个简单的应用程序,它轮询服务以在recyclerview中显示国家/地区列表。在这里,只要国家/地区列表中有任何更改,我就会使用LiveData来更新recyclerview。麻烦的是,LiveData的onChanged方法仅在第一次调用setValue时被调用。但是之后如果对数据有任何进一步的改变,就不会调用onChanged ??

以下是我的代码以获取更多信息 -

CountryListFragment

        //Setup observer
    Observer<List<CountryModel>> myObserver = new Observer<List<CountryModel>>() {
        @Override
        public void onChanged(@Nullable List<CountryModel> countryModels) {
            mCountryList = countryModels;
            //Update recyclerview
            myAdapter.updateRecyclerView(mCountryList);
        }
    };
    //Set Observer for Viewmodel
    countryViewModel.getmCountryList().observe(this,myObserver);

CountryViewModel

  

公共类CountryViewModel扩展AndroidViewModel {       private MutableLiveData&gt; mCountryList;       private MyRetrofitClient myRetrofitClient;

public CountryViewModel(@NonNull Application application) {
    super(application);
}

public void init(){
    mCountryList = new MutableLiveData<>();
    myRetrofitClient = new MyRetrofitClient();
    **mCountryList = myRetrofitClient.getCountryList();   //This works**
    pollCountryList();
}

//Create polling request for every 10 secs
private void pollCountryList(){
    final Handler mHandler = new Handler();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0; i<30;i++){
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //Call API on main thread
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            **myRetrofitClient.getCountryList();  //NOT CALLING ONCHANGED??**
                        }
                    });
                }

            }
        }).start();
}

public MutableLiveData<List<CountryModel>> getmCountryList() {
    return mCountryList;
}

MyRetrofitClient.getCountryList()

public MutableLiveData<List<CountryModel>> getCountryList(){
    final MutableLiveData<List<CountryModel>> lstResult = new MutableLiveData<>();
    MockServiceInterface serviceInterface = mRetrofit.create(MockServiceInterface.class);
    Call<List<CountryModel>> countryList = serviceInterface.getCountryList();
    countryList.enqueue(new Callback<List<CountryModel>>() {
        @Override
        public void onResponse(Call<List<CountryModel>> call, Response<List<CountryModel>> response) {
            if (response.isSuccessful()){
                List<CountryModel> lstResponse = response.body();
                lstResult.setValue(lstResponse);
            }else {
                System.out.print(response.errorBody());
            }
        }
        @Override
        public void onFailure(Call<List<CountryModel>> call, Throwable t) {
            t.printStackTrace();
        }
    });
    return lstResult;
}

谢谢!

修改

一些额外的意见 - 当我在CountryViewModel中调用MutableLiveData实例(mCountryList)的setValue方法时,它每次调用onChanged方法。

然而,在MyRetrofitClient的情况下它有所不同。第一次在MyRetrofitClient.getCountryList()中调用setValue时,它会调用onChanged方法。但后来却没有。

1 个答案:

答案 0 :(得分:0)

抱歉,我一开始误解了问题。

您没有收到更改,因为您从未在setValue上致电mCountryList

方法getCountryList()正在返回新对象MutableLiveData<List<CountryModel>> lstResult所有被调用的对象,没有人在观察。

<强>解决方案:

不要使用MutableLiveData返回getCountryList个对象,而是在mCountryList中设置onResponse()

<强>代码

public void init(){
    mCountryList = new MutableLiveData<>();
    myRetrofitClient = new MyRetrofitClient();
    myRetrofitClient.getCountryList();
    pollCountryList();
}

public LiveData<List<CountryModel>> getCountryListener() {
    return mCountryList;
}

public void getCountryList(){
    MockServiceInterface serviceInterface = mRetrofit.create(MockServiceInterface.class);
    Call<List<CountryModel>> countryList = serviceInterface.getCountryList();
    countryList.enqueue(new Callback<List<CountryModel>>() {
        @Override
        public void onResponse(Call<List<CountryModel>> call, Response<List<CountryModel>> response) {
            if (response.isSuccessful()){
                List<CountryModel> lstResponse = response.body();
                mCountryList.setValue(lstResponse);
            }else {
                System.out.print(response.errorBody());
            }
        }
        @Override
        public void onFailure(Call<List<CountryModel>> call, Throwable t) {
            t.printStackTrace();
        }
    });
}

使用getCountryListener()进行观察。

<强>活动:

countryViewModel.getCountryListener().observe(this,myObserver);