From c685e01a47cd03c28eb2bb75c8e9662888457b85 Mon Sep 17 00:00:00 2001 From: Levi Eggert Date: Wed, 24 Jul 2024 14:15:23 -0400 Subject: [PATCH] Delete realm files if unsupportedFileFormatVersion exception occurs --- .../Data/RealmDatabase/RealmDatabase.swift | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/godtools/App/Share/Data/RealmDatabase/RealmDatabase.swift b/godtools/App/Share/Data/RealmDatabase/RealmDatabase.swift index 5b5ff6e792..61814833b9 100644 --- a/godtools/App/Share/Data/RealmDatabase/RealmDatabase.swift +++ b/godtools/App/Share/Data/RealmDatabase/RealmDatabase.swift @@ -8,6 +8,7 @@ import Foundation import RealmSwift +import Realm import Combine class RealmDatabase { @@ -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) }