Realm.open与新Realm [英] Realm.open vs new Realm

查看:158
本文介绍了Realm.open与新Realm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在React Native应用程序的上下文中,仅在本地使用Realm(因此暂时没有Realm对象服务器).使用打开领域之间有什么区别 Realm.open({schema: [Car, Person]})并使用new Realm({schema: [Car, Person]});创建一个新的Realm实例,何时应该使用其中一个?

In the context of a React Native app, using Realm locally only (so no realm object server for now). 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]}); When should I use one or the other?

看起来像Realm.open只是一个回调,以确保同步已完成,因此仅对于同步的领域才需要?

Looks like Realm.open is just a callback to make sure the syncronisation is done, so only needed for synced Realms?

到目前为止,这是我发现的:

So far this is what I found:

根据文档参考 Realm.open表示带有承诺异步打开一个领域.如果该领域已同步,它将在可用之前完全同步." 这是根据文档参考中的构造函数, new Realm创建一个新的Realm实例[...].如果领域尚不存在,则此构造函数将创建它.unesdoc.unesco.org unesdoc.unesco.org否则,实例将访问现有的领域

According to the constructor in the doc reference, new Realm will Create a new Realm instance [...]. If a Realm does not yet exist [...], then this constructor will create it. Otherwise, the instance will access the existing Realm

在React示例中,他们使用新的: https://github.com/realm/realm-js/blob/master/examples/ReactExample/components/realm.js

In the React example they use new: https://github.com/realm/realm-js/blob/master/examples/ReactExample/components/realm.js

推荐答案

您是否有特定的用例?否则,在本机反应范围内,到目前为止我还没有使用Realm.open. 我见过大多数人采用的方法是,该体系结构的文件名为 realm.js 这样定义模式并导出新的领域

Is there any specific usecase you have in mind ? Otherwise within react native i havent had to use Realm.open so far. 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

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]});

,然后创建另一个名为realmTasks.js的文件,您可以在其中导入该领域对象并对其进行定义.如果您需要处于本地状态的领域对象来注册更改,则可以定义一个回调并将领域引用作为参数提供给它,例如

and then have another file called realmTasks.js where you import that realm object and define actions on it. 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 :)

这篇关于Realm.open与新Realm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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