SwiftKickMobile / TLLayoutTransitioning

Enhanced transitioning between UICollectionView layouts in iOS.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Transition crashes when the number of sections in the datasource increases

scalessec opened this issue · comments

Hey Tim, thanks for creating this. It was working well for me when I had only one section in my collection view, but now with multiple sections I'm experiencing a crash during transitionToCollectionViewLayout:duration:easing:completion: that I wasn't experiencing with the stock setCollectionViewLayout:animated:completion: method.

The setup:
I have a collection view that supports a transition between a 1-wide grid layout and a 4-wide grid layout. The cells in the collection view are sectioned by month (Sept 2014, August 2014, ...) and the content is paginated such that when the user scrolls to the bottom new content is requested from my API and appended to my dataSource (currently just calling reloadData instead of performing any insertion updates).

Steps:

  1. Start with a 1-wide grid. Transition to a 4-wide grid. Technically there's still only 1 section at this point, because we're still in the first month of data.
  2. Scroll down a few pages in the 4-wide grid until we've reached the next section.
  3. Attempt to transition back to the 1-wide grid results in a crash at this line:
UICollectionViewLayoutAttributes *toPose = reverse
                    ? [self.currentLayout layoutAttributesForItemAtIndexPath:indexPath]
                    : [self.nextLayout layoutAttributesForItemAtIndexPath:indexPath];

Because self.nextLayout's layoutAttributesForItemAtIndexPath:indexPath returns nil when the section index > 0. The error is:

Uncaught exception: *** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]

It appears that when I transition from the 1-wide layout, there's only one section in existence. Then, after scrolling the 4-wide layout, there's multiple sections. Attempting to go back to the 1-wide layout, the crash occurs.

If I scroll through many sections while in the 1-wide layout, and then scroll back up, I'm free to transition between layouts since the number of sections in the 1-wide layout is in sync with the number of sections in the 4-wide layout.

Any idea how to fix this? I'd very much like to continue using your library.

Thanks!

Have you tried invalidating the destination layout before transitioning?

Tim

On Sep 30, 2014, at 2:19 PM, Charles Scalesse notifications@github.com wrote:

Hey Tim, thanks for creating this. It was working well for me when I had only one section in my collection view, but now with multiple sections I'm experiencing a crash during transitionToCollectionViewLayout:duration:easing:completion: that I wasn't experiencing with the stock setCollectionViewLayout:animated:completion: method.

The setup:
I have a collection view that supports a transition between a 1-wide grid layout and a 4-wide grid layout. The cells in the collection view are sectioned by month (Sept 2014, August 2014, ...) and the content is paginated such that when the user scrolls to the bottom new content is requested from my API and appended to my dataSource (currently just calling reloadData instead of performing any insertion updates).

Steps:

  1. Start with a 1-wide grid. Transition to a 4-wide grid. Technically there's still only 1 section at this point, because we're still in the first month of data.
  2. Scroll down a few pages in the 4-wide grid until we've reached the next section.
  3. Attempt to transition back to the 1-wide grid results in a crash at this line:

UICollectionViewLayoutAttributes *toPose = reverse
? [self.currentLayout layoutAttributesForItemAtIndexPath:indexPath]
: [self.nextLayout layoutAttributesForItemAtIndexPath:indexPath];
Because self.nextLayout's layoutAttributesForItemAtIndexPath:indexPath returns nil when the section index > 0. The error is:

Uncaught exception: *** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]

It appears that when I transition from the 1-wide layout, there's only one section in existence. Then, after scrolling the 4-wide layout, there's multiple sections. Attempting to go back to the 1-wide layout, the crash occurs.

If I scroll through many sections while in the 1-wide layout, and then scroll back up, I'm free to transition between layouts since the number of sections in the 1-wide layout is in sync with the number of sections in the 4-wide layout.

Any idea how to fix this? I'd very much like to continue using your library.

Thanks!


Reply to this email directly or view it on GitHub #13.

I swear that was the very first thing I tried. Honestly. Ugh.

Thanks.

Hah. Glad it worked.

Tim

On Sep 30, 2014, at 2:47 PM, Charles Scalesse notifications@github.com wrote:

I swear that was the very first thing I tried. Honestly. Ugh.

Thanks.


Reply to this email directly or view it on GitHub #13 (comment).

Just in case anyone else has this issue, I thought I'd follow up with some additional info.

There was actually 2 separate problems here resulting the same crash, hence why I thought invalidateLayout originally didn't solve the problem. The first was what is described above and is fixed by invalidating the destination layout. The second occurred when the number of sections changed during the course of the transition itself. In my case this was also pretty common, because the transition from the 1-wide grid to the 4-wide grid frequently triggers an API request for the next page, and if the response comes back before the transition completes, it'll crash with the same error.