tableView cell을 클릭하면 완료 표시가 나타나야 하지만 tableView(_:didSelectRowAt:) 메서드가 실행조차 되지 않는 상태
private func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) async {
var selectedTodo = todoManager.getTodoList(date: viewModel?.getDate)[indexPath.row]
selectedTodo.done = !selectedTodo.done
if await todoManager.updateTodo(todo: selectedTodo) == true {
tableView.reloadRows(at: [indexPath], with: .automatic)
}
}
tableView(_:didSelectRowAt:) 메서드를 비동기로 호출하기 전까지 문제없이 동작했지만 비동기로 호출하면 동작하지 않는다.
따라서 메서드를 비동기 호출하면 안된다.
tableView(_:didSelectRowAt:) 메서드를 비동기로 변경하지 않고, 내부의 비동기 작업만을 처리해야 한다.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var selectedTodo = todoManager.getTodoList(date: viewModel?.getDate)[indexPath.row]
selectedTodo.done = !selectedTodo.done
Task {
if await todoManager.updateTodo(todo: selectedTodo) == true {
DispatchQueue.main.async {
tableView.reloadRows(at: [indexPath], with: .automatic)
}
}
}
}
이제 cell을 클릭하면 완료 표시가 되는 것을 볼 수 있습니다.