21

How should i update a already existing value using realm DB in android?

I have been trying to update it but it is adding as a new value only not overwritting it

Dinu
  • 600
  • 3
  • 6
  • 27
  • Please show us your efforts(the relevant part of your code, where you are trying to update). Only then we can tell you where's the problem. – DroidDev Dec 01 '14 at 07:37
  • **see full answer with example http://stackoverflow.com/questions/30933229/update-realm-object-android** – N J Aug 04 '15 at 14:22

3 Answers3

40

Another way to update an existing object with all its fields in your Realm DB is using the method realm.copyToRealmOrUpdate():

Object obj = new Object();
obj.setField1(field1);
obj.setField2(field2);
realm.beginTransaction();
realm.copyToRealmOrUpdate(obj);
realm.commitTransaction();

If your object has a Primary Key, this method will update the object automatically without duplicate objects :)

More info: copyToRealmOrUpdate()

SolArabehety
  • 8,467
  • 5
  • 38
  • 42
j_gonfer
  • 1,547
  • 21
  • 21
  • 1
    You actually don't get any benefit from beginning the transaction before you have your object created. So I would suggest to move that first line down before the copyToRealmOrUpdate() to make it clear. – bmunk May 28 '15 at 21:21
  • This answer is outdated? `realm.copyToRealmOrUpdate(obj);` is not needed if you are working with realm. It would be needed only if you are using Retrofit for example: https://realm.io/docs/java/latest/#retrofit – Micro Nov 03 '16 at 20:56
  • 2
    @MicroR So, what is the alternative? How to update an existing value in Realm? (I'm getting `Realm Database Error: Value already exists:` if I use `createObject()`). – Srikar Reddy Nov 13 '16 at 20:17
  • What if i don't have a primary key and I still want to update? – Parag Kadam May 20 '17 at 10:40
  • 2
    From documentation: "Using primary keys makes it possible to use the copyToRealmOrUpdate() method, which will look for an existing object with this primary key, and update it if one is found" – Vinicius Lima Jul 09 '17 at 19:09
  • @SrikarReddy u because not need a createObject for update.. make a simple constructor with all variables a use createOrUpdate and do not forget realm.close – marlonpya Jul 31 '17 at 02:15
  • is it necessary to use try catch block? – Zahidul Feb 13 '18 at 15:35
9

You can use insertOrUpdate method to do this.Hope this helps

  Realm.getDefaultInstance().executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {

                    objectToEdit.setNewValue("string");
                    realm.insertOrUpdate();
                }
            });
Saurabh
  • 975
  • 2
  • 12
  • 27
0
Realm realm = Realm.getDefaultInstance();
    OrderedRealmCollection<Practice> orderedRealmCollection = 
             realm.where(Practice.class)
            .contains("key", "matchig value")
            .findAll();

    if(orderedRealmCollection.size()!=0){
        Practice practice = orderedRealmCollection.first();
        Realm.getDefaultInstance().executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                practice.setUpdate_practice_steps(""+steps);
                realm.insertOrUpdate(practice);
            }
        });
    }
patel jigar
  • 52
  • 1
  • 7