ActiveAndroid icon indicating copy to clipboard operation
ActiveAndroid copied to clipboard

Model loading changed other instances

Open aemarkov opened this issue 8 years ago • 2 comments

I have a model:

@Table(...)
class MyModel extends Model
{
    @Columnt(...)
    public String Field;

    public void SomeMethod()
    {
        MyModel old = MyModel.get(this.id);
        //Compare old.Field and this.Field
    }
}

with a method, where I load the DB stored intanse of this record (for comparation). If current instance has modified fields and wasn't save, after loading old model fields of current instance are changed to same values as loaded from DB instance.

If field in DB is null, but in current instance field is not null, after loading the old has field value like in current instance (not null).

aemarkov avatar Jun 11 '16 23:06 aemarkov

I ran into this issue a while ago, and found out it was because of the cache. Try to clear it before loading your model again:

Cache.removeEntity(yourModel);

PtiPingouin avatar Sep 22 '16 09:09 PtiPingouin

I had the same issue a couple of days ago. I found a bug in my project and could not understand why date field in the db is getting a value while it should still be null. After some research I found out that at some point I am loading a list of models and perform a logic calculations, and in case the date field is null i assign some default value for calculations only but not saving the model. At some completely different code section I load the model again and update some other filed and save it. I was surprised to see that after saving this model, its date field got the previous assigned date instead of null. I don't think that it is intuitive to call Cache.removeEntity every time I load a model. I would expect that default behaviour will not cache values that are not intended to be there.

mikelibg avatar Oct 15 '16 22:10 mikelibg