iOS 9 Detects Cycles in Layout Trees · [Thinking inside a large box];

iOS 9 Detects Cycles in Layout Trees

A couple of months ago I was faced with an issue that was fairly hard to debug:

What I found out, after about 30 minutes of debugging, was the following:

The hackathon attendee hat mistakenly set the accessoryView of a UITableViewCell to the UITableView in which the cell was contained - causing an endless recursion during the first layout cycle.

This however wasn’t immediately obvious to me since the symptom was an exception within the cellForRowAtIndexPath: method.

Fixed in iOS 9

Today I wanted to see if it is possible to reproduce the issue with Xcode 7 Beta 3.

Indeed, I can still cause the crash. However, instead of causing a stack overflow the app now terminates because CALayer throws an exception:

CrashTableViewCell[33582:11713163] *** Terminating app due to uncaught exception 'CALayerInvalid', reason: 'layer <CALayer: 0x7f875943c730> is a part of cycle in its layer tree

I couldn’t find any reference to this exception in a Stack Overflow exception so I assume the cycle detection is new in iOS 9.

This fix will likely save some headaches!

It also serves as a good real world example of detecting cycles in directed graphs.