I spent my morning grappling emotionally with what Apple was telling me to do. The afternoon was, “OK, fine, we’ll try it your way.” The result was… pasta. Yes, pasta is definitely the analogy that I’m thinking of. Now, in the evening, I’ve come full circle and I’m going to do it exactly the way I wanted to in the first place. Whew - huge relief. But still wondering… (voiceStyle: .ultracalm) why are they saying to put data operations in the View?
OK, I think I can answer my own question now. It’s so that inexperienced (or in my case out-of-practice) programmers don’t have to think about what actions are accessing the ModelContext. That still makes me cringe, but I guess I can see where they’re coming from. Apple doesn’t exactly prefer a Tough Love approach to new Swifties (Swifters?, Swiftos?), and most of the time I’m grateful for that.
Thanks for this example. I'm trying - and failing - to extend it to work with multiple models and datasources. For instance, Users accessible from a UserDataSource and UserGroups accessible from a UserGroupDataSource. Help appreciated.
I spent my morning grappling emotionally with what Apple was telling me to do. The afternoon was, “OK, fine, we’ll try it your way.” The result was… pasta. Yes, pasta is definitely the analogy that I’m thinking of. Now, in the evening, I’ve come full circle and I’m going to do it exactly the way I wanted to in the first place. Whew - huge relief. But still wondering… (voiceStyle: .ultracalm) why are they saying to put data operations in the View?
Haha because nobody at Apple has built a large complex SwiftUI app
OK, I think I can answer my own question now. It’s so that inexperienced (or in my case out-of-practice) programmers don’t have to think about what actions are accessing the ModelContext. That still makes me cringe, but I guess I can see where they’re coming from. Apple doesn’t exactly prefer a Tough Love approach to new Swifties (Swifters?, Swiftos?), and most of the time I’m grateful for that.
Neither have I! I have spent a lot of time working with data, though.
Thanks for this example. I'm trying - and failing - to extend it to work with multiple models and datasources. For instance, Users accessible from a UserDataSource and UserGroups accessible from a UserGroupDataSource. Help appreciated.
I ran into the same problem and found this post https://developer.apple.com/forums/thread/759634
I've updated the initializer to take a ModelContainer and add all needed Models to it.
```Swift
init(container: ModelContainer) {
self.container = container
}
```
```Swift
let schema = Schema([Model1.self, Model2.self])
let modelConfiguration = ModelConfiguration(schema: schema)
let container = try ModelContainer(for: Model1.self, Model2.self, configurations: modelConfiguration)
self.model1DB = Model1Database(container: container)
self.model2DB = Model2Database(container: container)
```