WAI
Status Update
Comments
yb...@google.com <yb...@google.com>
yb...@google.com <yb...@google.com> #2
Could you CC the reviewers to this bug so that they can access it - just so we can get the relevant people involved.
Also is it possible to attach the patch to this bug report so we can see what is involved on the clang side?
Also is it possible to attach the patch to this bug report so we can see what is involved on the clang side?
Description
Version used: 1.0.0-alpha3
Devices/Android versions reproduced on: Nexus 5X (ODP3)
TL;DR: It seems like in different circumstances, a @Relation field with no results sometimes is set to null and sometimes is set to an empty List<>, and ideally this would be made consistent.
(warning: lengthy issue report)
In my continuing Room experiments, I have a self-referential relation for a tree of Category entities:
@Entity(
tableName="categories",
foreignKeys=@ForeignKey(
entity=Category.class,
parentColumns="id",
childColumns="parentId",
onDelete=CASCADE),
indices=@Index(value="parentId"))
public class Category {
@PrimaryKey
public final String id;
public final String title;
public final String parentId;
@Ignore
public Category(String title) {
this(title, null);
}
@Ignore
public Category(String title, String parentId) {
this(UUID.randomUUID().toString(), title, parentId);
}
public Category(String id, String title, String parentId) {
this.title=title;
this.parentId=parentId;
}
}
This works well.
In playing around with @Relation, I created a CategoryTuple to mirror Category and offer a pair of @Relation fields, one for the children and one for the parents:
public class CategoryTuple {
public final String id;
public final String title;
public final String parentId;
public CategoryTuple(String id, String title, String parentId) {
this.title=title;
this.parentId=parentId;
}
@Relation(parentColumn="id", entityColumn="parentId")
public List<Category> children;
@Relation(parentColumn="parentId", entityColumn="id")
public List<Category> parents;
}
My DAO has methods to retrieve the root CategoryTuple (identified as the one with a NULL parentId) and the children for a particular parent:
@Query("SELECT * FROM categories WHERE parentId IS NULL")
CategoryTuple findRootCategoryTuple();
@Query("SELECT * FROM categories WHERE parentId=:parentId")
List<CategoryTuple> findChildCategoryTuples(String parentId);
In instrumentation tests, I set up a Category with a Category as a child. Then, I do this:
final CategoryTuple rootTuple=store.findRootCategoryTuple();
assertNotNull(rootTuple);
assertEquals(1, rootTuple.children.size());
assertNull(rootTuple.parents);
final List<CategoryTuple> tuples=store.findChildCategoryTuples(rootTuple.id);
assertEquals(1, tuples.size());
assertEquals(0, tuples.get(0).children.size());
assertEquals(1, tuples.get(0).parents.size());
That's what I had to write, in terms of assertions, to get a clean test run.
rootTuple has no parents. The parents @Relation field on rootTuple is null.
The child tuple has no children. The children @Relation field on that child is an empty list.
IMHO, we should get consistent results, either null in both cases or an empty list in both cases. If this behavior is intentional, we could use some documentation for when to expect what value.
Attached is a project that has this code, amongst a lot of other crap you can ignore (or is that @Ignore?). The test code is in DaoTests, in the categories() test method.
Thanks!