简体   繁体   中英

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. When Realm needs to be used, it's always called through 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. 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. 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)

I'm trying to do it globally. I'm making the initial query to show in list view right at the 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

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()
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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