【发布时间】:2016-08-24 08:32:50
【问题描述】:
在 Entity 上定义 ToMany 关系时,生成的代码如下所示(ProductEntity 与 MediaEntity 具有 ToMany 关系):
/**
* To-many relationship, resolved on first access (and after reset).
* Changes to to-many relations are not persisted, make changes to the target entity.
*/
@Generated(hash = 580223476)
public List<MediaEntity> getMedia() {
if (media == null) {
final DaoSession daoSession = this.daoSession;
if (daoSession == null) {
throw new DaoException("Entity is detached from DAO context");
}
MediaEntityDao targetDao = daoSession.getMediaEntityDao();
List<MediaEntity> mediaNew = targetDao._queryProductEntity_Media(productId);
synchronized (this) {
if(media == null) {
media = mediaNew;
}
}
}
return media;
}
现在,即使我们在后台线程获取ProductEntity的实例(例如使用自定义Loader),它的getMedia()方法也会在UI线程上被调用,这会导致第一次调用getMedia()引起的SQLite查询在 UI 线程上执行。
有没有办法防止这种延迟加载子对象并指示 GreenDao 解决所有依赖关系并在父实体创建/初始化时填充所有字段?
GreenDAO 的 github 页面上对应的支持票链接:https://github.com/greenrobot/greenDAO/issues/416
附:我们可以在从 DaoSession 获取 ProductEntity 之后手动添加对 getMedia() 的调用,但这不是一个有效的解决方案:太容易出错。
【问题讨论】:
标签: android multithreading android-sqlite greendao