I'm making progress learning SwiftUI but I'm getting stuck when it comes to using it with Core Data.
I've spend the last few days searching and reading everything I can on this topic but I have no idea how to begin to solve this.
Can anyone outline the basic steps to using Core Data with SwiftUI?
I got some additional feedback on Twitter and one dev suggested that SwiftUI lists may not be suitable for working with Core Data with large data sets. Read the post here: https://twitter.com/numist/status/1141012785825845248
Here is the approach I'm going to take in my app.
Xcode beta 5 has some new Core Data features. I used the information in this blog post to get most of what I need.
Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.
Core Data is a graphical and persistence framework, which is used in Apple devices with operating systems macOS and iOS. Core Data was first introduced in Mac OS X 10.4 Tiger and iOS with iPhone SDK 3.0.
Even though SwiftUI relies on UIKit and AppKit, it takes a very different approach to building user interfaces. Its declarative syntax simplifies user interface development and its integration with Xcode makes building and debugging user interfaces easier and faster than ever before.
You can write a wrapper for your Core-Data logic, like EntityManager and then use it inside BindableObject
class PersonStore: BindableObject {
var didChange = PassthroughSubject<Void, Never>()
var persons: [PersonEntity] = [] {
didSet {
didChange.send(())
}
}
func fetchPersons(matching name: String) {
// make you core data query here and assign it to persons property
}
}
As soon as you query finish and value assigned to persons property SwiftUI will reload View to read new values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With