数据更改时LiveData不更新 [英] LiveData not updating when data changes

查看:85
本文介绍了数据更改时LiveData不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LiveData从服务器获取数据并进行观察.我的 onChanged()方法只是第一次调用,而在服务器中的数据更新时不会调用.

I'm using LiveData to fetch data from a server and observe it. My onChanged() method just gets called the first time, and does not get called when data in the server gets updated.

UserFragment:

UserViewModel userViewModel = ViewModelProviders.of(this).get(UserViewModel.class);
userViewModel.getUser().observe(this, new Observer<User>() {
    @Override
    public void onChanged(User user) {
        //Set UI
    }
});

UserViewModel:

public class UserViewModel extends AndroidViewModel {
    private LiveData<User> user;

    public UserViewModel(Application application) {
        super(application);
        user = UserRepository.getInstance().fetchUser();
    }

    public LiveData<User> getUser() {
        return user;
    }    
}

用户存储库:

public class UserRepository {    
    private ApiService apiService;
    private static UserRepository userRepository;

    private UserRepository() {
        apiService = RestClient.getClient().create(ApiService.class);
    }

    public synchronized static UserRepository getInstance() {
        if (userRepository == null) userRepository = new UserRepository();
        return userRepository;
    }

    public LiveData<User> fetchUser() {
        final MutableLiveData<User> data = new MutableLiveData<>();
        Call<User> call = apiService.getUser();
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(@NonNull Call<User> call, @NonNull Response<User> response) {
                if (response.body() != null) {
                    data.postValue(response.body());
                }
            }

            @Override
            public void onFailure(@NonNull Call<User> call, @NonNull Throwable t) {
                data.postValue(null);
                t.printStackTrace();
            }
        });
        return data;
    }
}

推荐答案

问题在于,每次调用fetchUser都会创建一个新的 LiveData<> .

The issue is that fetchUser creates a new LiveData<> every time you call it.

这意味着您的第一个将永远不会收到更新.

This means that your first one will never receive an update.

请看看这些...

public class UserRepository {    
    private ApiService apiService;
    private static UserRepository userRepository;

    private UserRepository() {
        apiService = RestClient.getClient().create(ApiService.class);
    }

    public synchronized static UserRepository getInstance() {
        if (userRepository == null) userRepository = new UserRepository();
        return userRepository;
    }

    // Your example code
    public LiveData<User> fetchUser() {
        // Your problem lies here. Every time you fetch user data, you create a new LiveData.
        // Instead, fetch user should update the data on a pre-existing LiveData.
        final MutableLiveData<User> data = new MutableLiveData<>();
        Call<User> call = apiService.getUser();
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(@NonNull Call<User> call, @NonNull Response<User> response) {
                if (response.body() != null) {
                    data.postValue(response.body());
                }
            }

            @Override
            public void onFailure(@NonNull Call<User> call, @NonNull Throwable t) {
                data.postValue(null);
                t.printStackTrace();
            }
        });
        return data;
    }

    // My alterations below:
    private MutableLiveData<User> userLiveData = new MutableLiveData<>();

    public LiveData<User> getUser() {
        return userLiveData;
    }

    public LiveData<User> fetchUser2() {
        Call<User> call = apiService.getUser();
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(@NonNull Call<User> call, @NonNull Response<User> response) {
                if (response.body() != null) {
                    userLiveData.postValue(response.body());
                }
                // TODO: Consider a fallback response to the LiveData here, in the case that bad data is returned. Perhaps null?
            }

            @Override
            public void onFailure(@NonNull Call<User> call, @NonNull Throwable t) {
                userLiveData.postValue(null);
                t.printStackTrace();
            }
        });
        return userLiveData;
    }
}

ViewModel

我也会对此稍作更改.与其观察获取,不如直接观察LiveData.

ViewModel

I would also change this slightly. Instead of observing fetch, I would observe the LiveData directly.

user = UserRepository.getInstance().getUser();

稍后,您可以随时从服务器请求更新的数据.

Later, you can request updated data from the server at any point.

UserRepository.getInstance().fetchUser2();

您还可以在 UserRepository 的第一个结构上调用 fetchUser2().然后只有更新会直接调用 fetchUser2().

You could also call fetchUser2() on the first construction of UserRepository. Then only updates would call fetchUser2() directly.

private UserRepository() {
    apiService = RestClient.getClient().create(ApiService.class);
    fetchUser2();
}

片段

此外,在您的片段中,请勿在 this 上进行观察.而是使用 getViewLifecycleOwner()

Fragment

Also, in your Fragment, do not observe on this. Instead use getViewLifecycleOwner()

userViewModel.getUser().observe(getViewLifecycleOwner(), new Observer<User>() {
    @Override
    public void onChanged(User user) {
        //Set UI
    }
});

这篇关于数据更改时LiveData不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆