0

I use Realm for Swift with memory only configuration. Because of updating the database in background thread I create a Realm instance every time I use it. This was mentioned in the Realm documentation and another question here https://stackoverflow.com/a/45375608/613121.

But by testing the database I notice an unpredictable behaviour. Sometimes the database was empty and the update refills the database instead of an update. The reason seams to be that the Realm was deallocated after the writing.

The solution may be to hold a strong reference in the main thread. I save the Realm configuration in a singleton. Would it be safe that the singleton returns a new Realm with same configuration for updates and hold a strong reference to another Realm for preventing losses?

corban
  • 618
  • 6
  • 16
  • In https://stackoverflow.com/questions/41002022/inmemory-realm-threading-in-swift this issue is discussed with the hint of a singleton on main thread. My workaround is holding a strong reference in config singleton on some thread. All work is done with a new Realm. So I don't mix up with threading at all - I hope. Maybe I should create my singleton with dispatch on main thread. And only create new Realm if caller comes from another thread!? – corban Mar 18 '19 at 15:20
  • While I understand the question, the use case is not clear. A singleton pattern may be a solution in some cases but as David mentioned in that answer *The best way to use Realm in a thread-safe way is to create a new reference to your Realm using let realm = try! Realm() every time you move between threads*. If you are encountering unexpected results, the problem may lie elsewhere. Adding some code that demonstrates the issue may help. Please take a moment and review [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Jay Mar 18 '19 at 20:58
  • Thanks Jay. I encountered the problem by using new Realm instances every time I use it. This seems to work fine. But in testing the database it was empty after writing to it from a background thread and stepping through the debugger. Therefore there may has to be a strong reference to a Realm with the same configuration. Now it seems to work. – corban Mar 19 '19 at 08:02

1 Answers1

1

It sounds like it should work fine. You'll have to try it, and that's probably quicker than waiting for a definitive answer on here.

I'd write a singleton class, that as part of the constructor defines the Realm configuration and stores it, and also opens and stores an instance of that realm. Then I'd add an accessor to that class for your threads to use; it would return a new instance of the realm using the same configuration. When those go out of scope (and you should allow them to go out of scope) your realm will persist due to the singleton instance maintaining its reference.

I may be reading your first referenced question wrongly, but that isn't dealing with in-memory realms and therefore doesn't demand that you hold onto a reference somewhere.

Chris Shaw
  • 1,610
  • 2
  • 10
  • 14
  • Yes I tried and it seems to work. Hopefully writing to a new Realm in a background thread which is destroyed afterwards will not cause any problems. – corban Mar 19 '19 at 08:08