【问题标题】:How to share a Realm Database between a React Native app and a Share Extension如何在 React Native 应用程序和共享扩展之间共享 Realm 数据库
【发布时间】:2016-04-12 16:59:49
【问题描述】:

有没有办法使用 React Native API 在 JavaScript 中实例化 Realm 数据库,以便包含主应用和共享扩展的应用组可以访问它?

我们正在创建一个 React Native 应用程序,该应用程序需要一个共享扩展来使用我们的后端服务将数据导入应用程序。我们的用户身份验证信息存储在 Realm 数据库中,共享扩展也需要访问此信息才能代表用户联系服务。

目前,我们找到的唯一方法是计算AppDelegate中的Realm路径,然后将其传递给我们的根组件,如下所示:

NSURL *realmPathUrl = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.company.product"];
NSString *realmPath = [[realmPathUrl path] stringByAppendingPathComponent:@"db.realm"];
NSDictionary *props = @{@"realmPath" : realmPath};
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"product" initialProperties:props launchOptions:launchOptions];

然后在根组件中:

const userPrefsRealmDatabase = new Realm({path: props.realmPath, ...otherOptions});

但这是一个非常混乱的解决方案,并且在我们的 Flux 实现中引入了一些问题,因为 Store 在根组件之前被实例化,但需要访问 Realm 数据库。

【问题讨论】:

  • 我有一个类似的案例,请您分享一下您是如何做到的?我真的很感激。提前致谢。

标签: ios react-native realm


【解决方案1】:

我建议您创建自己的本机模块,将路径导出为常量,因为当您的 JS 加载时它会立即可用。请参阅Exporting Constants 部分了解如何执行此操作。

【讨论】:

  • 谢谢,这就是我们最终要做的,可能是最好的解决方案
  • 做同样的事情也会解决Android中的问题吗?
【解决方案2】:

你可以看看 react-native 插件https://github.com/realm/realm-js

【讨论】:

  • 谢谢,我们已经在使用 React Native 插件,但它(似乎)没有本地方式将 Realm 放入应用组的共享数据文件夹。它允许您在初始化时为 Realm 文件指定 path,但据我所知,path 必须在 AppDelegate 中计算,这会带来很多复杂性。
猜你喜欢
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
  • 2019-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多