如何解决Android合并了多个LiveData对象中的LiveData
我在会议室中有5个表/对象的结构,通过外键链接,例如:
食谱(tableName =“ recipes”,具有自动生成的PrimaryKey local_id) 作者(外键:配方表中的local_id) 成分(外键:食谱表中的local_id)
食谱POJO包含作者和成分POJO的属性。
当某人单击“收藏夹”按钮时,配方,作者和配料对象将添加到Room DB中,并相应地填充外键。
然后,在菜单中选择要从数据库中检索配方列表的视图后,便可以查看该视图。 这里的问题是,在检索食谱时,我将需要重建Recipe对象并将其添加到Author和Ingredients对象。 所有这些都作为LiveData存储在Room中。
我环顾四周,但无法解决这个问题。
当前代码:
MainViewModel
public class MainViewModel extends AndroidViewModel {
private LiveData<List<Recipe>> recipes;
public MainViewModel(@NonNull Application application) {
super(application);
AppDatabase database = AppDatabase.getInstance(this.getApplication());
recipes = database.recipeDao().loadAllRecipes();
}
// getter to retrieve fav recipes
public LiveData<List<Recipe>> getFavRecipes() {
return recipes;
} }
食谱类别
public class Recipe implements Serializable {
private int id;
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "local_id")
private int localId;
private String name;
@Ignore
private List<Ingredient> ingredients;
@Ignore
private List<Step> steps;
private int servings;
private String image;
@Ignore
private List<Author> authors;
private String type;
@ColumnInfo(name = "fav_flag")
private boolean isFavourite;
private String notes;
RecipeDao
@Query("SELECT * FROM recipes ORDER BY local_id")
LiveData<List<Recipe>> loadAllRecipes();
我的活动
case R.id.action_show_fav_list:
// Loads list of fav recipes from ROOM DB
setupFavRecipesVM();
return true;
// SOME OTHER CODE IN HERE
private void setupFavRecipesVM() {
final MainViewModel viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
viewModel.getFavRecipes().observe(this,new Observer<List<Recipe>>() {
@Override
public void onChanged(List<Recipe> recipes) {
RecipesScrollingActivity.this.adapterSetUp(RecipesScrollingActivity.this,new ArrayList<Recipe>(recipes));
}
});
解决此问题的最佳方法是什么?
我已经尝试了很多不同的方法,例如设置MediatorLiveData,但是我无法使其正常工作。这只是我尝试过的一个例子,但我什至不确定自己是否正确。
MainViewModel中MediatorLiveData设置示例
private MediatorLiveData<List<Recipe>> init(RecipeDao recipeDao,IngredientDao ingredientDao) {
Log.i("MediatorLiveData","FIRED");
recipesLD = recipeDao.loadAllRecipes();
ingredientsLD = ingredientDao.loadAllIngredients();
recipesMLD.addSource(ingredientsLD,new Observer<List<Ingredient>>() {
@Override
public void onChanged(List<Ingredient> ingredients) {
List<Recipe> recipesCurrent = recipesMLD.getValue();
for (Ingredient ingredient : ingredients) {
int mRecipeId = ingredient.getRecipeId();
for (Recipe recipe : recipesCurrent) {
List<Ingredient>mIngredientList = recipe.getIngredients();
if (recipe.getLocalId() == mRecipeId) {
mIngredientList.add(ingredient);
recipe.setIngredients(mIngredientList);
}
}
}
recipesMLD.setValue(recipesCurrent);
}
});
recipesMLD.addSource(recipesLD,new Observer<List<Recipe>>() {
@Override
public void onChanged(List<Recipe> recipes) {
List<Recipe> recipesCurrent = recipesMLD.getValue();
for (Recipe recipe : recipes) {
int recipeId = recipe.getLocalId();
for (Recipe recipeCurrent : recipesCurrent) {
if (recipeCurrent.getLocalId() == recipeId) {
recipeCurrent.setName(recipe.getName() + "_MediatorLiveData");
}
}
}
recipesMLD.setValue(recipesCurrent);
}
});
return recipesMLD;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。