UIViewController lifecycle violation, viewWillDisappear called before viewWillAppear or viewDidLoad

Originator:dag.agren
Number:rdar://25744056 Date Originated:15/04/2016
Status:Open Resolved:
Product:iOS Product Version:
Classification: Reproducible:Always
 
Summary:
Under certain circumstances, a view controller's viewWillDisappear method will be called before viewWillAppear has been called, and even before viewDidLoad has been.

This can break many assumptions made by apps, and cause crashes and unexpected behaviour that is very hard to debug.

Steps to Reproduce:
One way to trigger the bug seems to be to switch quickly between tabs in a UITabController.

A sample project is included that does this automatically. dispatch_after is used to trigger two tab switches in short sequence. The view controllers contain only assert statements that check if the correct order of methods are called. The asserts fail if the tab switches happen quickly enough.

To reproduce:

1. Unpack the attached project and open it.
2. Run the app.

Expected Results:
Nothing in particular. The tab controller should quickly switch to the second tab and back to the first with no problems.

Actual Results:
The assert statements that guard against an incorrect order of lifecycle method invocations fail.

Version:
Xcode 7.3

Notes:
Apparently, viewWillLoad will be invoked if there is a long enough delay between tab switches, and then everything will work. However, the sequencing fails if tab switches are fast enough.

Configuration:
Xcode 7.3

Attachments:
'CrashTest.zip' was successfully uploaded.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!