mvvm - 为什么观察 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() 永远不会被调用。有人可以阐明我做错了什么吗?谢谢!
解决方案
好的,所以这原来是一个奇怪的 android studio 错误。我最初是在我真正的 nexus 4 设备上运行代码,并且永远不会调用 onChange。但是,在模拟设备上运行它后,它立即开始工作。现在,它也可以在我的真实设备上运行。
我不知道其背后的实际原因,但如果将来有人遇到无法调用 onChange 的问题,请尝试切换设备/模拟器。
干杯。
推荐阅读
- swift - Firebase 动态深层链接:{SCHEME}://google/link/?dismiss=1&is_weak_match=1
- sql - 如何按周转置结果集和分组?
- python - Flask 使用 APScheduler 修改 Session 变量
- spring - Spring Boot 应用程序在 consul 集群中多次注册同一个实例
- .net - Entity Framework 5.0 不包括结果
- c# - 两个的第二个 Await 调用会等待第一个完成吗?
- mongodb - MongoDb:查找具有重复项的精确数组匹配
- api - 在一个单元格谷歌电子表格中组合 2 个 url 过滤器,“ga:pagePath==/url1,url2”
- database-migration - 执行向下迁移时,如何删除字段上的唯一约束?
- asp.net-mvc-5 - 如何获得月份的确切日期?