Demo/test of easier to maintain Core Data processing in SwiftUI applications by abstracting Core Data processing away from the UI and into its own App Model while maintaining an ease of use similar to the built in @FetchRequest
TL;DR; Demo/test project showcasing easier to maintain Core Data processing in SwiftUI applications by abstracting Core Data processing away from the UI and into its own App Model.
Why
Good SW engineering practice seeks to achieve a Separation of Concerns. As of 10th of August 2020, the 'normal' SwiftUI approach to integration of Core Data involves data operations (using @FetchRequest
and Core Data's Managed Object Context) within the immediate hierarchy of the View
components that consume and create that same data.
As convenient as it is to work with initially (particularly @FetchRequest
) - splattering Core Data and associated processing throughout an application's UI components disobeys the Separation of Concerns and is liable to result in apps that are difficult maintain.
What's demonstrated
Alongside the conventional @FetchRequest
/MOC approach for comparison, this project demonstrates how Core Data can be handled within an app's model, thereby separating data and UI concerns, while still maintaining ease of use.
Combine
publisher for the Core Data results.@FetchRequest
@FetchRequest
Core Data integration for comparison.SwiftUI App
Life Cycle
template I.e. without an AppDelegate.swift
and SceneDelegate.swift
to setup the Core Data persistentContainer
, but with a model and the the usage of @StateObject
to do the same.@StateObject
, @Environment
, @EnvironmentObject
, @ObservedObject
and @FetchRequest
property wrappers.This project requires at least iOS14 and Xcode 12 (I used Xcode 12.0 beta 4)
Should run on any iOS 14+ simulator or device.
When it runs, the app will display summary data about the number of Item
entities it has in the system in various View components.
Components are named after how they get their data, I.e. either from:
@FetchRequest
and MOC approachWhen normal and flagged
Items are Added, the data across all of the View components should remain the same.
Combine
publisher class implementation.