Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update statement in Realm android

Tags:

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

like image 478
Dinu Avatar asked Dec 01 '14 07:12

Dinu


People also ask

How to delete in realm android?

clear() method that . deleteAllFromRealm() is now the correct method to use.

What is realm Android studio?

What is Realm? Realm is an object database that is simple to embed in your mobile app. Realm is a developer-friendly alternative to mobile databases such as SQLite and CoreData. Before we start, create an Android application.

How do I delete all data from a realm?

The right way of deleting your entire Realm (schema) is to use : Realm realm = Realm. getDefaultInstance(); realm. beginTransaction(); // delete all realm objects realm.


Video Answer


2 Answers

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()

like image 147
j_gonfer Avatar answered Sep 23 '22 08:09

j_gonfer


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();                 }             }); 
like image 24
John Avatar answered Sep 25 '22 08:09

John