SwiftUI - Attempted to scroll the collection view to an out-of-bounds item

1.3k Views Asked by At

My App is getting crashed immediately after navigating to the screen in iOS>16.0 devices by saying

*** Assertion failure in -[_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF020PagingCollectionView _validateScrollingTargetIndexPath:], UICollectionView.m:7339

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to scroll the collection view to an out-of-bounds item (0) when there are only 0 items in section 0. Collection view: <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF020PagingCollectionView: 0x157a3ee00; baseClass = UICollectionView; frame = (0 0; 720.333 393); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x60000275e9d0>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x600002f758e0>; contentOffset: {0, 0}; contentSize: {0, 393}; adjustedContentInset: {0, 0, 0, 0}; layout: <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF012PagingLayout: 0x1555269a0>; dataSource: <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF011Coordinator: 0x600001a63720>>.'

I don't have any idea regarding UICollectionView. I haven't used it anywhere in the code

Here's the code I am using. Here I am getting data from MyDataViewModel()

struct DataDisplayView: View {
    @StateObject private var myDataVM = MyDataViewModel()

    var body: some View {
        GeometryReader { reader in
            TabView {
                let data = myDataVM.data.chunked(into: 3)
                ForEach(0..<data.count, id: \.self) { data in
                    Circle()
                        .fill(Color.orange.opacity(0.3))
                        .frame(width: 100, height: 100)
                }
                .rotationEffect(.degrees(-90))
                .frame(
                    width: reader.size.width,
                    height: reader.size.height
                )
            }
            .frame(
                width: reader.size.height,
                height: reader.size.width
            )
            .rotationEffect(.degrees(90), anchor: .topLeading)
            .offset(x: reader.size.width)
            .tabViewStyle(
                PageTabViewStyle(indexDisplayMode: .never)
            )
        }
        .background(Color.black)
        .onAppear() {
            myDataVM.dataWebServiceCall()
        }
    }
}

Below is the extension of an array to stride

extension Array {
    func chunked(into size: Int) -> [[Element]] {
        return stride(from: 0, to: count, by: size).map {
            Array(self[$0 ..< Swift.min($0 + size, count)])
        }
    }
}

FYI: The above code works fine in iOS<16.0

FYI: In the above code if I use ForEach(0..<12, id: \.self) instead of ForEach(0..<data.count, id: \.self) it is working fine.

I am facing this issue in iOS>16.0 and with dynamic(API) data.

1

There are 1 best solutions below

0
linh lê On

I think that the data array has changed from let say 12 to 0 and the view list wasn’t updated so it still has 12 cells. But in fact the data has 0 items.

It maybe because some change on iOS 16.

The possible way to fix that is you need to force reloading the list view.