phpdroid
phpdroid

Reputation: 1663

Boolean fields are set to false on copyToRealmOrUpdate realm

I don't know why I see strange behavior when I am trying to update a row in Realm without updating boolean field are set as false. Here am not updating my boolean fields i.e noti and adult:-

CASE 1:

 Realm.init(context);
    Realm realm = Realm.getDefaultInstance();
    UserDetails m2= new UserDetails();
    m2.setUserid(1);
    m2.setSort(paths_Vvalue[position]);
    realm.beginTransaction();
    realm.copyToRealmOrUpdate(m2);
    realm.commitTransaction();

After execution of this code they(noti and adult) are saved as false even before they were as true

is this a Bug? everything Works fine if i add and update these fields like:-

CASE 2:

 Realm.init(context);
    Realm realm = Realm.getDefaultInstance();
    UserDetails res = realm.where(UserDetails.class).equalTo("userid", 1).findFirst();
    UserDetails m2= new UserDetails();
    m2.setUserid(1);
    m2.setSort(paths_Vvalue[position]);
    m2.setAdult(res.getAdult());  //setting boolean
    m2.setNoti(res.getNoti());  //setting boolean
    realm.beginTransaction();
    realm.copyToRealmOrUpdate(m2);
    realm.commitTransaction();

Everything works fine in this case but still,
Am i doing something wrong at case 1 or is this a bug or a functionality of realm?
any hint will be great thanks!

Upvotes: 0

Views: 548

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81549

Considering you are overwriting ("updating") your UserDetails with the same primary key and an object with following values

UserDetails m2= new UserDetails();
m2.setUserid(1);
m2.setSort(paths_Vvalue[position]);
//m2 adult is default `false`
//m2 noti is default `false`
...
realm.insertOrUpdate(m2);

You are updating the existing object with an (unmanaged) item where those field values are false, so of course it will be false.

Upvotes: 0

Related Questions