Realm, Memory caching and multiple threads

378 views
Skip to first unread message

Hosam Hassan

unread,
Nov 16, 2014, 6:07:17 PM11/16/14
to realm...@googlegroups.com
I am having trouble understand how realm works with memory and threads.

1- I do need memory caching to speed up the queries. If every time I make a query it is done on disk. This is very painful and slow. Does realm have memory caching built in?

2- If realm have memory caching. Is it only per realm object? If so then how I can do that using threads? I spring a new background operation thread that READ ONLY from the file. Currently I can't
share the realm object without it blowing in my face and crashing. When i restart the thread it just blows up.

Any idea how I can approach that problem?

Brian Munkholm

unread,
Nov 18, 2014, 6:01:46 AM11/18/14
to Hosam Hassan, realm...@googlegroups.com
Hi Hosam,
Yeah, maybe it's soon about time we get to that blog post about how Realm works internally.
But until then a few comments.
Realm memory-maps the database file. That is very efficient, and the OS will read the data from disk as needed, and keep all the hot data in memory. So except for the first time you query, you shouldn't be going to disk for queries. Effectively from your perspective it acts as a built in memory cache.

One important consideration though is that opening the file the first time is an expensive operation. We do however cache the Realm instance on each thread, so you should only see a penalty the first time.

We are looking into ways to do async queries or handover objects at the moment, so that would hopefully solve your pain.

To ensure your use case is solved by the current solution we are looking into, it would be great with a little more details on your needs.

Cheers
Brian





--
You received this message because you are subscribed to the Google Groups "Realm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-cocoa...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-cocoa/4c6ae8f7-8e12-46a5-a5e4-c1604dc2ba14%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hosam Hassan

unread,
Nov 18, 2014, 8:52:18 AM11/18/14
to realm...@googlegroups.com, hossamh...@gmail.com
Again I iterate about the same exact major flow with Realm. I do operations in a background thread on a READ ONLY realm object and I don’t want to keep reinitialising realm from the zero every time.

Currently I can’t share the same realm object between two threads. That makes absolutely no sense to me. It is like telling me I can’t use the same sqlite file from two threads.

Because of that I get the penalty for initialisation on every single query I do.

Tim Anglade

unread,
Nov 18, 2014, 10:35:29 AM11/18/14
to realm...@googlegroups.com, hossamh...@gmail.com
As Brian said we are working on this. And functionally, it is very difficult to use SQLite from multiple threads at the same time in a safe manner (see: Core Data). We like to think we’re already making your life a bit better with Realm right now where accessing the same object from 2 different threads means just copying the primary ID of the object, but we hear your pain! We are working on this as quickly as we can.
Reply all
Reply to author
Forward
0 new messages