LiveData最初不显示数据

时间:2018-12-04 13:52:47

标签: java android android-architecture-components android-livedata android-viewmodel

我正在从服务器中获取一些数据,并使用LiveData对其进行观察。当我的片段最初开始时,它不会在cellForRow中返回任何内容,并且在切换片段之后,它将触发具有正确数据的func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { //let rowpath = indexPath.row let cell = tableView.dequeueReusableCell(withIdentifier: "CheckList") as! TableViewCell let data = self.deviceData[indexPath.row] cell.lbTitle.text = "\(data.brandName) \(data.modelName)" cell.lbDetail.text = "\(data.operatorName) \(data.version), \(data.browserName) \(data.version)" cell.btnCheckMark.isSelected = data.isSelected cell.callback { button in self.deviceData[indexPath.row].isSelected = button.isSelected // or if deviceData is a class with reference semantics // data.isSelected = button.isSelected } return cell }

调试之后,我看到我的API调用成功,并且在那里获取了数据,但最初并没有通过onChanged()返回数据。

存储库:

onChanged()

ViewModel:

onChanged()

片段:

public class UserRepository {
    private ApiService apiService;
    private static UserRepository repository;
    private MutableLiveData<User> user = new MutableLiveData<>();

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

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

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

    public void fetchUser() {
        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) user.postValue(response.body());
            }

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

我可以通过在观察片段之前调用public class UserViewModel extends AndroidViewModel { private LiveData<User> user; public UserViewModel(@NonNull Application application) { super(application); user = UserRepository.getInstance().getUser(); } public LiveData<User> getUser() { return user; } } 来解决此问题。但这不是Google所说的首选方法,并且是一种解决方法。

1 个答案:

答案 0 :(得分:1)

正如注释中已经提到的,您甚至在fetchUser()中设置任何值之前似乎都在存储库上调用getUser()。更改fetchUser()使其返回LiveData,然后从视图模型而不是getUser()调用该方法:

public LiveData<User> fetchUser() {
    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) user.postValue(response.body());
        }

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

    return user;
 }

然后在视图模型中:

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