簡體   English   中英

如何在React Native應用程序和共享擴展之間共享Realm數據庫

[英]How to share a Realm Database between a React Native app and a Share Extension

有沒有辦法使用React Native API在JavaScript中實例化Realm數據庫,以便包含主應用程序和共享擴展的應用程序組可以訪問它?

我們正在創建一個React Native應用程序,該應用程序需要Share Extension才能使用我們的后端服務將數據導入應用程序。 我們的用戶身份驗證信息存儲在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實現的一些問題,因為Stores在根組件之前被實例化,但需要訪問Realm數據庫。

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

我建議您創建自己的本機模塊,將路徑導出為常量,因為當您加載JS時,它將立即可用。 有關如何執行此操作,請參閱導出常量部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM