SwiftUI app crashes while scrolling section in List via ScrollViewReader

In our SwiftUI application , we are using List in one of our screen. We have a requirement to scroll to specific section header.

Below code works fine till iOS16 and from iOS16 onwards we are getting the app crash.

Can anyone suggest me any probable solution to achive this ?.

        ScrollViewReader { proxy in

            List {

                ForEach(Array(viewModel.newSections), id: .self) { sectionName in

                    Section(header: VStack { LeaguesScheduleListSectionView(text: sectionName) }) {

                        ForEach(viewModel.newRowsPerSection[sectionName] ?? []) { leagueScheduleEvent in

                            LeaguesScheduleListRowView(leagueSchedule: leagueScheduleEvent)

                        }

                    }

                    .id(sectionName)

                }

            }

            .listStyle(.plain)

            .onAppear(perform: {

                 if viewModel.newSections.contains("Today/Ongoing"),

                 viewModel.newRowsPerSection[("Today/Ongoing")]?.count ?? 0 > 0 {

                 proxy.scrollTo("Today/Ongoing", anchor: UnitPoint.topLeading)

                 }

            })

        }

Please find the application crash report for reference.

2023-03-11 01:18:14.914674+0530 Tenmates-Staging-[36242:3611792] *** Assertion failure in -[SwiftUI.UpdateCoalescingCollectionView _validateScrollingTargetIndexPath:], UICollectionView.m:7349

2023-03-11 01:18:14.940662+0530 Tenmates-Staging-[36242:3611792] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to scroll the collection view to an out-of-bounds item (9223372036854775807) when there are only 0 items in section 0. Collection view: <SwiftUI.UpdateCoalescingCollectionView: 0x13c97ae00; baseClass = UICollectionView; frame = (0 0; 393 605); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x60000335e0d0>; backgroundColor = <UIDynamicSystemColor: 0x6000029b4e40; name = systemGroupedBackgroundColor>; layer = <CALayer: 0x600003d258a0>; contentOffset: {0, 0}; contentSize: {393, 1309.0000000000002}; adjustedContentInset: {0, 0, 0, 0}; layout: <UICollectionViewCompositionalLayout: 0x142371a00>; dataSource: <TtGC7SwiftUI31UICollectionViewListCoordinatorGVS_28CollectionViewListDataSourceOs5Never_GOS_19SelectionManagerBoxS2__: 0x142309b10>>.'

*** First throw call stack:

(

0   CoreFoundation                      0x000000018040e7c8 __exceptionPreprocess + 172

1   libobjc.A.dylib                     0x0000000180051144 objc_exception_throw + 56

2   Foundation                          0x0000000180b13b98 _userInfoForFileAndLine + 0

3   UIKitCore                           0x000000010be3cd2c -[UICollectionView _validateScrollingTargetIndexPath:] + 408

4   UIKitCore                           0x000000010be3d044 -[UICollectionView _contentOffsetForScrollingToItemAtIndexPath:atScrollPosition:] + 48

5   UIKitCore                           0x000000010be3dbbc -[UICollectionView _scrollToItemAtIndexPath:atScrollPosition:animated:] + 220

6   SwiftUI                             0x000000011001d374 OUTLINED_FUNCTION_2 + 2764

7   SwiftUI                             0x000000011001e40c OUTLINED_FUNCTION_2 + 7012

8   SwiftUI                             0x000000011001e454 OUTLINED_FUNCTION_2 + 7084

9   UIKitCore                           0x000000010cacb118 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1920

10  QuartzCore                          0x0000000187f5f970 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 440

11  QuartzCore                          0x0000000187f6a4b4 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 128

12  QuartzCore                          0x0000000187e97a74 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 440

13  QuartzCore                          0x0000000187ec3a24 _ZN2CA11Transaction6commitEv + 652

14  QuartzCore                          0x0000000187ec4e64 _ZN2CA11Transaction25flush_as_runloop_observerEb + 68

15  CoreFoundation                      0x000000018037212c __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32

16  CoreFoundation                      0x000000018036cacc __CFRunLoopDoObservers + 512

17  CoreFoundation                      0x000000018036cf84 __CFRunLoopRun + 968

18  CoreFoundation                      0x000000018036c7a4 CFRunLoopRunSpecific + 584

19  GraphicsServices                    0x0000000188ff7c98 GSEventRunModal + 160

20  UIKitCore                           0x000000010c62e37c -[UIApplication _run] + 868

21  UIKitCore                           0x000000010c632374 UIApplicationMain + 124

22  libswiftUIKit.dylib                 0x0000000109d60fd0 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF + 100

23  Tenmates-Staging-                   0x0000000102745fbc $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 104

24  Tenmates-Staging-                   0x0000000102745f44 $s17Tenmates_Staging_11AppDelegateC5$mainyyFZ + 44

25  Tenmates-Staging-                   0x0000000102749fa8 main + 28

26  dyld                                0x00000001086c9fa0 start_sim + 20

27  ???                                 0x00000001085dde50 0x0 + 4435336784

28  ???                                 0xe16f800000000000 0x0 + 16244343118437023744

)

It looks like iOS 16 bug. When we have much more users with iOS 17, we are getting this crash report exclusively 100% for iOS 16 users in Crashlytics. Would be nice to have some simple workaround, but this crash is not happening extremely often, so we just live with it, iOS 16 is not that popular anymore.

SwiftUI app crashes while scrolling section in List via ScrollViewReader
 
 
Q