简体   繁体   English

使用 Retrofit 请求响应刷新实时数据

[英]Refreshing livedata with Retrofit request response

I have a simple app, which shows a list of movies.我有一个简单的应用程序,它显示电影列表。 When a user clicks a button it should refresh the list, which means to go to Retrofit again and fetch the new list.当用户单击按钮时,它应该刷新列表,这意味着再次转到 Retrofit 并获取新列表。 The problem is, that this refresh doesn't work.问题是,这种刷新不起作用。 It only fetches the list once, when I open the app.当我打开应用程序时,它只获取一次列表。

public class MoviesViewModel extends AndroidViewModel {
    private MutableLiveData<Resource<List<Movie>>> mLastdMovieListObservable;
    private Repository mRepository;

    public MoviesViewModel(Application application) {
        super(application);
        mRepository = new Repository();
    }

    public LiveData<Resource<List<Movie>>> getMovieListLiveData() {
        if (mLastdMovieListObservable == null) {
            mLastdMovieListObservable = mRepository.getMovieList();
        }
        return mLastdMovieListObservable;
    }
}

and the Repository :和存储库:

public MutableLiveData<Resource<List<Movie>>> getMovieList() {
    final MutableLiveData<Resource<List<Movie>>> data = new MutableLiveData<>();
    data.setValue(Resource.loading(null));

    mInterface.getMovies(.....).enqueue(new Callback<MovieListResponse>() {
        @Override
        public void onResponse(Call<MovieListResponse> call, Response<MovieListResponse> response) {
            if (response.isSuccessful()) {
                data.postValue(Resource.success(response.body().getResult()));
            } else {
                data.postValue(Resource.error(response.message(), null));
            }
        }
        @Override
        public void onFailure(Call<MovieListResponse> call, Throwable t) {
            data.postValue(Resource.error(t.getMessage(), null));
        }
    });

    return data;
}   

Then in the fragment it's just :然后在片段中它只是:

mMoviesViewModel.getMovieListLiveData().observe(this, moviesResource -> {
    if (moviesResource.status == Status.SUCCESS) {
       .....
       .....

then the button click part, when I need to fetch the list again, I'm not sure how to implement it.然后按钮单击部分,当我需要再次获取列表时,我不确定如何实现它。 Anything I tried didn't work.我试过的任何东西都不起作用。 the Repository returned the data as null right away. Repository 立即将数据返回为 null。

You can use retry approach and I changed your code quickly for an example of retry and check this source for better code.您可以使用重试方法,我快速更改了您的代码以获取重试示例,并检查此源以获得更好的代码。 LINK 关联

Transformations.switchMap listens to retry and when it changed, swithMap will be call again.And in UI ViewModel should be config like below but in button you need to call just retry. Transformations.switchMap 监听重试,当它改变时,swithMap 将再次调用。在 UI ViewModel 中应该像下面这样配置,但在按钮中你需要调用只重试。

mMoviesViewModel.getMovieListLiveData().observe(this, moviesResource -> {
if (moviesResource.status == Status.SUCCESS) {
   .....
   .....

And in button:并在按钮中:

mMoviesViewModel.retry()

Also ViewModel:还有视图模型:

public class MoviesViewModel extends AndroidViewModel {
private MutableLiveData<Resource<List<Movie>>> mLastdMovieListObservable;
private Repository mRepository;
private Boolean mRetry = false;

public MoviesViewModel(Application application) {
    super(application);
    mRepository = new Repository();
    mLastdMovieListObservable = Transformations.switchMap(mRetry){
        mLastdMovieListObservable = mRepository.getMovieList()
    }
}

public LiveData<Resource<List<Movie>>> getMovieListLiveData() {
    return mLastdMovieListObservable;
}

public void retry(){
    mRetry.setValue(true)
}}

Changes for movie types can be like below:电影类型的变化如下:

public class MoviesViewModel extends AndroidViewModel {
private MutableLiveData<Resource<List<Movie>>> mLastdMovieListObservable;
private Repository mRepository;
private Int mMovieType = 0;

public MoviesViewModel(Application application) {
    super(application);
    mRepository = new Repository();
    mLastdMovieListObservable = Transformations.switchMap(mMovieType, type->
        mLastdMovieListObservable = mRepository.getMovieList(type)
    );
}

public LiveData<Resource<List<Movie>>> getMovieListLiveData() {
    return mLastdMovieListObservable;
}

public void setMovieType(Int movieType){
    mMovieType.setValue(movieType)
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM