我创建了应用程序使用现有的数据库领域2.3.0和swift 3.1 xcode 8.3 . 但是当我尝试访问领域数据库时 . 有一个错误 .
无法访问数据库:错误域= io.realm代码= 2“无法在路径上打开域”/ Users / dodipurnomo / Library / Developer / CoreSimulator / Devices / 858C796B-CBA8-424B-9A97-0893304B758B / data / Containers /Data/Application/A2D910EE-AAC5-4836-9FE7-97F744E802E5/Documents/Conversio.realm':不支持的Realm文件格式版本 . “的UserInfo = {NSFilePath = /用户/ dodipurnomo /库/开发商/ CoreSimulator /设备/ 858C796B-CBA8-424B-9A97-0893304B758B /数据/容器/数据/应用/ A2D910EE-AAC5-4836-9FE7-97F744E802E5 /文档/ Conversio . 领域,
当我尝试执行数据库时,上面是一条错误消息 . 至于数据库领域的类如下:
import RealmSwift
import UIKit
class DBManager{
//MARK: - Singleton shared intance
static let sharedIntance = DBManager()
//MARK: - overide init function in realm
static var realm: Realm {
get {
do {
let realm = try Realm()
return realm
}
catch {
print("Could not access database: ", error)
}
return self.realm
}
}
public static func write(realm: Realm, writeClosure: () -> ()) {
do {
try realm.write {
writeClosure()
}
} catch {
print("Could not write to database: ", error)
}
}
public static func query(realm: Realm,queryClosure: () -> ()){
}
func save(entityList: [Object], shouldUpdate update: Bool = false) {
DBManager.realm.beginWrite()
for entity in entityList {
if let key = type(of: entity).primaryKey(), let value = entity[key] , update {
if let existingObject = DBManager.realm.object(ofType: type(of: entity), forPrimaryKey: value as AnyObject) {
let relationships = existingObject.objectSchema.properties.filter {
$0.type == .array
}
for relationship in relationships {
if let newObjectRelationship = entity[relationship.name] as? ListBase , newObjectRelationship.count == 0 {
entity[relationship.name] = existingObject[relationship.name]
}
}
}
}
DBManager.realm.add(entity, update: update)
}
do {
try DBManager.realm.commitWrite()
} catch let writeError {
debugPrint("Unable to commit write: \(writeError)")
}
DBManager.realm.refresh()
}
}
我在appdelegate中设置了Realm如下:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let desPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let fullDesPath = URL(fileURLWithPath: desPath).appendingPathComponent("Conversio.realm")
var config = Realm.Configuration()
config.deleteRealmIfMigrationNeeded = true
config.fileURL = fullDesPath
Realm.Configuration.defaultConfiguration = config
chekDB()
return true
}
//chek database
func chekDB() {
let bundleDB = Bundle.main.path(forResource: "Conversio", ofType: "realm")
let desPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let fileManager = FileManager.default
let fullDesPath = URL(fileURLWithPath: desPath).appendingPathComponent("Conversio.realm")
let fullDestPathString = String(describing: fullDesPath)
if fileManager.fileExists(atPath: fullDesPath.path){
print("Database file is exis !")
print(fileManager.fileExists(atPath: bundleDB!))
}else{
do{
try fileManager.copyItem(atPath: bundleDB!, toPath: fullDesPath.path)
}catch{
print("error encured while copying file to directori \(fullDestPathString)")
}
}
}
2 回答
我在领域浏览器中打开了数据库,我更新了数据库 . 而且我在打开数据库时遇到了新问题 . 为什么数据库变空?
您获得的错误消息意味着Realm文件是使用较新版本的Realm创建的,因此将Realm更新为最新版本 .
另外请记住,如果您使用Realm Browser打开领域,它使用较新版本的领域,则会要求您转换文件格式 . 如果你这样做,你可以用旧版本的realm-cocoa打开这个领域 .