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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...