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

Is having one Realm object per thread OK? #3172

Closed
IsmailHassanein opened this issue Feb 3, 2016 · 5 comments
Closed

Is having one Realm object per thread OK? #3172

IsmailHassanein opened this issue Feb 3, 2016 · 5 comments
Assignees
Labels

Comments

@IsmailHassanein
Copy link

I'm adding this extension toRealm to avoid creating a Realm objects, but I'm not sure if this way is the good to use. Would you please tell me If it's ok or not?

import RealmSwift

extension Realm {
    class var sharedRealm: Realm {
            var realm = NSThread.currentThread().threadDictionary["sharedRealm"] as? Realm
            if  realm == nil {
                realm = try! Realm()
                NSThread.currentThread().threadDictionary["sharedRealm"] = realm
            }
            return realm!
    }
}

and the way I call it is always.

Realm.sharedRealm

every where in my code. there is no other try! Realm() in my project but the one here.

@tgoyne tgoyne self-assigned this Feb 3, 2016
@tgoyne
Copy link
Member

tgoyne commented Feb 3, 2016

No, that is not a good idea.

The simplest reason is because Realm already caches Realm instances per thread, so adding your own caching on top of that isn't doing anything functionally useful, and isn't even making your code using Realm any more concise.

The bigger problem is that this'll behave very poorly when used on background threads with GCD. Autorefresh only works on threads which are running inside a runloop (normally just the main thread), so the Realm instances on background threads will not be automatically updated to pull in changes made on other threads. Normally this is not a problem, because the instances on background threads are only live for the duration of a single dispatch block, and then the next block dispatched gets a fresh instance with the newest data.

Even if you manually call refresh() on a regular basis to ensure you're working with the latest data, the instances on other threads which have not yet been refreshed will force Realm to keep the data around for the versions they're using, resulting in the Realm file taking up much more space on disk.

@IsmailHassanein
Copy link
Author

That was a great information, which explains why some times I don't get updates happened in in background thread in the main one. Thanks.

@noamtamim
Copy link

@tgoyne or someone else from Realm -- is the above still true in Realm v3.7.2? Is it still a bad idea to hold a Realm in threadDictionary (in the sense that refreshes won't work, etc)?
How about holding a single Realm and only ever accessing it using a single DispatchQueue?

@tgoyne
Copy link
Member

tgoyne commented Jun 17, 2018

Nothing significant has changed with our threading model.

@noamtamim
Copy link

@tgoyne thanks. I'm only considering this because my customers report something that looks like #5138. In my use of Realm, I create a lot of Realm references. As much as about 100 per second.
I considered filing a bug report (or reviving the old one), but instead I'm trying to look for ways to reduce Realm ref creation.

Should I open a bug? I asked SO about best practice, but so far only got downvotes.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants