Ken
Ken

Reputation: 11

How to determine if a realm exists and has data in it

In the realm example

ealm-java/examples/gridViewExample/src/main/java/io/realm/examples/realmgridview/GridViewExampleActivity.java

The code always deletes the contents of the existing realm (if present) and then inserts new example data. How would you change that example to determine if the sample data (or any data) was already present in the realm - and to use the realm data instead of recreating a new realm? I am used to sql, realm looks exciting but I am having issues getting my head around some things such as this.

Thanks in advances to anyone who can help me get this sample working so it does not delete the realm data each time, once that is in place I can continue learning, by doing ;)

Upvotes: 1

Views: 3674

Answers (1)

Christian Melchior
Christian Melchior

Reputation: 20126

If you remove the line that says Realm.deleteRealm(config) and then check if the Realm is empty with:

Realm realm = Realm.getInstance(config);
realm.isEmpty(); // true if no objects are in the Realm
...

That should do what you want.

Upvotes: 5

Related Questions