LiveData 不会在数据更改时更新 [英] LiveData not updating when data changes
问题描述
我正在使用 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.
用户片段:
UserViewModel userViewModel = ViewModelProviders.of(this).get(UserViewModel.class);
userViewModel.getUser().observe(this, new Observer<User>() {
@Override
public void onChanged(User user) {
//Set UI
}
});
用户视图模型:
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;
}
}
视图模型
我也会稍微改变一下.我会直接观察 LiveData,而不是观察 fetch.
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();
}
片段
另外,在你的 Fragment 中,不要在 this
上观察.而是使用 getViewLifecycleOwner()
userViewModel.getUser().observe(getViewLifecycleOwner(), new Observer<User>() {
@Override
public void onChanged(User user) {
//Set UI
}
});
这篇关于LiveData 不会在数据更改时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!