11.7 C
Wednesday, January 31, 2024

ios – Creating momentary SwiftData object

I’ve a Exercise SwiftData mannequin and an inventory view of exercises. The person can create a brand new exercise by clicking on a plus button within the toolbar of the listing view, which is inside a NavigationStack. The WorkoutsListView makes use of the mannequin context as such:
@Setting(.modelContext) var modelContext.

After I open the NewWorkoutView as a sheet, I would like the person to have the ability to create a brand new exercise and click on ‘Performed’ to reserve it (which will probably be mirrored within the listing view) or just click on the shut button and discard every thing. My query is, how can I do that with the SwiftData context? I’ve tried accessing the context in the identical manner as above within the sheet view, however the adjustments don’t get mirrored (even once I manually save utilizing modelContext.save(). I’ve additionally tried passing within the container within the initialiser of the brand new view to create a context with this container and add the thing to it; this additionally doesn’t work. I want to add that I’ve abstracted the context logic to the view fashions, i.e. I’ve a variable like this in my opinion fashions: var modelContext: ModelContext? = nil and I set it to the surroundings mannequin context on view seem as follows: viewModel.modelContext = modelContext.

I’ve seen some approaches the place the brand new object is created within the mother or father view and handed to the kid view, however I want to create the thing ONlY if the person presses the ‘Performed’ button within the little one view, not beforehand. I’ve additionally tried to only create a variable within the view mannequin of the kid view and easily cross it again to the mother or father view utilizing a name again technique. Nevertheless, this doesn’t work as I’m utilizing the brand new @Bindable key phrase in a lot of my customized subviews to bind to properties of the fashions. So once I use the strategy I simply talked about, I get errors stating that the schema couldn’t be discovered, which I assume is to do with the truth that the variable on this case is just not a Mannequin object that may be made @Bindable.

Latest news
Related news


Please enter your comment!
Please enter your name here