ios - 如果方案更改则创建新数据库

标签 ios swift realm

如果当前失败,如何创建新的 Realm 数据库?
我在新版本的 Realm 中读到了可能的内容,但没有找到任何内容。

let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let realmPath = "\(documentsDirectory)/db.realm"
let url = URL(string: realmPath)
let config = Realm.Configuration(fileURL: url, readOnly: false, schemaVersion: 1, migrationBlock: nil, objectTypes: nil)
Realm.Configuration.defaultConfiguration = config

最佳答案

Realm 的Configuration struct 有一个属性 deleteRealmIfMigrationNeeded ,这可能就是您正在寻找的。

let config = Realm.Configuration(fileURL: url, readOnly: false, schemaVersion: 1, migrationBlock: nil, objectTypes: nil)
config.deleteRealmIfMigrationNeeded = true
Realm.Configuration.defaultConfiguration = config

如果需要迁移,它会删除并使用新架构重新创建 Realm 文件。

请参阅文档:https://realm.io/docs/swift/2.0.2/api/Classes/Realm/Configuration.html#/s:vVC10RealmSwift5Realm13Configuration28deleteRealmIfMigrationNeededSb

关于ios - 如果方案更改则创建新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40015418/

相关文章:

android - Realm 模型真的需要 getter 和 setter 吗?

react-native - 我一直收到 [RCTCxxBridge jsContextRef] 无法识别的选择器发送到实例 0x7f8af262f4f0' 是从 JS 线程抛出的

AppStore 中的 iOS 应用程序在更新到 iOS 8.0.2 后崩溃但仍在设备和模拟器上运行

ios - 获取 WKInterfaceObject 类的属性

swift - 如何使用自定义动画关闭演示 Controller

swift - 在 Swift 项目中存储扩展的位置

ios - 无法通过对象链接查询 NSData 属性

ios - 将 String 转换为 NSDate 时减去小时

iphone - 如何在 CustomTableViewCell 的 UILabel 上添加跑马灯效果?

ios - 在 iOS 应用程序的快速编程中,UIWebViewController 中的登录/密码授权