Thursday, October 5, 2017

Terminating app due to uncaught exception 'CALayerInvalid', reason: 'layer is a part of cycle in its layer tree'

I'm using Xcode 8 and Swift 3 to develop an application for learning purpose. I have one controller and a table view. Everything was working fine at first. But after some silly modification in Interface Builder by a newbie like me, the app crashed when I started it in the simulator. Below is the full log message.

section=0, numOfRows=10
Item: name=Shiny Mac, serialNumber=Optional("2A725A93"), valueInDollars=70, dateCreated=2017-10-05 09:37:41 +0000
row=0, section=0
2017-10-05 16:37:41.594 Homepwner[62092:2091530] *** Terminating app due to uncaught exception 'CALayerInvalid', reason: 'layer <CALayer: 0x600000036240> is a part of cycle in its layer tree'
*** First throw call stack:
(
0   CoreFoundation                      0x0000000102251b0b __exceptionPreprocess + 171
1   libobjc.A.dylib                     0x00000001018a1141 objc_exception_throw + 48
2   CoreFoundation                      0x00000001022ba625 +[NSException raise:format:] + 197
3   QuartzCore                          0x0000000108867ea2 _ZN2CA5Layer30ensure_transaction_recursivelyEPNS_11TransactionE + 102
4   QuartzCore                          0x0000000108867eed _ZN2CA5Layer30ensure_transaction_recursivelyEPNS_11TransactionE + 177
5   QuartzCore                          0x0000000108867eed _ZN2CA5Layer30ensure_transaction_recursivelyEPNS_11TransactionE + 177
6   QuartzCore                          0x0000000108867eed _ZN2CA5Layer30ensure_transaction_recursivelyEPNS_11TransactionE + 177
7   QuartzCore                          0x000000010886fb3a _ZN2CA5Layer15insert_sublayerEPNS_11TransactionEP7CALayerm + 394
8   QuartzCore                          0x000000010886ff50 -[CALayer addSublayer:] + 180
9   UIKit                               0x0000000102e1d7ad -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1322
10  UIKit                               0x000000010311d9a0 -[UITableViewCell _addSubview:positioned:relativeTo:] + 357
11  UIKit                               0x0000000102e0bcf8 -[UIView(Hierarchy) addSubview:] + 838
12  UIKit                               0x000000010323b340 -[UITableViewCellLayoutManager layoutSubviewsOfCell:] + 2386
13  UIKit                               0x000000010326db12 -[UITableViewCellLayoutManagerValue1 layoutSubviewsOfCell:] + 71
14  UIKit                               0x00000001031271d7 -[UITableViewCell layoutSubviews] + 119
15  UIKit                               0x0000000102e2155b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1268
16  QuartzCore                          0x0000000108876904 -[CALayer layoutSublayers] + 146
17  QuartzCore                          0x000000010886a526 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 370
18  UIKit                               0x0000000102e0f334 -[UIView(Hierarchy) layoutBelowIfNeeded] + 1108
19  UIKit                               0x0000000102e1608e +[UIView(Animation) performWithoutAnimation:] + 90
20  UIKit                               0x0000000102ecac29 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 1125
21  UIKit                               0x0000000102ecacf8 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
22  UIKit                               0x0000000102e9f639 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2845
23  UIKit                               0x0000000102ed3ccc -[UITableView _performWithCachedTraitCollection:] + 111
24  UIKit                               0x0000000102ebae7a -[UITableView layoutSubviews] + 233
25  UIKit                               0x0000000102e2155b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1268
26  QuartzCore                          0x0000000108876904 -[CALayer layoutSublayers] + 146
27  QuartzCore                          0x000000010886a526 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 370
28  QuartzCore                          0x000000010886a3a0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
29  QuartzCore                          0x00000001087f9e92 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
30  QuartzCore                          0x0000000108826130 _ZN2CA11Transaction6commitEv + 468
31  QuartzCore                          0x0000000108826b37 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 115
32  CoreFoundation                      0x00000001021f7717 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
33  CoreFoundation                      0x00000001021f7687 __CFRunLoopDoObservers + 391
34  CoreFoundation                      0x00000001021dc038 CFRunLoopRunSpecific + 440
35  UIKit                               0x0000000102d5808f -[UIApplication _run] + 468
36  UIKit                               0x0000000102d5e134 UIApplicationMain + 159
37  Homepwner                           0x00000001012bf8a7 main + 55
38  libdyld.dylib                       0x00000001060b065d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

According to http://blog.benjamin-encz.de/post/ios-9-detects-cycles-in-layout-trees/, I found the problem. It's because I assigned the tableView object to the accessoryView property of the UITableViewCell. I knew that because when I selected the UITableViewCell in Interface Builder and opened the Connection Inspector, I saw the accessoryView is connecting to Table View in Outlets section. Please see the image below.



To understand about Outlets and Referencing Outlets sections, you can go to this post. According to this post, the accessoryView property (an instance of UIView) represents the small area to the right of the content view of a table view's cell. See the image below.


The table view has a cell. The cell has an accessory view. But, I made the table view as the accessory view of its own cell. This does not make sense and make a circular relationship between the table view and its cell.

The solution is deleting the connection between the accessoryView and Table View from the Outlets section.







1 comment: