Refactor storage model to work with native Realm

This commit is contained in:
ronaldheft 2022-08-16 16:53:47 -04:00
parent b0905d0270
commit d83e04c47b
33 changed files with 1580 additions and 1305 deletions

View file

@ -0,0 +1,45 @@
//
// DaoExtensions.swift
// App
//
// Created by Ron Heft on 8/16/22.
//
import Foundation
import RealmSwift
extension Object {
func save() {
let realm = try! Realm()
try! realm.write {
realm.add(self, update: .modified)
}
}
func update(handler: () -> Void) {
try! self.realm?.write {
handler()
}
}
}
extension EmbeddedObject {
// Required to disassociate from Realm when copying into local objects
static func detachCopy<T:Codable>(of object: T?) -> T? {
guard let object = object else { return nil }
let json = try! JSONEncoder().encode(object)
return try! JSONDecoder().decode(T.self, from: json)
}
}
protocol Deletable {
func delete()
}
extension Deletable where Self: Object {
func delete() {
try! self.realm?.write {
self.realm?.delete(self)
}
}
}