Embracing Core Data in SwiftUI

Mohammad Azam
10 min readOct 15, 2022
Photo by Patrick Fore on Unsplash

Last year I was working on an app which was using the Core Data framework as a persistent medium to the SQLite store. I was reluctant to use any SwiftUI property wrappers for Core Data in my app, because I wanted to structure the app in several layers and those property wrappers were only available inside the View. The app worked but it was a pain to make sure that everything in Core Data was synced with SwiftUI views.

SwiftUI team has provided us with APIs to make sure that SwiftUI and Core Data works seamlessly together. In this post, we will be building a small budget app using SwiftUI and Core Data. We will start by discussing our original approach of implementing the app, where we did not use any helpers provided by the SwiftUI frame. Later, we will look into a much simpler implementation, which uses SwiftUI Core Data property wrappers.

The complete app is part of my course MV Design Pattern in iOS — Build SwiftUI Apps Apple’s Way

OLD WAY — Manually Calling Fetch or Implementing NSFetchedResultsController

I started with creating view models for each of my view. This included BudgetCategoryListViewModel, AddNewBudgetViewModel etc. Each view model that interacted with Core Data was passed CoreDataManager as a dependency. CoreDataManager contains all the code…

--

--

Mohammad Azam
Mohammad Azam

Written by Mohammad Azam

Lead instructor at a coding bootcamp. Top iOS mobile instructor on Udemy. Author of multiple books and international speaker. azamsharp.school

Responses (1)