Reputation: 7351
The Swift Realm docs mention that you only need to initialize Realm with let realm = try! Realm()
once per thread. I'm wondering if there are any performance (or other) issues that come with calling it once in each of the handful of classes in my project that use Realm. Is it worth making some sort of singleton class that keeps a reference to the realm object?
Upvotes: 1
Views: 278
Reputation: 7806
No, there are no drawbacks, as there are already safeguards in place to prevent that from happening at all. Realm instances are internally (weak!) cached per thread dependent on their path, so that you don't need to come up with an own mechanism.
It helps though to ensure that you keep at least one Realm instance alive on all threads where you need regular access. Overall if there is still one instance alive on any threads, it is significantly cheaper to instantiate a Realm on another thread as some data can be shared. If you're using GCD, keep in mind that there is only a guarantee which thread will execute the task for the main queue with the main thread.
Upvotes: 2