简体   繁体   English

Realm.open vs new Realm

[英]Realm.open vs new Realm

In the context of a React Native app, using Realm locally only (so no realm object server for now). 在React Native应用程序的上下文中,仅在本地使用Realm(因此暂时不使用域对象服务器)。 What is the difference between opening a realm using Realm.open({schema: [Car, Person]}) and creating a new Realm instance with new Realm({schema: [Car, Person]}); 使用Realm.open({schema: [Car, Person]})打开域和使用新Realm创建新Realm实例之间有什么区别new Realm({schema: [Car, Person]}); When should I use one or the other? 我什么时候应该使用其中一种?

Looks like Realm.open is just a callback to make sure the syncronisation is done, so only needed for synced Realms? 看起来Realm.open只是一个回调来确保同步完成,所以只需要同步的Realms?

So far this is what I found: 到目前为止,这是我发现的:

Realm.open: Realm.open:

According to the doc reference Realm.open means "Open a Realm asynchronously with a promise. If the Realm is synced, it will be fully synchronized before it is available." 根据文档引用, Realm.open意思是“与一个承诺异步打开一个领域。如果领域同步,它将在它可用之前完全同步。” It's the way shown in the js docs 这是js docs中显示的方式

new Realm: 新境界:

According to the constructor in the doc reference , new Realm will Create a new Realm instance [...]. 根据doc参考中的构造函数, new Realm创建一个新的Realm实例[...]。 If a Realm does not yet exist [...], then this constructor will create it. 如果一个领域尚不存在[...],那么这个构造函数将创建它。 Otherwise, the instance will access the existing Realm 否则,实例将访问现有的Realm

In the React example they use new: https://github.com/realm/realm-js/blob/master/examples/ReactExample/components/realm.js 在React示例中,他们使用new: https//github.com/realm/realm-js/blob/master/examples/ReactExample/components/realm.js

Is there any specific usecase you have in mind ? 你有什么具体的用例吗? Otherwise within react native i havent had to use Realm.open so far. 否则反应原生我到目前为止还没有必须使用Realm.open。 They way i've seen most people approach the architecture is to have a file named realm.js where the schemas are defined and a new Realm is exported like so 他们看到大多数人接近这个架构的方式是有一个名为realm.js的文件,其中定义了模式,并且导出了一个新的Realm

class ShoppingCartItem extends Realm.Object{}
ShoppingCartItem.schema = {
    name: 'ShoppingCartItem',
    primaryKey: 'ordernumber',
    properties: {
        ordernumber: 'string',
        quantity: 'int',
        unitPrice: 'double'
    }
}

export default new Realm({schema: [User, LastViewedProduct, ShoppingCartItem]});

and then have another file called realmTasks.js where you import that realm object and define actions on it. 然后有另一个名为realmTasks.js的文件,您可以在其中导入该领域对象并在其上定义操作。 If you need the realm object in the local state to register changes you can define a callback and give it the realm reference as an argument such as 如果你需要本地状态的域对象来注册更改,你可以定义一个回调并给它作为参数的域引用,如

export function addShoppingCartItem(ordernumber, quantity, unitPrice, callback){
    realm.write(() => {
        realm.create('ShoppingCartItem', {
            ordernumber: ordernumber,
            quantity: quantity,
            unitPrice: unitPrice
        });
    })
    return callback(realm);
}

I'm not sure if this completely answers your question or is useful to you at all but i hope this gives you a hint :) 我不确定这完全回答你的问题或对你有用,但我希望这会给你一个提示:)

The answer from docs can completely cover your question. 文档的答案可以完全涵盖您的问题。 So using sync approach ( const realm = new Realm() ) is more simple and appropriate for exporting realm as a module, but it's still preferable to get promise Realm.open() and handle realm instance when schema migration and sync from remote server are completed. 因此,使用同步方法( const realm = new Realm() )更简单,更适合将域导出为模块,但是当从远程服务器进行模式迁移和同步时,仍然可以获得承诺Realm.open()并处理realm实例。完成。

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

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