Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document Realm Best Practices #6354

Open
aritrobanerjee93 opened this issue Nov 27, 2018 · 5 comments
Open

Document Realm Best Practices #6354

aritrobanerjee93 opened this issue Nov 27, 2018 · 5 comments

Comments

@aritrobanerjee93
Copy link

aritrobanerjee93 commented Nov 27, 2018

Hi,
I have scoured the internet for realm best practices but they are scattered and inconsistent.

It would be great if the realm team came up with a repo of gists or samples to illustrate realm best practices for java.

Some areas would be-
1)Realm Initialisation
2)Realm Compaction
3)Realm closing and app exit best practices
4)Realm bulk multi insert best practices
5)Realm fetch best practices
6)Realm backup db best practices
7)How to prevent Realm DB from getting corrupted.
8)How to model Realm DB schema's (When to create separate realms etc.)
9)Realm DB optimisation.

@MkazemAkhgary
Copy link

MkazemAkhgary commented Nov 29, 2018

Also best practice for deep cloning a RealmList so that modifications on original RealmList wont affect cloned RealmList.
Edit: I guess the answer to that is realm.copyFromRealm(Iterable<Item>)

@Zhuinden
Copy link
Contributor

Zhuinden commented Nov 29, 2018

um, what? I agree with the initial premise, but that is just not how RealmLists work. Managed RealmLists belong to the object itself.

@cmelchior
Copy link
Contributor

@MkazemAkhgary

Also best practice for deep cloning a RealmList so that modifications on original RealmList wont affect cloned RealmList.

You cannot clone objects with primary keys. By definition there is only one of them. For all other types, simply copying it should work fine.

@cmelchior cmelchior changed the title Realm Best Practices Document Realm Best Practices Nov 29, 2018
@aritrobanerjee93
Copy link
Author

Any update on this?

@Zhuinden
Copy link
Contributor

Zhuinden commented Jun 18, 2019

I never seem to have read the initial question for some reason, but I know some things off the top of my head..

  1. Realm Initialisation

You can set the default realm configuration in Application.onCreate.

If you are writing a library project, then DO NOT SET the default configuration.

Then you generally either want a single instance for the UI thread based on Activity ref counting, OR you can open a Realm instance in each Activity/Fragment directly (this is documented).

Background threads require their own instances, and those instances should (must?) be closed when you are no longer using them. close() must be called as many times as you called getInstance on that given thread.

Hopefully, one day getInstance() will be called open to reflect this.

  1. Realm Compaction

Compaction opens the Realm, writes a compacted copy of it, then replaces the original file; so it's best to ensure that this happens only in one process. I used to compact when the application was closing (Activity ref count reached 0 so UI thread Realm was closed).

Compaction is available only if the global instance count is 0 (there is no open Realm on any threads in any processes).

  1. Realm closing and app exit best practices

This is already documented.

4)Realm bulk multi insert best practices

See https://stackoverflow.com/questions/29214236/how-to-add-1-milion-items-in-realm-correctly/38891222#38891222 or https://stackoverflow.com/a/39385985/2413303

5)Realm fetch best practices

Define a RealmResults as a field, initialize it from the Realm instance for this given thread with findAllAsync (on UI thread). On UI thread, add a RealmChangeListener.

You will retrieve the data from Realm when query is complete, and all future changes so that you don't need to wonder how to keep your data in sync with the db.

On background thread, open an instance, close it when it is no longer needed, and inbetween use the synchronous Realm query api.

6)Realm backup db best practices

dunno

  1. How to prevent Realm DB from getting corrupted.

uh, don't use encryption? 🤔 i dunno, I'm not a realm member

8)How to model Realm DB schema's (When to create separate realms etc.)

I wouldn't create separate Realms at all, especially now with the partial Realm api for sync (query-based Realms).

Generally you want to minimize the number of links between objects and bundle them all together, because link queries are restrictive, bi-directional links can slow down change notifications, and cascade deletion is not available in the bindings.

9)Realm DB optimisation.

Not sure what this means

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants