I am persisting a tree data structure Node
using SwiftData with CloudKit enabled. I understand how to manage my NonSingleton
models in SwiftUI. However, when a user first launches my app a Node
model instance needs to be created that is separate from any NonSingleton
model. So I think a Singleton
class is appropriate but I don't know how to persist it?
@Model
final class Node {
var parent: Node?
@Relationship(deleteRule: .cascade, inverse: \Node.parent)
var children = [Node]()
// ...
}
@Model
final class NonSingleton {
@Relationship(deleteRule: .cascade) var node: Node
// ...
}
@Model // ?
final class Singleton {
static var node = Node()
private init() {}
}