适当的领域使用模式/最佳实践? [英] Proper Realm usage patterns/best practices?

查看:108
本文介绍了适当的领域使用模式/最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在将项目转换为使用Realm。到目前为止,我们对Realm浏览器印象非常深刻(非常方便!)。

We're in the process of converting a project to use Realm. We're really impressed so far especially with the Realm Browser (so handy!).

结果,出现了一些问题,我们希望得到一些具体的使用模式,然后再进一步。我们的应用程序是多线程(API调用,动画等),因此在阅读问题时请记住这一点,因为我知道不能跨线程(当前)访问Realm实例。

As a result, a few questions have come up and we'd like to get some concrete usage patterns down before going any further. Our app is heavily multi threaded (API calls, animations, etc), so keep that in mind when reading the questions, since I know Realm instances cannot be accessed across threads (currently).


  • 我们多么担心重复创建Realm的实例?什么是开销?

  • 我们是否应该在ViewControllers中保留Realm实例或者重复使用Singletons
    ?我们已经尝试过这个但有时实例是从不同的线程访问的,所以我们不得不每次都回到创建一个新的实例。

  • 在Realm实例上访问关系属性时,是
    结果读取的数据是保留在内存中还是每次从
    磁盘读取?我们是否必须担心由于深层关系访问而保留的Realm实例
    变得过大?

  • 什么时候需要刷新Realm实例?我注意到当
    我在Realm浏览器中进行更改时,它们会反映在保留的
    Realm中,而不会调用刷新。



    • 根据文档看起来每个领域都有一个自动刷新属性。

    • How worried should we be about repeatedly creating instances of Realm? What is the overhead?
    • Should we bother retaining Realm instances in ViewControllers or Singletons for repeated use? We've tried this but sometimes the instances are accessed from different threads, so we had to revert back to creating a new instance every time.
    • When accessing relationship properties on Realm instances, is resulting data that is read retained in memory or is it read from disk every time? Do we have to worry about retained Realm instances becoming too large due to deep relationship access?
    • When is refreshing a Realm instance necessary? I've noticed that when I make changes in the Realm browser they are reflected in a retained Realm without calling refresh.
      • Looks like there's a Auto-Refresh property on each realm that causes this according to the documentation.

      例如......

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

      提前致谢。我期待着你的回答。 :D

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

      推荐答案

      (免责声明:我为Realm工作。我离开了Realm现在,但我仍然很乐意提供帮助!):)
      非常感谢!很高兴听到你在享受Realm!

      (Disclaimer: I work for Realm. I've left Realm now, but I'm still happy to help!) :) Thanks a lot! It's great to hear you're enjoying Realm!

      多个领域实例 -
      您根本不需要担心这个问题! Realm文件对象是在每个线程上首次实例化时创建的,每次尝试并在每次执行后都会返回相同的对象。

      Multiple Realm Instances - You don't need to worry about this at all! A Realm file object is created upon the first-time instantiation on each thread, and that same object is subsequently returned each time you try and instance it every time after that.

      保留领域实例 -
      从第一点开始,不,您不必担心在其他对象中挂起Realm引用。由于Realm在内部跟踪其Realm文件对象并返回相同的文件对象,因此不会因为不这样做而受到惩罚。话虽这么说,如果对对象内部的Realm对象进行永久引用可以简化代码复杂性,可以继续使用它。

      Retaining Realm Instances - Following on from the first point, no, you don't need to worry about hanging on to Realm reference inside other objects. As Realm tracks its Realm file objects internally and returns the same ones, you won't get penalised for not doing so. That being said, if having a permanent reference to a Realm object inside your object simplifies your code complexity, feel free to keep using it.

      访问领域关系属性 -
      严格来说,Realm文件中的数据不会从磁盘复制(就像普通的ORM一样);更多它使用内存映射直接将数据从磁盘直接引用到内存中的属性。所以,不,你不必担心Realm文件的内存太大。

      Accessing Realm Relationship Properties - Strictly speaking, data from Realm files aren't copied from disk (Like a normal ORM would do); more it uses memory mapping to directly reference the data from disk straight to your in-memory properties. So, no, you don't need to worry about Realm files getting too large in memory.

      自动刷新
      默认情况下,对主线程上的Realm文件对象启用自动刷新。必须手动为其他线程上的Realm文件对象启用它,或者您可以选择使用 refresh 方法调用手动刷新它们。

      Auto-refresh Auto-refresh is only enabled by default for the Realm file object on the main thread. It has to be manually enabled for Realm file objects on other threads, or you can instead elect to manually refresh them with the refresh method call.

      编辑:我的立场得到了纠正!默认情况下,多个线程上的所有Realm文件对象都具有 autorefresh 。当 autorefresh 打开时,您需要调用 refresh 的唯一时间是您需要对Realm文件所做的更改在运行循环的当前迭代完成之前反映在其他引用中。

      I stand corrected! All Realm file objects on multiple threads have autorefresh on by default. When autorefresh is on, the only time you need to call refresh is if you need the changes made to a Realm file to be reflected in the other references before the current iteration of the run loop has completed.

      引用对象的Realm引用
      绝对不是,这一点都不错!我实际上更喜欢在我自己的个人应用程序中执行此操作,这些应用程序使用Realm提供适当的上下文,因为它总是更容易,并且在代码中提供对象及其父Realm文件之间更强的可视上下文。 (哈哈,是的,如果这里存在线程问题,你可能会在到达点之前发现它。)

      我希望有所帮助!如果您需要澄清这里的任何内容,请告诉我们!

      I hope that helped! Let me know if you need clarification on anything here!

      这篇关于适当的领域使用模式/最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆