【发布时间】:2018-06-14 01:55:45
【问题描述】:
在 Swift 应用程序中,我在 didFinishLaunchingWithOptions 中设置默认的 Realm 配置,如下所示:
Realm.Configuration.defaultConfiguration = {
var config = Realm.Configuration.defaultConfiguration
config.deleteRealmIfMigrationNeeded = true
return config
}()
然后我在需要时以 3 种不同的方式创建新的 Realm 实例 - 阅读时:
let realm = try! Realm()
let users = realm.objects(User.self)
写作时:
let realm = try! Realm()
if let user = realm.object(ofType: User.self, forPrimaryKey: userId) {
try! realm.write {
user.name = name
}
}
在模型中:
import Foundation
import RealmSwift
import ObjectMapper
final class User: Object, StaticMappable {
@objc dynamic var id = 0
@objc dynamic var name = ""
override static func primaryKey() -> String? {
return "id"
}
func mapping(map: Map) {
id <- map["id"]
name <- map["name"]
}
static func objectForMapping(map: Map) -> BaseMappable? {
let objectOptional = try? Realm().object(ofType: self, forMapping: map)
if let object = objectOptional {
return object
}
return nil
}
}
问题是有时我在完成闭包中创建新的 Realm 实例时会收到此错误,这些实例被大量使用:
致命错误:“试试!”表达式意外引发错误:错误 Domain=io.realm Code=1 "路径中的领域 '/var/mobile/Containers/Data/Application/...../Documents/default.realm' 已使用不同的模式模式打开。” UserInfo={NSLocalizedDescription=路径中的领域 '/var/mobile/Containers/Data/Application/...../Documents/default.realm' 已使用不同的架构模式打开。错误代码=1
如何调试和解决这个问题?
【问题讨论】:
标签: swift multithreading closures realm