Friday, February 2, 2024
HomeiOS Developmentios - Creating momentary SwiftData object

ios – Creating momentary SwiftData object


I’ve a Exercise SwiftData mannequin and an inventory view of exercises. The consumer can create a brand new exercise by clicking on a plus button within the toolbar of the checklist 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 need the consumer to have the ability to create a brand new exercise and click on ‘Carried out’ to put it aside (which will likely be mirrored within the checklist view) or just click on the shut button and discard all the pieces. My query is, how can I do that with the SwiftData context? I’ve tried accessing the context in the identical method as above within the sheet view, however the modifications don’t get mirrored (even after 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 item 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 setting 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 father or mother view and handed to the kid view, however I want to create the item ONlY if the consumer presses the ‘Carried out’ button within the baby 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 father or mother view utilizing a name again methodology. Nonetheless, 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 after I use the method 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 just isn’t a Mannequin object that may be made @Bindable.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments