Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm Java Android: Create or update object

In Realm Javascript, it's possible to create an object by primary key and, if it already exists, update the existing object: https://realm.io/docs/javascript/latest/#creating-and-updating-objects-with-primary-keys

How can I achieve the same in Realm Java? Didn't find this function in the docs.

like image 381
Damnum Avatar asked Sep 10 '17 05:09

Damnum


People also ask

How do you create a realm object?

To define a Realm object in your application, create a subclass of RealmObject or implement RealmModel. All Realm objects must provide an empty constructor. All Realm objects must use the public visibility modifier in Java or the open visibility modifier in Kotlin.

How to delete in Realm Android?

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

What is realm database Android?

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.


1 Answers

How can I achieve the same in Realm Java? Didn't find this function in the docs.

There are two methods for this,

1.) copyToRealmOrUpdate() which returns the managed proxy

2.) insertOrUpdate() which does not return the managed proxy (so it's faster when you insert many items)

like image 197
EpicPandaForce Avatar answered Oct 24 '22 01:10

EpicPandaForce