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.
Uh oh!
There was an error while loading. Please reload this page.
-
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
bodymethod, and I've run into a problem I'm not sure how to handle.ModelContextbefore they can be associated with one anotherModelContextis not@SendableModelContexthas 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@MainActorbecause it runs a query to fetch all thePersonmodel 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
.runEffect, but immediately ran into the problem that aModelContextisn't@Sendable.What's the right way to do this? Any ideas?
Beta Was this translation helpful? Give feedback.
All reactions