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")
}
}
In this article of Sean Berry published in Medium, you can know more about the reason why viewWillAppear is not called.
Then, you can use a closure to know when your modal is dismissed.
Add it in your modal view controller:
var isDismissed: (() -> Void)?
Then you can call it when you dismiss the controller:
self.isDismissed?()
Finally in the presenting view controller, when you init your modal, you can listen for the closure, for example:
let modalController = YourController()
modalController.isDismissed = { [weak self] in
self?.loadSavedData()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.