Skip to content

Instantly share code, notes, and snippets.

@imnaveensharma
Last active September 19, 2020 09:09
Show Gist options
  • Save imnaveensharma/b6e7064706d31dac2c0f8fa59d5dea82 to your computer and use it in GitHub Desktop.
Save imnaveensharma/b6e7064706d31dac2c0f8fa59d5dea82 to your computer and use it in GitHub Desktop.
Store Data in Realm Database
import Realm
import RealmSwift
/*
https://realm.io/docs/swift/latest/
https://github.com/realm/realm-cocoa
https://www.raywenderlich.com/9220-realm-tutorial-getting-started
https://medium.com/flawless-app-stories/crud-operation-using-realmswift-part-1-17a99de83cc1
https://medium.com/@aliakhtar_16369/realm-notifications-realmswift-part-2-60c66ab99ea9
https://medium.com/@aliakhtar_16369/realm-custom-configuration-and-encryption-realmswift-part-3-f991f090ae22
*/
class RealmManager: NSObject {
}
public extension Realm {
static func printDefaultConfiguration() {
print("RealmManager :: Realm DB Default Configuration :: \(Realm.Configuration.defaultConfiguration)")
}
static func printDBPath() {
print("RealmManager :: Realm DB Path :: \(Realm.Configuration.defaultConfiguration.fileURL?.absoluteString ?? "Realm DB Path Not Found")")
}
func safeAdd(object: Object) {
self.add(object, update: .modified)
}
static func readData(_ block: (Realm) throws -> Void) {
do {
if let realm = try? Realm() {
try block(realm)
}
else
{
Realm.removeOldRealmDataFile()
// Re-try again
let realm = try Realm()
try block(realm)
}
} catch {
print("RealmManager :: Error on Read Realm DB :: \(error.localizedDescription)")
}
}
static func updateData(_ block: (Realm) throws -> Void) {
do {
if let realm = try? Realm() {
realm.beginWrite()
try block(realm)
try realm.commitWrite()
} else {
Realm.removeOldRealmDataFile()
// Re-try again
let realm = try Realm()
realm.beginWrite()
try block(realm)
try realm.commitWrite()
}
} catch {
print("RealmManager :: Error on Write Realm DB :: \(error.localizedDescription)")
}
}
static func removeOldRealmDataFile() {
if let realmURL = Realm.Configuration.defaultConfiguration.fileURL {
let realmURLs = [
realmURL,
realmURL.appendingPathExtension("lock"),
realmURL.appendingPathExtension("note"),
realmURL.appendingPathExtension("management"),
]
for URL in realmURLs {
do {
try FileManager.default.removeItem(at: URL)
} catch {
print("RealmManager :: Still failed when try to remove old realm data files.")
}
}
}
else
{
print("RealmManager :: Still failed when find realm url.")
}
}
}
//Stuffbee
// Write to Realm
Realm.updateData({ realm in
realm.safeAdd(object: objModel)
})
// Read from Realm
var models: [Model]?
Realm.readData { realm in
let modelsFromDB = realm.objects(Model.self) //Result in [Model]
let modelsFromDBWithFilter = modelsFromDB.filter { $0.itemId == itemId }
}
// Remove
Realm.readData { realm in
let modelsFromDB = realm.objects(Model.self)
try realm.write {
realm.delete(modelsFromDB)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment