Search code examples
androidrealmrealm-mobile-platform

Update specific value in Realm


Please how do I update a particular value in a Realm database without refreshing everything, I have used both copyToRealmOrUpdate and insertOrUpdate, the issue is, assuming I have a realm DB class of value ID, name and surname if I actually do this:

   final MyObject obj = new MyObject();
obj.setId(42);
obj.setName("Fish");
realm.executeTransaction(new Realm.Transaction()

{
    @Override
    public void execute (Realm realm){
    // This will create a new object in Realm or throw an exception if the
    // object already exists (same primary key)
    // realm.copyToRealm(obj);

    // This will update an existing object with the same primary key
    // or create a new object if an object with no primary key = 42
    realm.copyToRealmOrUpdate(obj);
}
});

it might update ID 42 to 'name'="fish", but if I query for surname it will not return a valid value, actually the real code I used in my project is:

                final RealmDB_Class realmDB_class = new RealmDB_Class();
            realmDB_class.setPasswordForPrint("2");//no zero so that it can ask one more time
            realmDB_class.setId(1);
            realm.executeTransaction(new Realm.Transaction() {
                                         @Override
                                         public void execute(Realm realm) {
                                             realm.insertOrUpdate(realmDB_class);
                                         }
                                     }


            );

I just want to update the setpasswordForPrint to '2' while preserving the other values I had earlier saved in the realm database such as a variable called 'name' . The Issue is that when I query for name, it points to null, probably because I did not re-insert it here. I do not want to have to insert it.

I have tried using copyToRealmOrUpdate but it also didn't work. Am I to insert all the values again or isn't there a way to just update one value such as SQL UPDATE 'A' SET ...WHERE ... without inserting fresh values. If a 'row' has name, and surname, can't I just update the name using the ID without putting the surname again.


Solution

  • realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute (Realm realm) {
             MyObject obj = realm.where(MyObject.class).equalTo("id", 42).findFirst();
             if(obj == null) {
                 obj = realm.createObject(MyObject.class, 42);
             }
             obj.setName("Fish");
        }
    });
    

    Or

    realm.executeTransaction(new Realm.Transaction() {
         @Override
         public void execute(Realm realm) {
              final RealmDB_Class realmDB_class = realm.where(RealmDB_Class.class).equalTo("id", 1).findFirst();
              realmDB_class.setPasswordForPrint("2");
         }
    });
    

    insertOrUpdate() cannot differentiate between whether a value you're saving is explicitly set to null or you've just never initialized it, so to update specific field, you need to use accessor on managed object inside transaction.