2017-05-17 27 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打開這個領域。

相關問題