NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

Introduction to using Core Data with SwiftUI

Paul Hudson    @twostraws   

Updated for Xcode 12.0

As two massive pieces of Apple’s software platform, it won’t surprise you to learn that Core Data and SwiftUI have been written to work well together: we get property wrappers, environment support, and more, all to make sure we can integrate Core Data into our SwiftUI apps with the least hassle.

Before SwiftUI it was common to find a range of ways you might find Core Data being used from an architectural perspective – Apple strongly encouraged us to create containers at the AppDelegate level then reach back up as needed, others preferred using manager classes, and some preferred abstracting Core Data away entirely so they had the freedom to move to Realm or other options at a later date.

However, SwiftUI’s integration with Core Data is different because it points very strongly in one direction: create the Core Data container once when the app starts, inject its managed object context into the environment, then perform fetch requests directly on there.

This isn’t me guessing – Apple literally designed it in a highly specific way, and if you want to take advantage of all the features of SwiftUI’s Core Data integration then you ought to follow the path Apple is laying down for us.

Here are the four specific features that will help you see what I mean:

  1. NSManagedObject conforms to the ObservableObject protocol, which means we can bind any object to part of our user interface.
  2. There’s a managedObjectContext key in the environment designed to store our active Core Data managed object context.
  3. Xcode’s template then injects that context into the initial content view inside the scene delegate.
  4. There’s a @FetchRequest property wrapper that uses the environment’s managed object context to perform fetch requests.

So, we create a managed object context when the app launches, attach it to the environment for our views, then use @FetchRequest to load data for the app to use.

Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.9/5

Link copied to your pasteboard.