Proper usage patterns / best practices for real-time use?

In the process of transforming the project, we use Realm. We are really impressed so far with the help of Realm Browser (so convenient!).

As a result, several questions appeared, and we would like to get some specific usage patterns before moving on. Our application is highly multithreaded (API calls, animations, etc.), so keep this in mind when reading questions, as I know that Realm instances cannot be accessed via streams (currently).

  • How were we worried if we would repeatedly create instances of Realm? What is overhead?
  • Should we worry about saving Realm instances in ViewControllers or Singletons for reuse? We tried this, but sometimes instances get access from different threads, so we had to go back to creating a new instance every time.
  • When accessing relationship properties in Realm instances, the resulting data that is considered to be stored in memory, or read from disk every time? Do we need to worry about saved instances of Realm getting too big due to deep access to relationships?
  • When do I need to update an instance of Realm? I noticed that when I make changes to the Realm browser, they are reflected in the saved Kingdom without calling an update.
    • It seems that in each area there is an Auto-Refresh property that calls this according to the documentation.
  • Accessing the realm property with improper Object practice? We used this to write to Realm if the function using the object did not create the object or Realm (in the same thread, of course).

For example...

 func saveStuff(thingToUpdate: Object?) { if let thingToUpdate = thingToUpdate, let realm = thingToUpdate.realm { realm.write { thingToUpdate.name = "lionpants" } } } 

Thanks in advance. I look forward to your answers .: D

+52
ios swift realm
Jul 23 '15 at 14:44
source share
1 answer

(Disclaimer: I work for Realm. :)) Thank you very much! Nice to hear you are enjoying the kingdom!

A few instances of the kingdom - you do not need to worry about it! The Realm object of the file is created when the first instance is created in each thread, and the same object is subsequently returned every time you try to execute it every time after that.

Vault Instances - Following from the first moment, no, you don’t have to worry about linking to Realm inside other objects. Since Realm keeps track of its Realm file objects within itself and returns the same ones, you will not be punished for it. However, if the permalink to a Realm object within your object simplifies your code complexity, feel free to continue using it.

Access to Realm property properties - Strictly speaking, data from Realm files are not copied from disk (like regular ORM); more he uses memory mapping to directly reference data from disk directly to your properties in memory. So no, you don’t have to worry about Realm files getting too big in memory.

Auto update Auto-update is enabled by default for the Realm file object in the main thread. It must be manually enabled for Realm file objects in other threads, or you can instead manually update them by calling the refresh method.

EDIT: I stand fixed! All Realm file objects on multiple threads have autorefresh by default. If autorefresh enabled, the only time you need to call refresh is to make changes to the Realm file reflected in other links until the current iteration of the run loop completes.

Link to the object Link to the area Absolutely not, this is a good practice in general! I actually prefer to do this in my personal applications that use Realm to provide the proper context, as it is always simpler and provides a stronger visual context between the object and its parent Realm file in the code. (Haha, if there was a problem with threads here, you would probably find it before reaching the write point).




I hope this helps! Let me know if you need to clarify anything here!

+61
Jul 23 '15 at 17:43
source share
β€” -



All Articles