From 252ef573bc3204890249e12acd872ee177e0e1ac Mon Sep 17 00:00:00 2001 From: Zev Eisenberg Date: Sun, 4 Aug 2019 18:02:17 -0400 Subject: [PATCH 01/11] Add Core Data model. --- CoreDataDemo.xcodeproj/project.pbxproj | 21 +++++++ CoreDataDemo/AppDelegate.swift | 25 +------- CoreDataDemo/DataStore.swift | 61 +++++++++++++++++++ .../Demo.xcdatamodel/contents | 16 +++++ 4 files changed, 101 insertions(+), 22 deletions(-) create mode 100644 CoreDataDemo/DataStore.swift create mode 100644 CoreDataDemo/Demo.xcdatamodeld/Demo.xcdatamodel/contents diff --git a/CoreDataDemo.xcodeproj/project.pbxproj b/CoreDataDemo.xcodeproj/project.pbxproj index a4f48d2..f4a3149 100644 --- a/CoreDataDemo.xcodeproj/project.pbxproj +++ b/CoreDataDemo.xcodeproj/project.pbxproj @@ -15,6 +15,8 @@ 6D08495D22F77E4E009B09A2 /* AddSpeakerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D08495C22F77E4E009B09A2 /* AddSpeakerViewController.swift */; }; 6D08495F22F77E58009B09A2 /* AddSessionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D08495E22F77E58009B09A2 /* AddSessionViewController.swift */; }; 6D08496122F77EA9009B09A2 /* SessionTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D08496022F77EA9009B09A2 /* SessionTableViewCell.swift */; }; + 7A02983322F788FB00BC0AAC /* Demo.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 7A02983122F788FB00BC0AAC /* Demo.xcdatamodeld */; }; + 7A02985A22F78B9C00BC0AAC /* DataStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A02985922F78B9C00BC0AAC /* DataStore.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -28,6 +30,8 @@ 6D08495C22F77E4E009B09A2 /* AddSpeakerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddSpeakerViewController.swift; sourceTree = ""; }; 6D08495E22F77E58009B09A2 /* AddSessionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddSessionViewController.swift; sourceTree = ""; }; 6D08496022F77EA9009B09A2 /* SessionTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SessionTableViewCell.swift; sourceTree = ""; }; + 7A02983222F788FB00BC0AAC /* Demo.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = Demo.xcdatamodel; sourceTree = ""; }; + 7A02985922F78B9C00BC0AAC /* DataStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataStore.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -60,7 +64,9 @@ 6D08494522F777D2009B09A2 /* CoreDataDemo */ = { isa = PBXGroup; children = ( + 7A02983122F788FB00BC0AAC /* Demo.xcdatamodeld */, 6D08494622F777D2009B09A2 /* AppDelegate.swift */, + 7A02985922F78B9C00BC0AAC /* DataStore.swift */, 6D08494C22F777D3009B09A2 /* Main.storyboard */, 6D08494F22F777D3009B09A2 /* Assets.xcassets */, 6D08495122F777D3009B09A2 /* LaunchScreen.storyboard */, @@ -144,6 +150,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 7A02985A22F78B9C00BC0AAC /* DataStore.swift in Sources */, + 7A02983322F788FB00BC0AAC /* Demo.xcdatamodeld in Sources */, 6D08494722F777D2009B09A2 /* AppDelegate.swift in Sources */, 6D08495F22F77E58009B09A2 /* AddSessionViewController.swift in Sources */, 6D08496122F77EA9009B09A2 /* SessionTableViewCell.swift in Sources */, @@ -346,6 +354,19 @@ defaultConfigurationName = Release; }; /* End XCConfigurationList section */ + +/* Begin XCVersionGroup section */ + 7A02983122F788FB00BC0AAC /* Demo.xcdatamodeld */ = { + isa = XCVersionGroup; + children = ( + 7A02983222F788FB00BC0AAC /* Demo.xcdatamodel */, + ); + currentVersion = 7A02983222F788FB00BC0AAC /* Demo.xcdatamodel */; + path = Demo.xcdatamodeld; + sourceTree = ""; + versionGroupType = wrapper.xcdatamodel; + }; +/* End XCVersionGroup section */ }; rootObject = 6D08493B22F777D2009B09A2 /* Project object */; } diff --git a/CoreDataDemo/AppDelegate.swift b/CoreDataDemo/AppDelegate.swift index 8aadaa3..f043262 100644 --- a/CoreDataDemo/AppDelegate.swift +++ b/CoreDataDemo/AppDelegate.swift @@ -7,40 +7,21 @@ // import UIKit +import CoreData @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true } - func applicationWillResignActive(_ application: UIApplication) { - // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. - // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. - } - - func applicationDidEnterBackground(_ application: UIApplication) { - // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. - // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. - } - - func applicationWillEnterForeground(_ application: UIApplication) { - // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. - } - - func applicationDidBecomeActive(_ application: UIApplication) { - // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. - } - func applicationWillTerminate(_ application: UIApplication) { - // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. + // Saves changes in the application's managed object context before the application terminates. + DataStore.shared.saveContext() } - } - diff --git a/CoreDataDemo/DataStore.swift b/CoreDataDemo/DataStore.swift new file mode 100644 index 0000000..27d2cf5 --- /dev/null +++ b/CoreDataDemo/DataStore.swift @@ -0,0 +1,61 @@ +// +// DataStore.swift +// CoreDataDemo +// +// Created by Zev Eisenberg on 8/4/19. +// Copyright © 2019 Matthew Dias. All rights reserved. +// + +import CoreData + +class DataStore { + + static var shared = DataStore() + + var managedObjectContext: NSManagedObjectContext { + return persistentContainer.viewContext + } + + private init() { } + + private lazy var persistentContainer: NSPersistentContainer = { + /* + The persistent container for the application. This implementation + creates and returns a container, having loaded the store for the + application to it. + */ + let container = NSPersistentContainer(name: "Demo") + container.loadPersistentStores(completionHandler: { (storeDescription, error) in + if let error = error as NSError? { + // Replace this implementation with code to handle the error appropriately. + // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. + + /* + Typical reasons for an error here include: + * The parent directory does not exist, cannot be created, or disallows writing. + * The persistent store is not accessible, due to permissions or data protection when the device is locked. + * The device is out of space. + * The store could not be migrated to the current model version. + Check the error message to determine what the actual problem was. + */ + fatalError("Unresolved error \(error), \(error.userInfo)") + } + }) + return container + }() + + func saveContext () { + let context = persistentContainer.viewContext + if context.hasChanges { + do { + try context.save() + } catch { + // Replace this implementation with code to handle the error appropriately. + // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. + let nserror = error as NSError + fatalError("Unresolved error \(nserror), \(nserror.userInfo)") + } + } + } + +} diff --git a/CoreDataDemo/Demo.xcdatamodeld/Demo.xcdatamodel/contents b/CoreDataDemo/Demo.xcdatamodeld/Demo.xcdatamodel/contents new file mode 100644 index 0000000..937a264 --- /dev/null +++ b/CoreDataDemo/Demo.xcdatamodeld/Demo.xcdatamodel/contents @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file From 14bfc36341c598615d23b81a320065f1025840f2 Mon Sep 17 00:00:00 2001 From: Zev Eisenberg Date: Sun, 4 Aug 2019 18:20:18 -0400 Subject: [PATCH 02/11] Save on entering background. --- CoreDataDemo/AppDelegate.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CoreDataDemo/AppDelegate.swift b/CoreDataDemo/AppDelegate.swift index f043262..c4aaf11 100644 --- a/CoreDataDemo/AppDelegate.swift +++ b/CoreDataDemo/AppDelegate.swift @@ -19,6 +19,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate { return true } + func applicationDidEnterBackground(_ application: UIApplication) { + // Saves changes in the application's managed object context when the app enters the background. + DataStore.shared.saveContext() + } + func applicationWillTerminate(_ application: UIApplication) { // Saves changes in the application's managed object context before the application terminates. DataStore.shared.saveContext() From fe8f64c5eaac649cc8e7d9f1190ddd65b42d88b2 Mon Sep 17 00:00:00 2001 From: Zev Eisenberg Date: Sun, 4 Aug 2019 18:24:02 -0400 Subject: [PATCH 03/11] Insert speaker into store. --- CoreDataDemo/AddSpeakerViewController.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CoreDataDemo/AddSpeakerViewController.swift b/CoreDataDemo/AddSpeakerViewController.swift index 891d82f..dc97f5d 100644 --- a/CoreDataDemo/AddSpeakerViewController.swift +++ b/CoreDataDemo/AddSpeakerViewController.swift @@ -29,7 +29,10 @@ class AddSpeakerViewController: UIViewController { present(alert, animated: true, completion: nil) return } - // TODO: add name to Core Data + + let moc = DataStore.shared.managedObjectContext + let speaker = Speaker(context: moc) + speaker.name = name dismiss(animated: true, completion: nil) } } From 202bab2513da783e107582ac6148161513e94322 Mon Sep 17 00:00:00 2001 From: Zev Eisenberg Date: Sun, 4 Aug 2019 18:34:27 -0400 Subject: [PATCH 04/11] Populate speaker picker on Add Session screen. --- CoreDataDemo/AddSessionViewController.swift | 31 +++++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/CoreDataDemo/AddSessionViewController.swift b/CoreDataDemo/AddSessionViewController.swift index fd752bb..6d8d661 100644 --- a/CoreDataDemo/AddSessionViewController.swift +++ b/CoreDataDemo/AddSessionViewController.swift @@ -7,6 +7,7 @@ // import UIKit +import CoreData class AddSessionViewController: UIViewController { @@ -22,6 +23,31 @@ class AddSessionViewController: UIViewController { formatter.dateStyle = .long return formatter }() + + private lazy var fetchedResultsController: NSFetchedResultsController = { + let fetchRequest: NSFetchRequest = Speaker.fetchRequest() + + let sortDescriptor = NSSortDescriptor(keyPath: \Speaker.name, ascending: true) + + fetchRequest.sortDescriptors = [sortDescriptor] + + let fetchedResultsController = NSFetchedResultsController( + fetchRequest: fetchRequest, + managedObjectContext: DataStore.shared.managedObjectContext, + sectionNameKeyPath: nil, // not using sections + cacheName: nil // no need for caching + ) + + do { + try fetchedResultsController.performFetch() + } catch { + // Replace this implementation with code to handle the error appropriately. + // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. + let nserror = error as NSError + fatalError("Error fetching speakers: \(nserror), \(nserror.userInfo)") + } + return fetchedResultsController + }() override func viewDidLoad() { super.viewDidLoad() @@ -56,8 +82,7 @@ extension AddSessionViewController: UIPickerViewDataSource { } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { - // TODO: populate from Core Data - return 1 + return fetchedResultsController.fetchedObjects?.count ?? 0 } } @@ -65,7 +90,7 @@ extension AddSessionViewController: UIPickerViewDataSource { extension AddSessionViewController: UIPickerViewDelegate { func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { - return "TODO: speakers from Core Data" + return fetchedResultsController.fetchedObjects?[row].name } } From d6aadc248fe0fefddf011b9bdad43b78465a6069 Mon Sep 17 00:00:00 2001 From: Zev Eisenberg Date: Sun, 4 Aug 2019 18:35:35 -0400 Subject: [PATCH 05/11] Allow picking a speaker. --- CoreDataDemo/AddSessionViewController.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CoreDataDemo/AddSessionViewController.swift b/CoreDataDemo/AddSessionViewController.swift index 6d8d661..6e33879 100644 --- a/CoreDataDemo/AddSessionViewController.swift +++ b/CoreDataDemo/AddSessionViewController.swift @@ -92,5 +92,9 @@ extension AddSessionViewController: UIPickerViewDelegate { func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return fetchedResultsController.fetchedObjects?[row].name } + + func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { + speakerField.text = fetchedResultsController.fetchedObjects?[row].name + } } From 7d3c3b5d136b1315cd9a5320c0cd99d85f82c33c Mon Sep 17 00:00:00 2001 From: Zev Eisenberg Date: Sun, 4 Aug 2019 18:38:19 -0400 Subject: [PATCH 06/11] Track selected speaker. --- CoreDataDemo/AddSessionViewController.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/CoreDataDemo/AddSessionViewController.swift b/CoreDataDemo/AddSessionViewController.swift index 6e33879..91e81fa 100644 --- a/CoreDataDemo/AddSessionViewController.swift +++ b/CoreDataDemo/AddSessionViewController.swift @@ -24,6 +24,8 @@ class AddSessionViewController: UIViewController { return formatter }() + private var selectedSpeakerIndex: Int? + private lazy var fetchedResultsController: NSFetchedResultsController = { let fetchRequest: NSFetchRequest = Speaker.fetchRequest() @@ -64,8 +66,9 @@ class AddSessionViewController: UIViewController { } @IBAction func addTapped(_ sender: Any) { - guard let title = titleField.text, !title.isEmpty else { - let alert = UIAlertController(title: "Title can’t be empty", message: nil, preferredStyle: .alert) + guard let title = titleField.text, !title.isEmpty, + let speakerIndex = selectedSpeakerIndex else { + let alert = UIAlertController(title: "All Fields Required", message: nil, preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) present(alert, animated: true, completion: nil) return @@ -94,6 +97,7 @@ extension AddSessionViewController: UIPickerViewDelegate { } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { + selectedSpeakerIndex = row speakerField.text = fetchedResultsController.fetchedObjects?[row].name } From abae4545f598ba4875bf3235357fddaab55c941e Mon Sep 17 00:00:00 2001 From: Zev Eisenberg Date: Sun, 4 Aug 2019 18:49:29 -0400 Subject: [PATCH 07/11] Add sessions to data store. --- CoreDataDemo/AddSessionViewController.swift | 37 +++++++++++++++++---- CoreDataDemo/AddSpeakerViewController.swift | 1 + 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/CoreDataDemo/AddSessionViewController.swift b/CoreDataDemo/AddSessionViewController.swift index 91e81fa..b1e9704 100644 --- a/CoreDataDemo/AddSessionViewController.swift +++ b/CoreDataDemo/AddSessionViewController.swift @@ -25,6 +25,12 @@ class AddSessionViewController: UIViewController { }() private var selectedSpeakerIndex: Int? + private var selectedDate: Date? { + didSet { + guard let date = selectedDate else { return } + dateField.text = dateFormatter.string(from: date) + } + } private lazy var fetchedResultsController: NSFetchedResultsController = { let fetchRequest: NSFetchRequest = Speaker.fetchRequest() @@ -59,6 +65,8 @@ class AddSessionViewController: UIViewController { speakerField.inputView = speakerPicker dateField.inputView = datePicker + datePicker.addTarget(self, action: #selector(datePicked(_:)), for: .primaryActionTriggered) + datePicker.datePickerMode = .date } @IBAction func cancelTapped() { @@ -67,14 +75,31 @@ class AddSessionViewController: UIViewController { @IBAction func addTapped(_ sender: Any) { guard let title = titleField.text, !title.isEmpty, - let speakerIndex = selectedSpeakerIndex else { - let alert = UIAlertController(title: "All Fields Required", message: nil, preferredStyle: .alert) - alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) - present(alert, animated: true, completion: nil) - return + let speakerIndex = selectedSpeakerIndex, + let speaker = fetchedResultsController.fetchedObjects?[speakerIndex], + let date = selectedDate else { + let alert = UIAlertController(title: "All Fields Required", message: nil, preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) + present(alert, animated: true, completion: nil) + return } - // TODO: add session to Core Data + + let session = Session(context: DataStore.shared.managedObjectContext) + session.title = title + session.speaker = speaker + session.date = date dismiss(animated: true, completion: nil) + DataStore.shared.saveContext() + } + +} + +// Actions +extension AddSessionViewController { + + @objc + func datePicked(_ sender: UIDatePicker) { + selectedDate = sender.date } } diff --git a/CoreDataDemo/AddSpeakerViewController.swift b/CoreDataDemo/AddSpeakerViewController.swift index dc97f5d..67fafff 100644 --- a/CoreDataDemo/AddSpeakerViewController.swift +++ b/CoreDataDemo/AddSpeakerViewController.swift @@ -34,5 +34,6 @@ class AddSpeakerViewController: UIViewController { let speaker = Speaker(context: moc) speaker.name = name dismiss(animated: true, completion: nil) + DataStore.shared.saveContext() } } From eb5b1353a887b24dfcd018e9a5097f46a9c31a7c Mon Sep 17 00:00:00 2001 From: Zev Eisenberg Date: Sun, 4 Aug 2019 18:59:43 -0400 Subject: [PATCH 08/11] Populate session list. --- CoreDataDemo/SessionsViewController.swift | 40 ++++++++++++++++++----- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/CoreDataDemo/SessionsViewController.swift b/CoreDataDemo/SessionsViewController.swift index bb2afb7..3fb695e 100644 --- a/CoreDataDemo/SessionsViewController.swift +++ b/CoreDataDemo/SessionsViewController.swift @@ -7,6 +7,7 @@ // import UIKit +import CoreData class SessionsViewController: UITableViewController { @@ -21,23 +22,46 @@ class SessionsViewController: UITableViewController { } - // MARK: - Table view data source + private lazy var fetchedResultsController: NSFetchedResultsController = { + let fetchRequest: NSFetchRequest = Session.fetchRequest() - override func numberOfSections(in tableView: UITableView) -> Int { - // #warning Incomplete implementation, return the number of sections - return 0 - } + let sortDescriptor = NSSortDescriptor(keyPath: \Session.date, ascending: true) + + fetchRequest.sortDescriptors = [sortDescriptor] + + let fetchedResultsController = NSFetchedResultsController( + fetchRequest: fetchRequest, + managedObjectContext: DataStore.shared.managedObjectContext, + sectionNameKeyPath: nil, // not using sections + cacheName: nil // no need for caching + ) + + do { + try fetchedResultsController.performFetch() + } catch { + // Replace this implementation with code to handle the error appropriately. + // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. + let nserror = error as NSError + fatalError("Error fetching speakers: \(nserror), \(nserror.userInfo)") + } + return fetchedResultsController + }() + + + // MARK: - Table view data source override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - // #warning Incomplete implementation, return the number of rows - return 0 + return fetchedResultsController.fetchedObjects?.count ?? 0 } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "sessionCell", for: indexPath) as! SessionTableViewCell + let session = fetchedResultsController.fetchedObjects?[indexPath.row] - // Configure the cell... + cell.titleLabel.text = session?.title + cell.speakerLabel.text = session?.speaker?.name + cell.dateLabel.text = session?.date.map(dateFormatter.string(from:)) return cell } From d57e61c4a8fa4d684fe64844e91408b36b588ee1 Mon Sep 17 00:00:00 2001 From: Zev Eisenberg Date: Sun, 4 Aug 2019 19:05:33 -0400 Subject: [PATCH 09/11] Add byline. --- CoreDataDemo/SessionsViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CoreDataDemo/SessionsViewController.swift b/CoreDataDemo/SessionsViewController.swift index 3fb695e..972d7b6 100644 --- a/CoreDataDemo/SessionsViewController.swift +++ b/CoreDataDemo/SessionsViewController.swift @@ -60,7 +60,7 @@ class SessionsViewController: UITableViewController { let session = fetchedResultsController.fetchedObjects?[indexPath.row] cell.titleLabel.text = session?.title - cell.speakerLabel.text = session?.speaker?.name + cell.speakerLabel.text = session?.speaker?.name.map { "by \($0)" } cell.dateLabel.text = session?.date.map(dateFormatter.string(from:)) return cell From 9a3632b68d7e75e957431341cdb603748997ca01 Mon Sep 17 00:00:00 2001 From: Zev Eisenberg Date: Sun, 4 Aug 2019 19:05:42 -0400 Subject: [PATCH 10/11] Reload table when the fetched results change. --- CoreDataDemo/SessionsViewController.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CoreDataDemo/SessionsViewController.swift b/CoreDataDemo/SessionsViewController.swift index 972d7b6..49ce208 100644 --- a/CoreDataDemo/SessionsViewController.swift +++ b/CoreDataDemo/SessionsViewController.swift @@ -35,6 +35,7 @@ class SessionsViewController: UITableViewController { sectionNameKeyPath: nil, // not using sections cacheName: nil // no need for caching ) + fetchedResultsController.delegate = self do { try fetchedResultsController.performFetch() @@ -67,3 +68,11 @@ class SessionsViewController: UITableViewController { } } + +extension SessionsViewController: NSFetchedResultsControllerDelegate { + + func controllerDidChangeContent(_ controller: NSFetchedResultsController) { + tableView.reloadData() + } + +} From 6b772ad3a8cd184c5b05a8ca4fa7e51212385fb7 Mon Sep 17 00:00:00 2001 From: Zev Eisenberg Date: Sun, 4 Aug 2019 19:30:24 -0400 Subject: [PATCH 11/11] Add a comment. --- CoreDataDemo/DataStore.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CoreDataDemo/DataStore.swift b/CoreDataDemo/DataStore.swift index 27d2cf5..0a0c848 100644 --- a/CoreDataDemo/DataStore.swift +++ b/CoreDataDemo/DataStore.swift @@ -10,6 +10,12 @@ import CoreData class DataStore { + // Note: having a shared/singleton data store isn't great practice, but it + // simplifies a few things for this demo app. It is better to have a data + // store at the top level of your app, such as in your App Delegate, and + // then pass it down to objectst that need it. Passing the data store around + // makes it much easier to test your classes by passing them a mock data + // store, or a data store that does not save changes to disk. static var shared = DataStore() var managedObjectContext: NSManagedObjectContext {