2

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.

Bhanuteja
  • 771
  • 2
  • 8
  • 18
  • If you will check like if data count > 0 and only then display TabView it will work, but it crashes for me on scroll via timer with items reloading – Stepan Maksymov Dec 28 '22 at 12:42
  • I recreated your code in iOS 16 on a navigationstack and did not get any crash, when navigating to ```DataDisplayView```. You will need to post some code of you API call aswell if this is to be figured out, it's most probably what's causing the crash. – devdchaudhary Aug 09 '23 at 00:58
  • I recommend you make this call asynchronously and preferably in the background. – Maykon Meneghel Aug 09 '23 at 14:24
  • Why do you need to separate the display into chunks? – Maykon Meneghel Aug 09 '23 at 14:25
  • 1
    I think you need to put this code into your viewModel "let data = myDataVM.data.chunked(into: 3)", do this chunk process right after your api call and assign result to Published property of your viewModel, note that assigning should be in main thread to update our view. And in use like this ForEach($viewModel.data... – Arsienij Aug 10 '23 at 16:55
  • For me, this looks half info. On Appear he is calling API, This should be in DI resolving maybe at the init of the View model. But looks like he wants to create an array with that api. He tried doing stride and chunking but only for looping those arrays to create a circle. He needs to add more information to get advices from other peoples. – B25Dec Aug 15 '23 at 03:28

1 Answers1

-1

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.

linh lê
  • 102
  • 5