iOS/Sources/Shared/API/Models/RealmPersistable.swift
Zac West 6ae54f679f
Fix server identifier of non-synced actions & complications for deleted servers (#1992)
## Summary
When a server is deleted, we now update the identifiers of any locally-defined actions or complications to point to the first server. We want to avoid ever having a server identifier pointing to a deleted server so we can trust the identifiers more.

## Any other notes
Deleting in this case would be bad as it's an unexpected data loss. I can imagine someone deletes and re-adds the server (potentially the last one, too), or other maintenance tasks.
2021-12-12 10:19:52 -08:00

30 lines
884 B
Swift

import Foundation
import HAKit
import RealmSwift
protocol UpdatableModel {
associatedtype Source: UpdatableModelSource
static func didUpdate(objects: [Self], server: Server, realm: Realm)
static func willDelete(objects: [Self], server: Server?, realm: Realm)
static func primaryKey() -> String? // from realm, we use
static func serverIdentifierKey() -> String
static var updateEligiblePredicate: NSPredicate { get }
static func primaryKey(sourceIdentifier: String, serverIdentifier: String) -> String
func update(with object: Source, server: Server, using realm: Realm) -> Bool
}
extension UpdatableModel {
static var updateEligiblePredicate: NSPredicate { NSPredicate(value: true) }
}
protocol UpdatableModelSource {
var primaryKey: String { get }
}
extension HAEntity: UpdatableModelSource {
var primaryKey: String { entityId }
}