I think it only happens for Codable properties.
In the following example, b is not persisted, but c is.
struct C: Codable {
var value: Int
}
@Model
class Item {
var a = 0
@Attribute(.ephemeral) var b = 0
@Attribute(.ephemeral) var c = C(value: 0)
init() { }
}
struct ContentView: View {
@Query var items: [Item]
@Environment(\.modelContext) var modelContext
var body: some View {
List {
Section {
Button("add") {
modelContext.insert(Item())
}
}
Section {
ForEach(items) { item in
VStack(alignment: .leading) {
HStack {
Text("a = \(item.a)")
Button("inc") {
item.a += 1
}
}
HStack {
Text("b = \(item.b)")
Button("inc") {
item.b += 1
}
}
HStack {
Text(verbatim: "c = \(item.c)")
Button("inc") {
item.c.value += 1
}
}
}
.buttonStyle(.borderedProminent)
}
}
}
}
}
There's also a behavior that seems stranger. Whenever the app goes to background, b is reset to zero.
Topic:
App & System Services
SubTopic:
iCloud & Data
Tags: