-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Comments
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 |
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. |
@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 |
Nothing significant has changed with our threading model. |
@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. Should I open a bug? I asked SO about best practice, but so far only got downvotes. |
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?and the way I call it is always.
every where in my code. there is no other
try! Realm()
in my project but the one here.The text was updated successfully, but these errors were encountered: