Skip to content

Commit 72287cc

Browse files
committed
Use proper main actor isolation for modelToContainer
Only backing SwiftUI modifiers, aka main.
1 parent c5e69c5 commit 72287cc

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Sources/ManagedModels/SwiftUI/ModelContainer+SwiftUI.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public extension View {
2525
self.modelContext(container.viewContext)
2626
}
2727

28+
@MainActor
2829
@ViewBuilder
2930
func modelContainer(
3031
for modelTypes : [ any PersistentModel.Type ],
@@ -48,6 +49,7 @@ public extension View {
4849
}
4950
}
5051

52+
@MainActor
5153
@inlinable
5254
func modelContainer(
5355
for modelType : any PersistentModel.Type,
@@ -73,6 +75,7 @@ public extension Scene {
7375
self.modelContext(container.viewContext)
7476
}
7577

78+
@MainActor
7679
@SceneBuilder
7780
func modelContainer(
7881
for modelTypes : [ any PersistentModel.Type ],
@@ -92,6 +95,7 @@ public extension Scene {
9295
self.modelContainer(try! result.get())
9396
}
9497

98+
@MainActor
9599
@inlinable
96100
func modelContainer(
97101
for modelType : any PersistentModel.Type,
@@ -113,8 +117,10 @@ public extension Scene {
113117
// MARK: - Primitive
114118

115119
// Note: The docs say that a container is only ever created once! So cache it.
116-
nonisolated(unsafe) private var modelToContainer = [ ObjectIdentifier: NSPersistentContainer ]()
120+
@MainActor
121+
private var modelToContainer = [ ObjectIdentifier: NSPersistentContainer ]()
117122

123+
@MainActor
118124
private func makeModelContainer(
119125
for modelTypes : [ any PersistentModel.Type ],
120126
inMemory : Bool = false,

0 commit comments

Comments
 (0)