currently I'm looking into using Core Data within my SwiftUI project and I'm facing some architecture questions.
Since touching some SwiftUI projects, I learned quite a few things that where managed using the @State
and @Binding
principle where it wasn't an issue to pass a parent state to a child view. Now by incorporating Core Data into my project I wonder how the appropriate way would look like to access data in child views, I guess passing an NSManagedObject
as a @Binding
is not the way it should be done...
So, my current situation looks like this:
ContentView
that loads those NSManagedObjects
and displays it as a list with NavigationLink
views
- by clicking on a list row item, the user gets navigated to the corresponding child view showing those list item in detail
- within this child view the selected list item should be displayed with it's information that should be able to alter
I think the following intention is fitting better, but there also some open points I'm thinking about. Within the ContentView
all NSManagedObject
relevant data gets loaded and displayed as list. By clicking on an list item, the child view appears and the only thing I pass is the specific id of that list item. Within the child view by using this id I'll load the data and display it, ready to alter. Maybe incorporating MVVM at this stage also makes sense.
What do you think about that solution, is that to intended way for those situations?
Another question I have, when using MVVM, how to decide what to put within a single ViewModel
? Is it a good starting point to put every use case of the app into it's own ViewModel
?
Many thanks in advice!
question from:
https://stackoverflow.com/questions/65540624/swiftui-and-core-data-workflow 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…