我试图在我的应用程序中实现Realm数据库,我有一些带有一些Preloaded数据的realm数据库.我搜索了很多堆栈溢出资源,但没有取得任何成功.
到目前为止,我已经完成了以下步骤:
在app delegate中添加了以下代码:
path = Bundle.main.path(forResource: "data", ofType: "realm")
var config = Realm.Configuration(fileURL: NSURL(fileURLWithPath: path!))
config.readOnly = true
// Open the Realm with the configuration
let realm = try! Realm(configuration: config)
Run Code Online (Sandbox Code Playgroud)但这不起作用,请提供一些解决方案.
注意:我不想迁移我的数据库.
提前致谢
下面的代码片段非常适合我在第一次启动应用程序时加载预加载的Realm实例:
let defaultPath = Realm.Configuration.defaultConfiguration.fileURL?.path
let path = Bundle.main.path(forResource: "default", ofType: "realm")
if !FileManager.default.fileExists(atPath: defaultPath!), let bundledPath = path {
do {
try FileManager.default.copyItem(atPath: bundledPath, toPath: defaultPath!)
} catch {
print("Error copying pre-populated Realm \(error)")
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
786 次 |
| 最近记录: |