我正在使用领域开发一个应用程序。然后我想通过应用程序上的一组 .realm 文件来初始化领域。它最初位于 /Users/*****/Library/Developer/CoreSimulator/Devices/~****~/Documents/default.realm/ 我希望应用程序文件夹中的 default.realm 文件与 Storyboard 和 ViewController 对齐.swift 和其他文件。我该如何解决这个问题?
您可以在 Realm init 方法中传递 fileURL
let config = Realm.Configuration(
// Get the URL to the bundled file
fileURL: Bundle.main.url(forResource: "MyBundledData", withExtension: "realm"),
// Open the file in read-only mode as application bundles are not writeable
readOnly: true)
// Open the Realm with the configuration
let realm = try! Realm(configuration: config)
Run Code Online (Sandbox Code Playgroud)
重要提示:请参阅上面代码中的注释:
以只读模式打开文件,因为应用程序包不可写
要将领域文件放在您的包中,您将无法写入它,它将是只读的。如果您想随应用程序一起提供预填充的数据库,您可以在捆绑包中执行此操作,但如果您希望它可写,则需要在首次启动时将其复制到应用程序的文档目录中,然后使用中的版本那个目录代替。
您在 xcode 中添加并包含在构建设置的“复制捆绑包资源”中的资源/文件都包含在您的捆绑包中。该包不可写。要随您的应用程序发送需要可写的文件,需要将其移动到合适的文件夹。通常是应用程序文档文件夹。
| 归档时间: |
|
| 查看次数: |
2436 次 |
| 最近记录: |