简体   繁体   English

使用 Realm React Native 查找 Realm 文件的路径以与 Realm Browser 一起使用

[英]Find path for Realm file with Realm React Native to use with Realm Browser

On iOS, it is possible to inspect the contents of a Realm database by opening the corresponding file with the Realm Browser .在 iOS 上,可以通过使用Realm Browser打开相应的文件来检查Realm数据库的内容。 The path to that file can be printed by using the following line of code (as noted here ):到该文件的路径可以通过使用下面的代码行(如所指出的被打印这里):

print(Realm.Configuration.defaultConfiguration.path)

Is it possible to do the same when using the React Native version of Realm?使用 Realm 的 React Native 版本时是否可以做同样的事情?

刚刚通过挖掘文档自己找到了答案:

console.log('create db:', db.path)

Just in case you can't get the above to work.以防万一你不能让上述工作。 Here is how I did it:这是我如何做到的:

1) import the file in which you define the schema and create a new realm into a component that will mount when you refresh the simulator. 1) 导入您在其中定义架构的文件,并在刷新模拟器时将挂载的组件中创建一个新领域。

import realm from './path_to_file_where_realm created'

2) console.log(realm.path) in the componentWillMount() lifecycle method & enable remote debugging to see the output. 2) 在 componentWillMount() 生命周期方法中的 console.log(realm.path) & 启用远程调试以查看输出。

3) open up realm browser and click open realm file. 3) 打开领域浏览器并单击打开领域文件。 Command Shift G on mac will let you paste in the path from the console.log Mac 上的 Command Shift G 将让您粘贴来自 console.log 的路径

click on the realm file to open it with the realm browser.单击领域文件以使用领域浏览器打开它。

Also for those not familiar with the realm browser, it can be found here .同样对于那些不熟悉领域浏览器的人,可以在这里找到。 It just lets you see & edit data in your realm with a graphical interface.它只是让您使用图形界面查看和编辑领域中的数据。

In my case i used,在我的情况下,我使用,

const Realm = require('realm');
console.log('REALM PATH', Realm.defaultPath);

To elaborate on Daniel Mesa's answer:详细说明 Daniel Mesa 的回答:

In your react native class (assuming ES6 here) below your import lines (above the class definition) add the following:在导入行下方(类定义上方)的 React Native 类(此处假设为 ES6)中,添加以下内容:

const Realm = require('realm');

Then add a componentDidMount function and log the Realm default path:然后添加一个componentDidMount函数并记录 Realm 默认路径:

  componentDidMount() {
    console.log('REALM PATH', Realm.defaultPath);
  }

This should then output the realm db path in the console log (debug your react native app).然后应该在控制台日志中输出领域数据库路径(调试您的本机应用程序)。

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

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