I'm currently having the user input data on a modal and save it to Core Data:
var container: NSPersistentContainer!
func saveContext() {
if container.viewContext.hasChanges {
do {
try container.viewContext.save()
} catch {
print("An error occured whlie saving: \(error.localizedDescription)")
}
}
}
Upon dismissing the modal, I want to reload the table on the parent view controller. But, no matter how I load it, from viewDidLoad or viewWillAppear, the table doesn't get reloaded. I know that the data is properly saved into Core Data because when I rebuild, the data gets properly displayed on the table.
This is the function I want to trigger upon dismissing the modal:
func loadSavedData() {
if fetchedResultsController == nil {
let request = Goal.createFetchRequest()
let sort = NSSortDescriptor(key: "title", ascending: true)
request.sortDescriptors = [sort]
request.fetchBatchSize = 20
fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: container.viewContext, sectionNameKeyPath: "title", cacheName: nil)
fetchedResultsController.delegate = self
}
fetchedResultsController.fetchRequest.predicate = myPredicate
do {
try fetchedResultsController.performFetch()
tableView.reloadData()
} catch {
print("Fetch failed")
}
}