bobbyali / kidzvids2

Second attempt at the kid-friendly Youtube iOS player
1 stars 2 forks source link

Crash when changing from long playlist to short one #17

Closed bobbyali closed 9 years ago

bobbyali commented 9 years ago

App crashes with this error: fatal error: Cannot index empty buffer

bobbyali commented 9 years ago

May need to force the Grid view controller to scroll to the top when returning from the settings view controller. How?

bobbyali commented 9 years ago

Tried making UICollectionView go to top when view controller is loaded, but it didn't help.

https://github.com/bobbyali/kidzvids2/blob/master/kidsvids/GridCollectionViewController.swift#L87

Not sure what to do now. It seems that the Collection View is out of sync with the data when the Playlist is changed. The Playlist gets changed by modifying a reference variable (currentPlaylist) in the singleton PlaylistCollection class, and this is directly accessed by the GridViewController, which should then update the number of items based on this:

https://github.com/bobbyali/kidzvids2/blob/master/kidsvids/GridCollectionViewController.swift#L120

But for some reason the UICollectionView (I think) still believes that the old number of items is available, and then crashes when it tries to access an item that is no longer there (because the new Playlist has fewer items). But I can't figure out how to force the UICollectionView to update this.

bobbyali commented 9 years ago

This is the line where the app actually crashes:

https://github.com/bobbyali/kidzvids2/blob/master/kidsvids/GridCollectionViewController.swift#L128

bobbyali commented 9 years ago

This link looks relevant, but I've tried the suggestions and they still don't work.

http://stackoverflow.com/questions/28055177/how-do-i-prevent-cellforitematindexpath-from-executing-before-i-have-my-data-rea

I've made numberOfSectionsInCollectionView and numberOfItemsInSection conditional on having the data loaded, but this code doesn't seem to be getting executed before the crash occurs.

https://github.com/bobbyali/kidzvids2/blob/master/kidsvids/GridCollectionViewController.swift#L112-L126

isDataFetched is set to true in the ScrollView function (for when user scrolls to bottom to fetch more results) and refreshViewController() (when new playlist is loaded by view controller). It's then set to false in the delegate fetchCompleted() method.

bobbyali commented 9 years ago

Interestingly, the bug doesn't occur if the user never scrolls down. (i.e. they load the app, then change playlist immediately without scrolling)

bobbyali commented 9 years ago

Fixed. Needed a combination of the checks for isDataLoaded, and also needed some logic fixing in my code (my mistakes) when refreshing the view controller.