【发布时间】: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