Sample program to demonstrate how CoreData can be used with SwiftUI.
Update: July 10, 2020 for Xcode 12
I know I said that I wouldn’t enhance this project any further, but after hearing no real new news along these lines from Apple at WWDC 2020, I felt I had to tidy up a few loose ends and make a few additional enhancements for myself. After spending more than a year learning the philosophy of SwiftUI and rewriting my own App several times to get it to work the way I wanted, I restructured and reorganized the CoreDataDataSource to make it easier to modify the execution of the fetch requests based on selected @State vars changing.
First, all of the various init() methods were combined into a single all encompasing init() method that is used to construct an initial CoreDataDataSource object. Second, the actual Data methods and properties were reorganized to be more consistent with the various usage scenarios, supporting a single array, multiple arrays when using a sectionNameKeyPath, and without a Fetched Results Controller. Third, taking a page from SwiftUI View’s and View Modifiers, Fetch Request Modifier methods were added to allow for easier modifying of the Fetch Requests with current @State vars just prior to performing the actual fetch.
There is also a ‘count’ Property and ‘fetch()’ Method that work without a Fetched Results Controller. They will use the same Fetch Request as would be used by the List methods and properties.
I have now included stripped down examples of the most common usage patterns is the CodeExamples.swift file.
=======================
This sample program demonstrates how CoreData can be used with SwiftUI. The goal of this program is to try to duplicate or reinvent some familiar UIKit patterns using SwiftUI that are common with some Core Data based iOS Apps.
This project currently compiles and runs on Xcode Version 12.0 beta 2 (12A6163b)
Most of the current development testing targets have been actual iPhone devices. It does run on iPad and macOS with a few size class issues (currently not a high priority for me).
The key component of this sample program is the class CoreDataDataSource which encapsulates much of the functionality of an NSFetchedResultsController in a manner to be useful with SwiftUI. This version of CoreDataDataSource has several custom initializers for different usage scenarios.
=======================
The App uses a Tab View that displays and edits the same CoreData database several ways.
Tab 1:
Tab 2:
Tab 3:
Tab 4:
Tab 5:
=======================
CREDITS:
=======================
KNOWN ISSUE: