Open aritrobanerjee93 opened 5 years ago
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>)
um, what? I agree with the initial premise, but that is just not how RealmLists work. Managed RealmLists belong to the object itself.
@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.
Any update on this?
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.
2) 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).
3) 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
7) 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
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.