简体   繁体   English

最好每次打开一个全局Realm对象还是使用Realm.open? Real.open与新领域

[英]It it better to open a global Realm object or use Realm.open everytime? Real.open vs new Realm

In Realm introduction for React Native, there's no global usage of Realm. 在React Native的Realm简介中,没有Realm的全局用法。 When Realm needs to be used, it's always called through Realm.open() . 当需要使用Realm时,总是通过Realm.open()调用它。

What is the correct way of doing it? 正确的做法是什么? Seems like opening a new Realm each time may be inneficient, but there's no comment on the mechanism of Realm.open. 似乎每次打开一个新的Realm可能都是无效的,但是对Realm.open的机制没有任何评论。 It might be cacheing things, I don't know. 我不知道这可能是在缓存东西。

On the other side, new Realm() can be used and stored in a way that's accessible throughout the entire application. 另一方面,可以通过整个应用程序可访问的方式使用和存储new Realm() However, its creation is not assynchronous like Real.open() and I've run through some delay problems on using this technique (it can be made assynchronous too, though) 但是,它的创建不像Real.open()那样Real.open()并且在使用此技术时我遇到了一些延迟问题(不过也可以使其异步)。

I'm trying to do it globally. 我正在尝试在全球范围内做。 I'm making the initial query to show in list view right at the componentWillMount() . 我正在使初始查询显示在componentWillMount()列表视图中。 It takes some time for the list view to initially appear and fr some reason it only displays some objects and then takes some seconds to display the rest. 列表视图最初显示需要花费一些时间,并且由于某种原因它仅显示一些对象,然后花费几秒钟来显示其余对象。

I'm doing as seen in Realm.open vs new Realm on the link https://github.com/realm/realm-js/blob/master/examples/ReactExample/components/realm.js 我正在做Realm.open与新Realm链接https://github.com/realm/realm-js/blob/master/examples/ReactExample/components/realm.js

Performance wise, if you plan to have a single realm schema, I would recommend to keep a global Realm. 在性能方面,如果您打算有一个单一的领域架构,我建议保留一个全局领域。 eg: 例如:

Realm.open(mySchema).then(realm => {
  this.setState({realm})
})

I think you just need to care about opening and closing realm if you plan to have multiple schemas. 我认为,如果您打算拥有多个架构,则只需要关心打开和关闭领域。 eg: 例如:

Realm.open(mySchema).then(realm => {
  //do stuff
  realm.close()
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM