我正在使用CoreData和MVVM模式创建简单的Todo应用。当我创建新任务和模态视图消失时,只有在应用程序重新启动后,tableView才会重新加载新数据。我使用自定义委托进行重新加载:
AddNewTaskController
@IBAction func save(_ sender: UIBarButtonItem) {
switch viewModel.validation() {
case .success(_):
viewModel.save { result in
switch result {
case .success(_):
DispatchQueue.main.async {
self.refreshTableDelegate.refreshTable()
self.dismiss(animated: true, completion: nil)
}
case .error(let error):
DispatchQueue.main.async {
self.errorAlert(withMessage: error)
}
}
}
case .error(let errorMsg):
DispatchQueue.main.async {
self.errorAlert(withMessage: errorMsg)
}
}
}
TasksListController
private func getTasks() {
viewModel.getTasks { result in
switch result {
case .success(_):
DispatchQueue.main.async { [unowned self] in
self.tableView.reloadData()
}
case .error(let errorMsg):
DispatchQueue.main.sync { [unowned self] in
self.errorAlert(withMessage: errorMsg)
}
}
}
}
func refreshTable() {
getTasks()
}
有谁知道如何使用委托和MVVM刷新tableView?