From 4576663edb29125815a30d4f04930d2699731946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20G=C3=BCnd=C3=BCz?= Date: Tue, 13 Oct 2026 14:24:02 +0200 Subject: [PATCH 1/2] Describe precisely what isPersisted checks The documentation said the model "exists in the database", but the property only checks whether it was inserted into a ModelContext, which is true before the context is saved. It now says so and points to persistentModelID.isTemporary for the saved state. --- Sources/HandySwiftUI/Extensions/PersistentModelExt.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/HandySwiftUI/Extensions/PersistentModelExt.swift b/Sources/HandySwiftUI/Extensions/PersistentModelExt.swift index 18bb182..1012997 100644 --- a/Sources/HandySwiftUI/Extensions/PersistentModelExt.swift +++ b/Sources/HandySwiftUI/Extensions/PersistentModelExt.swift @@ -3,11 +3,11 @@ @available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) extension PersistentModel { - /// Returns whether this model instance is currently managed by a ModelContext. + /// Returns whether this model instance is currently managed by a `ModelContext`. /// - /// Use this property to determine if a model instance has ever been inserted into - /// a ModelContext (i.e., exists in the database) or if it's just an in-memory instance - /// that hasn't been saved yet. + /// Use this property to determine if a model instance has been inserted into a `ModelContext` or if it's just an in-memory + /// instance that was never inserted. Inserting is enough for this to be `true`: the model might not be saved to the store yet. + /// To check whether it was saved, use `persistentModelID.isTemporary` (iOS 27, macOS 27, tvOS 27, watchOS 27, visionOS 27). /// /// Example: /// ```swift From cfcc871915079d0c02dcb9318b68c9e7d574183b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20G=C3=BCnd=C3=BCz?= Date: Wed, 14 Oct 2026 16:11:44 +0200 Subject: [PATCH 2/2] Test that inserting a model is enough for isPersisted The doc comment now says that isPersisted turns true on insert, before any save; a test with an in-memory container pins that. The example also called insert with try, which does not throw. --- .../Extensions/PersistentModelExt.swift | 2 +- .../PersistentModelExtTests.swift | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 Tests/HandySwiftUITests/PersistentModelExtTests.swift diff --git a/Sources/HandySwiftUI/Extensions/PersistentModelExt.swift b/Sources/HandySwiftUI/Extensions/PersistentModelExt.swift index 1012997..df4da84 100644 --- a/Sources/HandySwiftUI/Extensions/PersistentModelExt.swift +++ b/Sources/HandySwiftUI/Extensions/PersistentModelExt.swift @@ -14,7 +14,7 @@ /// let task = Task(title: "New Task") /// print(task.isPersisted) // false /// - /// try modelContext.insert(task) + /// modelContext.insert(task) /// print(task.isPersisted) // true /// ``` public var isPersisted: Bool { diff --git a/Tests/HandySwiftUITests/PersistentModelExtTests.swift b/Tests/HandySwiftUITests/PersistentModelExtTests.swift new file mode 100644 index 0000000..15eb2ed --- /dev/null +++ b/Tests/HandySwiftUITests/PersistentModelExtTests.swift @@ -0,0 +1,27 @@ +import SwiftData +import Testing + +@testable import HandySwiftUI + +@Model +final class TestItem { + var title: String + + init(title: String) { + self.title = title + } +} + +@MainActor +struct PersistentModelExtTests { + @Test + func insertingIsEnoughWithoutSaving() throws { + let container = try ModelContainer(for: TestItem.self, configurations: ModelConfiguration(isStoredInMemoryOnly: true)) + let item = TestItem(title: "New Item") + #expect(!item.isPersisted) + + container.mainContext.insert(item) + #expect(item.isPersisted) + #expect(container.mainContext.hasChanges, "not saved yet") + } +}