android - 如何在房间数据库中使用递归 CTE 构建闭包表
问题描述
下面的类是从 API 调用返回的结构,减去 primaryKey、parentId 和 depth。我的目标是使用递归 CTE 在 Room DB 中构建一个闭包表。
class Category {
public Integer primaryKey;
public String parentId;
public Integer depth;
public String categoryId;
public String name;
public String pluralName;
public String shortName;
@Ignore public List<Category> categories;
}
当返回的 POJO 从可观察对象发出时,我尝试遍历树。问题在于 categoryId==parentId 的某些传递闭包的深度不正确。
for(Category category: categories){
List<Category> tree = new ArrayList<>();
buildCategoryClosureTable(category, tree, 0);
}
void buildCategoryClosureTable(Category target, List<Category> tree, Integer depth){
tree.add(target);
if(!target.categories.isEmpty()){
depth++;
do{
Category category = target.categories.remove(0);
buildFoursquareCategoryClosureTable(category, tree, depth);
}while(!target.categories.isEmpty());
}
for(Category category : tree){
FoursquareCategory foursquareCategory = new FoursquareCategory(target, category.categoryId, depth);
insertFoursquareCategoryIntoBaseTable(foursquareCategory);
depth--;
}
tree.remove(target);
}
那么...如何使用递归 CTE 在 Room DB 中构建闭包表?
解决方案
推荐阅读
- java - 在 gradle 脚本中通过 AntBuilder 提供主类属性
- python - 如何在熊猫的行中插入某个值
- python - Django如何在自定义输入文件上显示保存的媒体文件名?
- python - 将参数传递给正在运行的 python 脚本
- sql - 如何在不删除表的情况下同步表模式?
- python - 如何用kivy中的图像替换标签中的文本?
- pine-script - pine 脚本是否可以根据触发器下订单,然后退出该订单确切数量的柱?
- r - UseMethod("filter_") 中的错误:使用 R 访问和处理 Quikscat 风速和风向时
- javascript - 当您单击 jquery/javascript 中的另一个元素时,防止执行一个代码
- azure - 无法在服务器防火墙规则中添加我的客户端 IP