首页 > 解决方案 > 为什么观察 LiveData 时不调用 onChanged()

问题描述

这个问题是对这个问题的后续处理:How to make retrofit API call using ViewModel and LiveData

该帖子回复中突出显示的错误 1 ​​和 2 已得到修复。对于错误 3,我还没有将 API 调用移动到存储库,但是一旦代码开始正常工作,我就会这样做。

所以我正在尝试使用 Retrofit 进行 API 调用,将 MVVM 与 LiveData 和 ViewModel 结合使用。API 调用(当前位于 ViewModel 中)正常工作,但 Activity 中的 Observer 没有接收到更改。

我已经将我的 ViewModel 观察者设置如下:

public class PopularGamesActivity extends AppCompatActivity {


private static final String igdbBaseUrl = "https://api-endpoint.igdb.com/";
private static final String FIELDS = "id,name,genres,cover,popularity";
private static final String ORDER = "popularity:desc";
private static final int LIMIT = 30;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_popular_games);

    PopularGamesViewModel popViewModel = ViewModelProviders.of(this).get(PopularGamesViewModel.class);
    popViewModel.getGameList().observe(this, new Observer<List<Game>>() {
        @Override
        public void onChanged(@Nullable List<Game> gameList) {
            String firstName = gameList.get(0).getName();
            Timber.d(firstName);
        }

我的 ViewModel 代码如下:

public class PopularGamesViewModel extends AndroidViewModel {

private static final String igdbBaseUrl = "https://api-endpoint.igdb.com/";
private static final String FIELDS = "id,name,genres,cover,popularity";
private static final String ORDER = "popularity:desc";
private static final int LIMIT = 30;


public PopularGamesViewModel(@NonNull Application application) {
    super(application);

}

public LiveData<List<Game>> getGameList() {
    final MutableLiveData<List<Game>> gameList = new MutableLiveData<>();

    // Create the retrofit builder
    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl(igdbBaseUrl)
            .addConverterFactory(GsonConverterFactory.create());

    // Build retrofit
    Retrofit retrofit = builder.build();

    // Create the retrofit client
    RetrofitClient client = retrofit.create(RetrofitClient.class);
    Call<List<Game>> call = client.getGame(FIELDS,
            ORDER,
            LIMIT);

    call.enqueue(new Callback<List<Game>>() {
        @Override
        public void onResponse(Call<List<Game>> call, Response<List<Game>> response) {
            Timber.d("api call sucesss");
            if (response.body() != null) {
                Timber.d("First game: " + response.body().get(0).getName());

                gameList.setValue(response.body());
            }
        }

        @Override
        public void onFailure(Call<List<Game>> call, Throwable t) {
            Timber.d("api call failed");
        }
    });

    return gameList;
}

}

当我运行代码时,ViewModel 类中的 onResponse 将从 API 调用中输出正确的响应,因此调用工作正常。但是 PopularGamesActivity 类中的 onChanged() 永远不会被调用。有人可以阐明我做错了什么吗?谢谢!

标签: mvvmretrofitviewmodelandroid-architecture-components

解决方案


好的,所以这原来是一个奇怪的 android studio 错误。我最初是在我真正的 nexus 4 设备上运行代码,并且永远不会调用 onChange。但是,在模拟设备上运行它后,它立即开始工作。现在,它也可以在我的真实设备上运行。

我不知道其背后的实际原因,但如果将来有人遇到无法调用 onChange 的问题,请尝试切换设备/模拟器。

干杯。


推荐阅读