Fancy UITableView crash on - [__ NSArrayM objectAtIndex:]: index 0 out of bounds for empty array

I've searched a ton and haven't found an answer that seems to solve the problem I'm running into.

I have a UITableViewController. In the viewDidLoad method, I load objects from the database and then call reloadData.

This usually works fine now. However, now I am doing a sort that is saved so that the same sorting algorithm is used the next time I start the application. Note that in all cases the size of the array supporting the partition is the same after sorting.

If I use the default sort method, I don't get any crash even though it goes through the same class> call delegate> reloadTable code path. However, if I use an alternate sort method that uses the same code path, I see that the table view asks for the row count and gets the correct number, but then I get this exception. Only objects are sorted in a different order, so it doesn't make sense to me.

It should also be noted that if I sort the objects (which use the same code path) after displaying the table, it works fine.

It doesn't make any sense to me and almost looks like a UITableView bug, but I hope this is something I can fix.

My application is open source, so I can link directly to the appropriate files:

https://github.com/einsteinx2/iSub/blob/weird_crash/Classes/UI/Reusable/ItemViewController.swift https://github.com/einsteinx2/iSub/blob/weird_crash/Classes/UI/Reusable/ItemViewModel.swift

The most relevant are ItemViewModel.sortAll()

andItemViewController.itemsChanged(viewModel: ItemViewModel)

I have run out of ideas on how to debug this. It just doesn't make sense.

Some trial errors and errors I've tried:

  • Using only one table section
  • Calling reloadData after a delay to eliminate any race condition.
  • Don't call reloadData in my itemChanged delegate method (still failing as reloadData appears to be automatically called by the table view controller)

Nothing matters at all. I am totally confused how this is possible. If anyone has any ideas on how I can continue to debug this or how this is possible, I'm all ears. Thank.

EDIT: in order to make things more complicated, I also continued sorting for albums that use the same ItemViewController, however, reverting to an album that was previously sorted does not crash, although it should do the same and runs the same code path ...

+3


source to share


1 answer


Wow, I finally figured it out and will talk about edge case!

It turns out that the crash has to do with the indexes of the table partition (letters on the right side) and not with the actual loading or sorting of the data.

I used the unicode character character

if not sorted by name. Somehow it worked when you sorted live, but there is some bug in the UITableView that causes the internal array of section indices to be empty when you load the table with this data. I switched to using the black circle symbol

and now it doesn't work anymore.



I think this should be the smallest change for most of the research I've ever done in 8 years of coding for iOS.

Hope this helps a random person in the future. I will report the bug to Apple, although it will most likely not be a high priority so who knows if / when they will fix it.

0


source







All Articles