Emi*_*lla 0 realm reactjs react-native
我有一个关于在反应本机中创建和使用领域数据库的问题。
我有两个屏幕,负责执行crud、category.js和client.js等操作
在category.js中我有以下构造函数
constructor(props) {
super(props);
realm = new Realm({
schema: [{ name: 'category', primaryKey: 'id', properties: { id: 'int', descricao: 'string', status: 'bool' } }]
})
}
Run Code Online (Sandbox Code Playgroud)
在cliente.js中我有
constructor(props) {
super(props);
realm = new Realm({
schema: [{ name: 'client', primaryKey: 'id', properties: { id: 'int', nome: 'string', cpf: 'string', celular: 'string', status: 'bool' } }]
})
}
Run Code Online (Sandbox Code Playgroud)
现在我有以下疑问。
对于这个模式,我是否有一个包含表类别和客户端的数据库?
当我在 client.js 屏幕上并且想要转到 Category.js 时,我收到以下错误通知:已在具有不同架构的当前线程上打开。
如何关闭或打开连接,以便我可以在两个屏幕上使用该领域?
您不是在realm尝试创建新实例的新屏幕上打开。这就是您遇到问题的原因。您的应用程序中应该只有一个 实例realm。
realm依赖项的文件。new Realm()实例并将您的架构添加到其中realm。realm在组件中使用realm您刚刚创建的实例。realm.jsimport Realm from 'realm';
class Category extends Realm.Object {}
Category.schema = {
name: 'category',
primaryKey: 'id',
properties: {
id: 'int',
descricao: 'string',
status: 'bool'
}
};
class Client extends Realm.Object {}
Client.schema = {
name: 'client',
primaryKey: 'id',
properties: {
id: 'int',
nome: 'string',
cpf: 'string',
celular: 'string',
status: 'bool'
}
};
const RealmInstance = new Realm({ schema: [Category, Client] });
export default RealmInstance;
Run Code Online (Sandbox Code Playgroud)
然后你应该可以通过以下方式导入它
import realm from './path/to/realm.js'
Run Code Online (Sandbox Code Playgroud)
您可以realm通过查看他们的示例来了解如何做到这一点。
| 归档时间: |
|
| 查看次数: |
1777 次 |
| 最近记录: |