【发布时间】:2018-04-14 16:34:27
【问题描述】:
我在一个应用程序中有两个领域文件,当我想迁移它们时出现问题。我希望在 Xcode 中运行时自动更新领域,而不是每次都更改 schemaVersion。
class News: Object {
@objc dynamic var name: String
}
class NewsManager {
static var realm = try! Realm()
static var cacheRealm: Realm = {
let documentDirectory = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask,
appropriateFor: nil, create: false)
let url = documentDirectory.appendingPathComponent("cache.realm")
var config = Realm.Configuration()
config.fileURL = url
return try! Realm(configuration: config)
}()
}
当我向 News 添加一个新属性时,例如 @objc dynamic var title: String, 我在 AppDelegate func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool
中添加以下代码let config = Realm.Configuration(schemaVersion: 1, migrationBlock: { migration, oldSchemaVersion in
})
Realm.Configuration.defaultConfiguration = config
返回尝试崩溃的消息! NewsManager 中的领域(配置:config)。
Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=10 "Migration is required due to the following errors:
- Property 'News.test' has been added." UserInfo={Error Code=10, NSLocalizedDescription=Migration is required due to the following errors:
- Property 'News.test' has been added.}: file /BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-900.0.69.2/src/swift/stdlib/public/core/ErrorType.swift, line 181
我该怎么办?
领域:3.0.1
斯威夫特:4.0
iOS:10
【问题讨论】: