Replies: 1 comment
-
My own demo project works pretty much as you described. It creates a modelContext in the
The
The
(this view is not using |
Beta Was this translation helpful? Give feedback.
-
My own demo project works pretty much as you described. It creates a modelContext in the
The
The
(this view is not using |
Beta Was this translation helpful? Give feedback.
-
I'm trying to wrap my head around using SwiftData in an app (macOS) where I'm using SwiftData to persist all my models. For the most part, I've had no trouble, since querying for data is something that can be triggered inside a SwiftUI View, and there's no mutating side effect I need to worry about. However, I do want to control the creation of new model objects inside a Reducer's
body
method, and I've run into a problem I'm not sure how to handle.ModelContext
before they can be associated with one anotherModelContext
is not@Sendable
ModelContext
has to happen on the main actorSo, I have a behavior, "generate a bunch of model objects that will be children of this other model object," and I want to run it in a reducer:
So, this implementation won't compile, because my function
people(in:)
is marked as@MainActor
because it runs a query to fetch all thePerson
model objects out of theModelContext
. Xcode points this out:Call to main actor-isolated global function 'people(in:)' in a synchronous nonisolated context
.I tried writing this as a
.run
Effect, but immediately ran into the problem that aModelContext
isn't@Sendable
.What's the right way to do this? Any ideas?
Beta Was this translation helpful? Give feedback.
All reactions