Core Data

Preventing unwanted fetches when using NSFetchedResultsController and fetchBatchSize

Published on: January 18, 2021

This article covers a topic that is extensively covered in my Practical Core Data book. This book is intended to help you learn Core Data from scratch using modern techniques and every chapter features sample apps in SwiftUI as well as UIKit whenever this is relevant. When you use Core Data in a UIKit or SwiftUI app, the easiest way to do this is through a fetched results controller. In SwiftUI, fetched results controller is best used through the @FetchRequest property wrapper. In UIKit, a fetched results controller can be conveniently set up to provide diffable data source snapshots for...

Read more...

Observing the result of saving a background managed object context with Combine

Published on: December 7, 2020

I love posts where I get to put write about two of my favorite frameworks at the moment; Combine and Core Data. When you're working with Core Data, it's common to perform save operations asynchronously using a background context. You could even perform an asynchronous save on the main managed object context. Consider the following method that I added to an object that I wrote called StorageProvider: public extension StorageProvider { func addTask(name: String, description: String, nextDueDate: Date, frequency: Int, frequencyType: HouseHoldTask.FrequencyType) { persistentContainer.performBackgroundTask { context in let task = HouseHoldTask(context: context) task.name = name task.taskDescription = description task.nextDueDate =...

Read more...

Responding to changes in a managed object context

Published on: November 23, 2020

Working with multiple managed object contexts will often involve responding to changes that were made in one context to update another context. You might not even want to update another context but reload your UI or perform some other kind of update. Maybe you want to do this when a specific context updates, or maybe you want to run some code when any context updates. In this week's post I will show you how you can listen for changed in managed object contexts, and how you can best use them. I will also show you a convenient way to extract...

Read more...

Observing changes to managed objects across contexts with Combine

Published on: October 12, 2020

A common pattern in Core Data is to fetch objects and show them in your UI using one managed object context, and then use another context to update, insert or delete managed objects. There are several ways for you to update your UI in response to these changes, for example by using an NSFetchedResultsController. I wrote about doing this in a SwiftUI app in an earlier post. In this week's post I will show you a useful technique that allows you to observe specific managed objects across different contexts so you can easily update your UI when, for example, that...

Read more...

Understanding the differences between your Core Data model and managed objects

Published on: October 5, 2020

You may have noticed that when Xcode generates your NSManagedObject classes based on your Core Data model file, most of your managed object's properties are optional. Even if you've made them required in the model editor, Xcode will generate a managed object where most properties are optional. In this article we'll explore this phenomenon, and why it happens. Exploring generated NSManagedObject subclasses When you build a project that uses Xcode's automatic code generation for Core Data models, your NSManagedObject subclasses are generated when you build your project. These classes are written your project's Derived Data folder and you shouldn't modify...

Read more...

Implementing a one-way sync strategy with Core Data, URLSession and Combine

Published on: August 24, 2020

A common use of a Core Data store is to cache data from a remote resource locally to support offline functionality of an app. There are multiple ways to implement a caching mechanism and many of them don't involve Core Data. For example, you could simply fetch a JSON file from a server and store the contents of that JSON file on disk. A downside of fetching a full data set every time is that you risk using a lot of bandwidth, especially if your data set is large, or if your data set is expected to grow over time....

Read more...

Fetching objects from Core Data in a SwiftUI project

Published on: August 10, 2020

When you've added Core Data to your SwiftUI project and you have some data stored in your database, the next hurdle is to somehow fetch that data from your Core Data store and present it to the user. In this week's post, I will present two different ways that you can use to retrieve data from Core Data and present it in your SwiftUI application. By the end of this post you will be able to: Fetch data using the @FetchRequest property wrapper Expose data to your SwiftUI views with an observable object and an @Published property. Since @FetchRequest is...

Read more...

Using Codable with Core Data and NSManagedObject

Published on: August 3, 2020

If you've ever wanted to decode a bunch of JSON data into NSManagedObject instances you've probably noticed that this isn't a straightforward exercise. With plain structs, you can conform your struct to Codable and you convert the struct from and to JSON data automatically. For an NSManagedObject subclass it's not that easy. If your Core Data data model is configured to automatically generate your entity class definitions for you (which is the default), you may have tried to write the following code to conform your managed object to Decodable: extension MyManagedObject: Decodable { } If you do this, the compiler...

Read more...

Setting up a Core Data store for unit tests

Published on: July 27, 2020

Unit testing is an important skill in the toolbox of any engineer. Knowing how to write a reliable, robust test suite helps you write reliable and robust code. If you've followed my introduction to unit testing part one and part two, or if you're experienced with unit testing you know that your tests should run isolated from any other tests. You also know that you should make sure that your test relies on as few external dependencies as possible. When you want to test your Core Data code, it might not be immediately obvious how you can test your Core...

Read more...

Using Core Data with SwiftUI 2.0 and Xcode 12

Published on: July 20, 2020

In Xcode 12 you can create projects that no longer use an AppDelegate and SceneDelegate to manage the application lifecycle. Instead, we can use Swift's new @main annotation to turn a struct that conforms to the App protocol into the main entry point for our applications. When you create a new project in Xcode 12, you have the option to use the SwiftUI App application lifecycle for your SwiftUI project. While Xcode 12 beta 5 introduces an option to include Core Data in your SwiftUI application when you create a new project, you might have an existing SwiftUI project that...

Read more...