Search code examples
ioscrashcommitanimations

iOS code crashing on commitAnimations while in main thread


I am animating a scroll view content inset for pull down to refresh.

NSLog(@" in main thread? %d", [NSThread isMainThread]); // prints 1
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.3];
[scrollView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, scrollView.contentInset.bottom, 0.0f)];
// Crash here
[UIView commitAnimations];

I get a crash when committing the animations and it crashes it the main thread. If I remove this block of code, my app never crashes. What could be causing a crash at this point if it's pretty much narrowed down to this block of code?


Solution

  • Since the animation was to scroll and cellForRowAtIndexPath referenced results from a Core Data fetch request, the animation crashed the app since I accidentally did it right before reloading the table view data. This occurred because data in the table view was no longer available so the animation scrolled to a row that no longer existed.

    Lesson learned - if you're working with scrollview animations and Core Data, be careful about when you choose to reload your table view data. Don't perform the fetch, then scroll, then reload your table view data. Do perform the fetch, then reload your table view data, then scroll.

    It would have been really obvious had there been an error message with the crash.