iOS 15 UICollectionView issue for UICollectionViewRecursion
Asked Answered
B

6

13

The application is working fine till Xcode 12 and iOS 14 but as soon as I upgraded to Xcode 13 / iOS 15 my application stoped working with below error log

2021-11-26 12:49:38.844428+0530 Mitaja[6170:102791] [UICollectionViewRecursion] cv == 0x7feb458e0000 Enabling recursion trigger logging
2021-11-26 12:49:39.040798+0530 Mitaja[6170:102791] [UICollectionViewRecursion] UICollectionView 0x7feb458e0000 Visible cells update scheduled with call stack:
 (
    0   UIKitCore                           0x00007fff24644a06 -[UICollectionView _setNeedsVisibleCellsUpdate:withLayoutAttributes:] + 107
    1   UIKitCore                           0x00007fff24675708 -[UICollectionView _invalidateLayoutWithContext:] + 1310
    2   UIKitCore                           0x00007fff246a660c -[UICollectionViewLayout invalidateLayoutWithContext:] + 200
    3   UIKitCore                           0x00007fff2469a329 -[UICollectionViewFlowLayout invalidateLayoutWithContext:] + 667
    4   UIKitCore                           0x00007fff246a3b5f -[UICollectionViewFlowLayout _didPerformUpdateVisibleCellsPassWithLayoutOffset:] + 430
    5   UIKitCore                           0x00007fff24653a33 -[UICollectionView _updateVisibleCellsNow:] + 10438
    6   UIKitCore                           0x00007fff24653af8 -[UICollectionView _updateVisibleCellsNow:] + 10635
    7   UIKitCore                           0x00007fff24653af8 -[UICollectionView _updateVisibleCellsNow:] + 10635
    8   UIKitCore                           0x00007fff24653af8 -[UICollectionView _updateVisibleCellsNow:] + 10635
    9   UIKitCore                           0x00007fff24653af8 -[UICollectionView _updateVisibleCellsNow:] + 10635
    10  UIKitCore                           0x00007fff24653af8 -[UICollectionView _updateVisibleCellsNow:] + 10635
    11  UIKitCore                           0x00007fff24653af8 -[UICollectionView _updateVisibleCellsNow:] + 10635
    12  UIKitCore                           0x00007fff24653af8 -[UICollectionView _updateVisibleCellsNow:] + 10635
    13  UIKitCore                           0x00007fff24653af8 -[UICollectionView _updateVisibleCellsNow:] + 10635
    14  UIKitCore                           0x00007fff24653af8 -[UICollectionView _updateVisibleCellsNow:] + 10635
    15  UIKitCore                           0x00007fff24653af8 -[UICollectionView _updateVisibleCellsNow:] + 10635
    16  UIKitCore                           0x00007fff24653af8 -[UICollectionView _updateVisibleCellsNow:] + 10635
    17  UIKitCore                           0x00007fff24653af8 -[UICollectionView _updateVisibleCellsNow:] + 10635
    18  UIKitCore                           0x00007fff24653af8 -[UICollectionView _updateVisibleCellsNow:] + 10635
    19  UIKitCore                           0x00007fff24653af8 -[UICollectionView _updateVisibleCellsNow:] + 10635
    20  UIKitCore                           0x00007fff24653af8 -[UICollectionView _updateVisibleCellsNow:] + 10635
    21  UIKitCore                           0x00007fff24653af8 -[UICollectionView _updateVisibleCellsNow:] + 10635
    22  UIKitCore                           0x00007fff24657447 -[UICollectionView layoutSubviews] + 325
    23  UIKitCore                           0x00007fff255bcaad -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2962
    24  QuartzCore                          0x00007fff2893c2b6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 548
    25  QuartzCore                          0x00007fff289474f1 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 65
    26  QuartzCore                          0x00007fff288830ca _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 652
    27  QuartzCore                          0x00007fff288bac47 _ZN2CA11Transaction6commitEv + 699
    28  QuartzCore                          0x00007fff288bbfc8 _ZN2CA11Transaction25flush_as_runloop_observerEb + 60
    29  CoreFoundation                      0x00007fff20368c77 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    30  CoreFoundation                      0x00007fff2036349c __CFRunLoopDoObservers + 541
    31  CoreFoundation                      0x00007fff20363a4c __CFRunLoopRun + 1126
    32  CoreFoundation                      0x00007fff20363103 CFRunLoopRunSpecific + 567
    33  GraphicsServices                    0x00007fff2c851cd3 GSEventRunModal + 139
    34  UIKitCore                           0x00007fff24ffbe63 -[UIApplication _run] + 928
    35  UIKitCore                           0x00007fff25000a53 UIApplicationMain + 101
    36  Mitaja                              0x0000000108209adf main + 63
    37  dyld                                0x000000010ca84e1e start_sim + 10
    38  ???                                 0x000000010f5854fe 0x0 + 4552414462
    39  ???                                 0x0000000000000000 0x0 + 0
    40  Mitaja                              0x0000000106ff7000 __dso_handle + 0
)


2021-11-26 12:49:40.736245+0530 Mitaja[6170:102791] [UICollectionViewRecursion] <UICollectionView: 0x7feb458e0000; frame = (122.5 0; 291.5 0); clipsToBounds = YES; hidden = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x600002764810>; layer = <CALayer: 0x600002d43b00>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; layout: <UICollectionViewFlowLayout: 0x7feb43fa4810>; dataSource: <Mitaja.JPostCardVC: 0x7feb452ed000>> is stuck in its update/layout loop. See previous logs in the "UICollectionViewRecursionCategory" to find callers that scheduled a recursive update
2021-11-26 12:49:40.736365+0530 Mitaja[6170:102791] *** Assertion failure in -[UICollectionView _setNeedsVisibleCellsUpdate:withLayoutAttributes:], UICollectionView.m:1511

What should be the solution to it?
Any help will be appreciated.
I tried to debug it for many times but no success, please help me with this.

EDIT

"UICollectionView (<UICollectionView 0x7fd6ca190000>) is stuck in its update/layout loop. This can happen for many reasons, including self-sizing views whose preferred attributes are not returning a consistent size. To debug this issue, check the Console app for logs in the "UICollectionViewRecursion" category."

Error description for crash

Bircher answered 26/11, 2021 at 7:56 Comment(4)
I got the same issue. Do you found how to solve it?Bollay
We have debugged it and find out the root cause. The issue is with the height of the collection view. We have to give it some fixed height and the issue is resolved. thank you all for the answers, which help me to find out the issue.Bircher
Can you post some code? Did you set the height of the collection view frame or use a constraint?Cud
You could try the solution provided here: https://mcmap.net/q/909046/-self-sizing-collection-view-enters-a-recursive-loop-in-ios-15Grapeshot
T
2

I had similar issue in iOS 15. In my case it was fixed by overriding UICollectionViewFlowLayout method:

override func shouldInvalidateLayout(forPreferredLayoutAttributes preferredAttributes: UICollectionViewLayoutAttributes, 
         withOriginalAttributes originalAttributes: UICollectionViewLayoutAttributes) -> Bool {
    return true
}
Torosian answered 18/5, 2022 at 15:4 Comment(1)
it works for me but can you please explain how it helps to layoutEvocative
S
0

We were experiencing the same error with our iOS app using Xamarin.Forms. We were able to resolve the issue by explicitly specifying the height of the items in the collection view. Not a permanent solution, but should be able to get you past the crash.

Sruti answered 15/12, 2021 at 13:15 Comment(0)
E
0

I am here https://github.com/WenchaoD/FSCalendar/issues/1351 Answered similar questions on the; The preliminary judgment is that the total width of the item exceeds the screen width when calculating the item size in layout. It is suggested to use floor to round down when calculating the item size to temporarily solve this problem; The same code is in ios15 Version 2 can run normally before; In addition, it is very strange that I have no problem on iPhone 11, and the flash back will appear on iPhone 11 Pro max

Eigenfunction answered 20/12, 2021 at 3:46 Comment(0)
H
0

https://github.com/xamarin/Xamarin.Forms/issues/15230 mentiones a couple of workarounds. I know it is Xamarin.Forms, but under the hood the ItemSizingStrategy="MeasureFirstItem" probably translates to an iOS property on the UICollectionView

Headrace answered 23/3, 2022 at 8:37 Comment(0)
M
0

I have faced this issue, it was solved.

In my case, the root cause is the overwrite methods inside the subclass of UICollectionViewFlowLayout have a bad implementation, they keep all collection sizes (cache them) to re-use later which instead of should be calculated whenever the delegate invoke to.

So, you should review the classes inherited from UICollectionViewFlowLayout to make sure they have no reference cycle and should have no recursion, and keep it safe.

Hope you have a good solution.

Monotype answered 30/4, 2022 at 6:15 Comment(0)
S
0

There are two simple things that you can do to avoid this crash.

  1. Inside the cell, override function preferredLayoutAttributesFitting and return the layoutAttribute after making the frame size rounded for x, y, height and width.
  2. Removing estimatedItemSize that we set for default or estimated width and height.
Send answered 23/3, 2023 at 7:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.