How do I build a GreenDao query that loads all Items specified through a list of ids?

StackOverflow https://stackoverflow.com/questions/14195383

  •  13-01-2022
  •  | 
  •  

質問

I have a List of Strings each String is a unique identifier for a item persisted through GreenDao.

How do I build a query that allows me to load all this items form my database?

Is there a possibility to do it with a QueryBuilder or do I need to go back to writing SQL?

役に立ちましたか?

解決

This is possible with the in condition in the Property class.

This example loads all boxes with field values contained in fieldValues. fieldValues is of the type List<String>

  List<LocalBox> boxes = getBoxDao(context).queryBuilder()
        .where(LocalBoxDao.Properties.field.in(fieldValues)).list();

他のヒント

I would like to put some light in "in query",
There is a limitation in this, we cant pass more than 100 ids inside a in query
So what I did to achieve this is:

List<Product> productList = new ArrayList<Product>();
DaoSession daoSessionUni = TarneaAndroidApplicationContext.getInstance().getDaoSession();

for (int i = 0; i < rowIds.size(); i = i + 100) 
{
    ProductDao productDao = daoSessionUni.getProductDao();
    QueryBuilder<Product> queryBuilder = productDao.queryBuilder().where(
        ProductDao.Properties.Id.in(rowIds.subList(i + 100 < rowIds.size() ? 
                                                   i + 100 : 
                                                   rowIds.size())),
        ProductDao.Properties.IsDeleted.eq(0));             
    productList.addAll(queryBuilder.list());
}

if we want to pass a list of id using in Query we can use this method.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top