LiveData onChanged 方法仅在第一次调用

use*_*549 5 android retrofit2 android-livedata

我有一个简单的应用程序,可以轮询服务以在回收器视图中显示国家/地区列表。在这里,每当国家/地区列表发生任何变化时,我都会使用 LiveData 更新回收器视图。问题是,LiveData 的 onChanged 方法仅在第一次调用 setValue 时才会被调用。但之后如果数据有任何进一步的更改,onChanged 就不会被调用?

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

国家列表片段

        //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);
Run Code Online (Sandbox Code Playgroud)

乡村视图模型

公共类 CountryViewModel 扩展 AndroidViewModel { 私有 MutableLiveData> mCountryList; 私人 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;
}
Run Code Online (Sandbox Code Playgroud)

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());
            }
        }
Run Code Online (Sandbox Code Playgroud)
        @Override
        public void onFailure(Call<List<CountryModel>> call, Throwable t) {
            t.printStackTrace();
        }
    });
    return lstResult;
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

编辑:

一些额外的观察结果-当我在 CountryViewModel 中调用 MutableLiveData 实例(mCountryList)的 setValue 方法时,它每次都会调用 onChanged 方法。

然而,对于 MyRetrofitClient 来说,情况有所不同。第一次在 MyRetrofitClient.getCountryList() 中调用 setValue 时,它​​会调用 onChanged 方法。但后来就没有了。

Yog*_*tia 0

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

您没有收到任何更改,因为您从未致电setValuemCountryList

方法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();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

用来getCountryListener()观察。

活动:

countryViewModel.getCountryListener().observe(this,myObserver);
Run Code Online (Sandbox Code Playgroud)