Skip to content

Commit

Permalink
Delete realm files if unsupportedFileFormatVersion exception occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
levieggertcru committed Jul 24, 2024
1 parent cf74d25 commit c685e01
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion godtools/App/Share/Data/RealmDatabase/RealmDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation
import RealmSwift
import Realm
import Combine

class RealmDatabase {
Expand All @@ -20,8 +21,38 @@ class RealmDatabase {

self.databaseConfiguration = databaseConfiguration
config = databaseConfiguration.getRealmConfig()

_ = checkForUnsupportedFileFormatVersionAndDeleteRealmFilesIfNeeded(config: config)
}


private func checkForUnsupportedFileFormatVersionAndDeleteRealmFilesIfNeeded(config: Realm.Configuration) -> Error? {

do {

_ = try Realm(configuration: config)

}
catch let realmConfigError as NSError {

if realmConfigError.code == Realm.Error.unsupportedFileFormatVersion.rawValue {

do {
_ = try Realm.deleteFiles(for: config)
}
catch let deleteFilesError {

return deleteFilesError
}
}
else {

return realmConfigError
}
}

return nil
}

func openRealm() -> Realm {
return try! Realm(configuration: config)
}
Expand Down

0 comments on commit c685e01

Please sign in to comment.