Integrate Combine Into an App

Aug 5 2021 · Swift 5.4, macOS 11.3, Xcode 12.5

Part 2: Integrate with Core Data & Unit Test

07. Create the Core Data Stack

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 06. Extend, Save & Delete Core Data Objects Next episode: 08. Use @FetchRequest to Get Core Data Entries

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Before we actually try to fetch data from our Core Data database, we need to somehow get a reference to the Core Data Stack, specifically the NSManageObjectContext. This will allow us, as we saw in the last episode, to save and delete content from the database. Know that this isn't using combined per se, but instead allows us to use combine in the next episode. As a quick reminder, a context in this case, it's like a scratch space for our managed objects. Nothing happens to the underlying database until we call save on the context. Contexts can make objects, fetch them from the database and deal with other features of the ManageObject life cycle, such as faulting and validation. In our case, we're going to create our contexts in such a fashion, that only the main app class has access to it. This will be done by using a private enum which effectively makes a namespace for us we won't ever use the enum elsewhere in the code. Once we have that CoreDataStack, we can get the viewContext and send it to the SwiftUI Environment so that our views can use it where needed. This saves us from having to pass it around through initializers. Let's take a look at how to set up the CoreDataStack in the code. At the top of the ChuckNorrisJokesApp.swift file in the app folder, add an import for CoreData. Then define the CoreDataStack at the bottom of the file. Remember that using an enum here only defines a namespace you don't want to instantiate it. Let's look at this code. First, the enum has a static property called viewContext. This is how we'll initially get a hold of the generated viewContext so we can pass it to the SwiftUI Environment. There's also a static method called save, that will check to see if the viewContext has changes, and if so, we'll ask the viewContext to save. Before we move on, we need to make sure the viewContext is in the SwiftUI Environment. We can do that by injecting there with the environment modifier on JokeView. We also wanna make sure that when the app goes to the background, the context gets saved. We can do this by way of the onChange modifier, and look to see when the scene phase changes which we can get from the environment. First add a scene change property at the top of the struct. Now add the onChange modifier to monitor for the state change. Okay, now with access to the CoreDataStack, let's look at saving the leading and fetching from CoreData, which we'll do in the next episode.