Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Core Data with SwiftUI

I'm making progress learning SwiftUI but I'm getting stuck when it comes to using it with Core Data.

  • Model Objects - I made a couple of entities with the required properties and relationships
  • ??? I have no idea where to begin. How to I get SwiftUI talking to Core Data?
  • SwiftUI - I have some basic layouts working and showing data from some temp classes, but these have no awareness of 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?

Update:

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.

  • List Views:
    • UITableViewController (with a FetchedResultsController)
    • Manage the connection to Core Data store
    • Use dependency injection to pass records to a detail view
  • Detail / Edit Views:
    • When possible build these with UIHostingController
    • Use SwiftUI Forms to make simple data entry forms
    • Receive data via dependency injection
    • Copy data to a temporary BindableObject type
      • Make local UI chances to the temp record
      • Save the temp data back to the actual record on exit

Update 2 - 2019.08.14

Xcode beta 5 has some new Core Data features. I used the information in this blog post to get most of what I need.

like image 384
radicalappdev Avatar asked Jun 18 '19 14:06

radicalappdev


People also ask

When should I use Core Data vs Userdefaults?

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.

What is Core Data in Swift iOS?

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.

Why is SwiftUI so fast?

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.


1 Answers

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.

like image 193
Mecid Avatar answered Oct 19 '22 21:10

Mecid