j256 / ormlite-android

ORMLite Android functionality used in conjunction with ormlite-core
http://ormlite.com/
ISC License
1.59k stars 367 forks source link

ReferenceObjectCache.makeSoftCache() makes lazily loaded foreign collections sometimes return null #85

Open dkapaev opened 6 years ago

dkapaev commented 6 years ago

Steps to reproduce:

  1. In DBHelper I created DAOs for all entities like this

    public MyEntityDao getMyEntityDao() {
        try {
            if (mMyEntityDao == null) {
                mMyEntityDao = new MyEntityDao(getConnectionSource(), MyEntity.class);
                mMyEntityDao.setObjectCache(ReferenceObjectCache.makeSoftCache());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return mMyEntityDao;
    }
  2. MyEntity class:

    @DatabaseTable
    public class MyEntity {
    ...
    @ForeignCollectionField(eager = false)
    private Collection<MyEntityValue> myEntityValues;
    
    public Collection<MyEntityValue> getMyEntityValues() {
        return myEntityValues;
    }
    ...
    }
  3. I make a network call, cascade delete all old entities and insert new ones. Without cache enabled, Collection<MyEntityValue> myEntityValues was an empty/non-empty LazyForeignCollection. With cache enabled, it is sometimes NULL and causing NPEs in parts of code that previously worked.