Core Data Architecture with SwiftUI

In Xcode 12 the core data setup looks something along these lines https://developer.apple.com/forums/thread/650173, passing the context to the environment and then using the @FetchRequest property wrapper in your views, my questions about this are:

  1. What's the best approach if you need to use a dynamic predicate with your @FetchRequest, should I use @FetchRequest at all?

  2. What's the best approach if you use core data in a view model?

  • Should the core data stack just be a shared instance that can be called from anywhere, including the view model (guessing probably not)?

  • Should I try to pass the context taking it from the environment in the view and passing it to the model somehow?

  • Should I have one model with the context that is added to the environment from the App Struct and not pass the view context on it's own (Probably not a great idea as it would need to handle everything in one file)?

I'd love to see some sample code around this of some best practices or some advice about the approach?

Also loving the new WWDC format this year and everything that has been released 😀


Replies

Link is broken above, it should be https://developer.apple.com/forums/thread/650173