Created
October 20, 2022 23:15
-
-
Save ebubekirsezer/cdeed859c217d7866550b5988b9e5360 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
// | |
// RealmManager.swift | |
// GitStar | |
// | |
// Created by EbubekirSezer on 21.10.2022. | |
// | |
import Foundation | |
import Realm | |
import RealmSwift | |
class RealmManager { | |
// REALM SERVICE | |
static let service: RealmManager = RealmManager.init() | |
static func deleteAllRealmFiles() { | |
let base: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! | |
func removeFileAt(path: RealmFileType) { | |
try? FileManager.default.removeItem(at: base.appendingPathComponent(path.rawValue)) | |
} | |
for path in RealmFileType.allCases { | |
removeFileAt(path: path) | |
} | |
} | |
func realm(named: String) -> Realm? { | |
var config = Realm.Configuration.defaultConfiguration | |
guard let fileUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last?.appendingPathComponent(named + ".realm") else { | |
return self.realm(named: named) | |
} | |
config.fileURL = fileUrl | |
do { | |
return try Realm(configuration: config, queue: nil) | |
} catch { | |
print("RealmError:\(error.localizedDescription)") | |
return self.realm(named: named) | |
} | |
} | |
func realmConfiguration(for path: RealmFileType = .Default) -> Realm.Configuration { | |
var config = Realm.Configuration.defaultConfiguration | |
guard let fileUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last?.appendingPathComponent(path.rawValue + ".realm") else { | |
return config | |
} | |
config.fileURL = fileUrl | |
return config | |
} | |
func migrate() { | |
Realm.migrate() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment