Question

I'm using GreenDao and creating a core function that help you to update some values of an entity, and if the entity is not in the database then it also inserting it. The problem is that I'm always getting the cached copy of the entity, I know that GreenDao manage some simple cache and I would like to have the ability bypass it. does anyone knows how I can query right from the database?

This doesn't work

.Dao().queryBuilder().where(comDao.Properties.Id.eq(id)).build().listLazyUncached();
Was it helpful?

Solution

GreenDao indeed has Inner caching mechanism in its daoCore.jar sources.

You can disable the caching easily by searching for the code that put() and get() entities from the cache.

 which is a: HashMap<? extends AbstractDao>.

Then generate MyDaoCore.jar and add it to your project.

Secondly, in order to update or insert and entity (without replacing it entirely) you need to implement the following pseudo code. I'm sorry that I'm not adding the actual code, I solved it long time ago.

public void insertOrUpdate(List<? extends AbstractDao> entities){
    List<Entity> toInsert;
    List<Entity> toUpdate;

    for (Entity e : entities) 
    {
       if( e.inDatabase() )
            toUpdate.add(e);
       else 
            toInsert.add(e);
    }
    Dao.updateAll(toUpdate);
    Dao.insertAll(toInsert);
}

Edit 1: You can use IN statement in order to get all the ids of an entity in only one query like this:

.where(Dao.Properties.Id.in(ids)).build().list();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top