Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash on .commitEdit event when trying to delete a row with .delete editStyle. #1

Closed
Ignakas opened this issue May 3, 2019 · 1 comment
Assignees
Labels
bug Something isn't working
Milestone

Comments

@Ignakas
Copy link

Ignakas commented May 3, 2019

Hi,
I am trying to delete a row from my table using .delete editStyle and the app is crashing due to Fatal error: Index out of range. By the looks of it the datasource gets updated before the tableView finishes editing and director tries to access model at indexPath(in didEndEditingRowAt: method) which was already deleted.

To reproduce the issue - add the lines below into CatalogViewController of OwlDemo app.
What's the correct way to delete a row from a table?

catalogAdapter.events.editStyle = { ctx in
    return .delete
}

catalogAdapter.events.canEditRow = { ctx in
    return true
}

catalogAdapter.events.deleteConfirmTitle = { ctx in
    return "Delete"
}

catalogAdapter.events.commitEdit = { [weak self] ctx, style in
    guard let indexPath = ctx.indexPath else { return }
    self?.tableDirector?.sections[indexPath.section].remove(at: indexPath.row)
    self?.tableDirector?.reload()
}
@malcommac
Copy link
Owner

Hi, Thank you for your report.
I've just fixed this issue but in order to correctly receive events for end editing model and cell you mus enclose the function inside an editing session:

That's the code:

catalogAdapter.events.commitEdit = { [weak self] ctx, style in
  guard let indexPath = ctx.indexPath else { return }
  self?.tableDirector?.reload(afterUpdate: { dir in
    dir.sections[indexPath.section].remove(at: indexPath.row)
    return UITableView.RowAnimation.none
  }, completion: nil)
}

@malcommac malcommac added this to the 1.0.1 milestone May 5, 2019
@malcommac malcommac added the bug Something isn't working label May 5, 2019
@malcommac malcommac self-assigned this May 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants