mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-07-12 23:14:48 +02:00
Refactor storage model to work with native Realm
This commit is contained in:
parent
b0905d0270
commit
d83e04c47b
33 changed files with 1580 additions and 1305 deletions
45
ios/App/Shared/util/DaoExtensions.swift
Normal file
45
ios/App/Shared/util/DaoExtensions.swift
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue