UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Core Data: Introduction

Paul Hudson    @twostraws   

This technique project is going to explore Core Data in more detail, starting with a summary of some basic techniques then building up to tackling some more complex problems.

When you’re working with Core Data, please try to keep in mind that it has been around for a long time – it was designed way before Swift existed, never mind SwiftUI, so occasionally you’ll meet parts that don’t work quite as well in Swift as we might hope. Hopefully we’ll see this improve over the years ahead, but in the meantime be patient!

We have lots of Core Data to explore, so please create a fresh project where we can try it out. Call it “CoreDataProject” and not just “CoreData” because that will cause Xcode to get confused.

Make sure you do not check the “Use Core Data” box. Instead, please copy DataController.swift from your Bookworm project, make a new, empty Data Model called CoreDataProject.xcdatamodeld, then update CoreDataProjectApp.swift to create a DataController instance and inject it into the SwiftUI environment. This will leave you where we started in the early stages of the Bookworm project.

Important: As you’re copying DataController from Bookworm to this new project, the data model file has changed – make sure you change the NSPersistentContainer initializer to refer to the new data model file rather than Bookworm.

As you progress, you might sometimes find that Xcode will ignore changes made in the Core Data editor, so I like to drive the point home by pressing Cmd+S before going to another file. Failing that, restart Xcode!

All set? Let’s go!

Tip: Sometimes you’ll see a heading titled “Want to go further?” This contains some bonus examples that help take your knowledge further, but you don’t need to follow here unless you want to – think of it as extra credit.

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI 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 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 Beyond Code

Was this page useful? Let us know!

Average rating: 4.5/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.