2017-05-17 26 views
1

我创建的应用程序使用现有的数据库领域2.3.0和swift 3.1 xcode 8.3。 但是当我尝试访问领域数据库。有一个错误。现有的Realm数据库在swift中不工作3.1 xcode 8

无法访问数据库:错误域= io.realm代码= 2“无法在路径打开一个境界“/用户/ dodipurnomo /库/开发商/ CoreSimulator /设备/ 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。 realm,

当我尝试执行数据库时,上面是一条错误消息。 而对于类hendleing数据库领域如下:

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如下:

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)") 
     } 
    } 
} 
+0

错误消息的重要部分是“不支持的Realm文件格式版本” –

回答

0

我的境界浏览器中打开数据库我已经更新了数据库。我在打开数据库时出现了一个新问题。为什么数据库变空了?

+0

您是否打开Realm文件的哪个版本的Realm浏览器? – Dmitry

+0

realm 2.7.0:D .. – user7845351

+0

你有2.3.0 Realm浏览器。 – user7845351

1

您收到的错误消息意味着领域文件是使用较新版本的Realm创建的,因此请将Realm更新为最新版本。

另外请记住,如果您打开使用领域更新版本的Realm浏览器的领域,它会要求您转换文件格式。如果你这样做,你可以用老版本的realm-cocoa打开这个领域。

相关问题