Created
October 20, 2022 23:07
-
-
Save ebubekirsezer/bdcbb3f0c84b86807c71daa13ae50d33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RealmExtension.swift | |
// GitStar | |
// | |
// Created by EbubekirSezer on 21.10.2022. | |
// | |
import Foundation | |
import Realm | |
import RealmSwift | |
private let kMinimumDataVersionKey: String = "kMinimumDataVersionKey" | |
private let kRealmDataVersion: UInt64 = 1 // If any change happen in the Realm DB, increase the parameter by 1 | |
private let kMinimumDataVersion: Int = 1 // If you want to delete all the data increase the parameter by 1 | |
private let kRealmIDKey: String = "realmID" | |
extension Realm { | |
static func migrate() { | |
let config = Realm.Configuration(schemaVersion: kRealmDataVersion) { migration, oldSchemaVersion in | |
if oldSchemaVersion < 1 { | |
migration.enumerateObjects(ofType: Topic.className()) { (_, newObject) in | |
newObject?[kRealmIDKey] = UUID().uuidString // In the migration, we are changing the realmID | |
} | |
} | |
} | |
Realm.Configuration.defaultConfiguration = config | |
do { | |
_ = try Realm() | |
print(config.fileURL ?? "") | |
} catch let error as NSError { | |
print(error) | |
} | |
let minimumDataVersion = kMinimumDataVersion | |
let currentDataVersion = UserDefaults.standard.integer(forKey: kMinimumDataVersionKey) | |
let deleteContent: Bool = currentDataVersion < minimumDataVersion | |
if deleteContent { | |
// We will add delete method here later | |
// RealmManager.deleteAllRealmFiles() | |
UserDefaults.standard.set(minimumDataVersion, forKey: kMinimumDataVersionKey) | |
UserDefaults.standard.synchronize() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment