realm 共用
https://stackoverflow.com/questions/40428923/realm-react-native-and-objective-c
rn
1:yarn add realm --save
这时候,如果直接运行,会错误:红屏错误
Cannot read property 'debugHosts' of undefined
2:react-native link realm
链接原生库
运行正常
native - ios
1:pod ‘Realm’
这时候运行失败:
pod 集成的realm中的librealmcore-ios.a 和 rn 链接原生库libRealmReact.a 有字段重复定义

2:将pod集成的realm需要的静态库删除
这时候运行报错:

3:将rn链接原生库libRealmReact.a删除,报红屏错误:Cannot read property 'debugHosts' of undefined
具体问题演示
注:很恶心,无法上传pdf,这个链接可能打开的草鸡慢,抱歉,下面官方沟通记录里也有
官方回应
I did some research and found that the reason you may been running into issues is that Realm can't support two SDKs at the same time. When you begin working with realm objects there is no way to convert the format from framework to framework.
I'm sorry if this doesn't work your use case. Our engineering is aware of the problem and may work on it in the future.
20180626
- 之前研究rn-native 共用realm:问题在于环境集成上:cocoapods
https://forums.realm.io/t/using-same-realm-db-in-react-native-and-native-ios/712
跟官方沟通暂时不支持
- 近期在rn学习群里看到有说realm可以共用的:
- 实践:native-ios 使用其他方式集成realm
1:Dynamic Framework
2:cocoapods
3:Static Framework(ios only)。后续验证
- 结论:
1:环境可行
rn访问realm数据库出问题:property ‘xxx’ has been made required
native-ios 访问realm数据库问题:property ‘xxx’ has been made optional
2:cocoapds方式集成依然不行,后续跟进删除一方的文件依赖试试。但是感觉不行,因为rn,native中关于realm的依赖方式不同。
3:环境可行
20180627
方案
https://stackoverflow.com/questions/40428923/realm-react-native-and-objective-c
1:native 提供方法 共rn调用:RCTBridgeModule。在方法上更统一
2:各自独立访问,感觉在结构上更合理一些。
- 路径问题:
native-ios:
rn:
确定是否一样:路径相同,表示操作同一个realm文件
指定路径:ios 》 rn
- 权限问题:
Access Control
rn中:
realm中的对象属性总是可选的,不需要显式地指定,也不需要指定。默认optional
field:‘type’ (required)
filed:‘type?’ (optional)
filed:{type:‘type’, optional:true/false}
native-ios:
复写requiredproperties方法,return @[@“filed”,@“filed”]; 指定字段是required的。需要显示指定required/optional
解决方式:保持两端permission一致即可
1:required
2:optional
- 测试:
native-ios ~ rn : 共用realm 读写正常
网友评论