An error occurs when scrolling the list in ios16. What should I do?

Viewed 79

An error occurs when the Add button is pressed. On ios15 it worked fine.

On ios 15 it worked fine. but on ios16 An error occurs when the Add button is touched. I don't know why. Help.

Error: Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1068e25ec)

public struct ListTest2: View
{
    @State var data: [Int] = []
    
    public var body: some View
    {
        ScrollViewReader
        { proxy in
            VStack
            {
                Button {
                    proxy.scrollTo(data.count - 1)
                } label: {
                    Text("goto end")
                }
                
                Button {
                    proxy.scrollTo(1)
                } label: {
                    Text("goto start")
                }
                
                
                Button {
                    data.append(data.count + 1)
                    proxy.scrollTo(data.count - 1)
                } label: {
                    Text("Add")
                }
                                
                List {
                    
                    ForEach(data, id:\.self)
                    { index in
                        Text("\(index )")
                    }
                }
            }
            .onAppear()
            {
                for index in 1...30 {
                    data.append(index)
                }
            }
        }
    }
} 
1 Answers

So we know that this bug is in iOS 16 with the List, majorly it's affecting the indexes of the items and also causing problems with lazy loading (loading items when reached second last item) in my case, so the proper fix I figured without any hack is,

@ObservedObject private var friendsModel = FriendsModel.shared

ScrollView {
    LazyVStack {
        ForEach(friendsModel.friends.indices, id: \.self) { index in
            Button {
                // your action
            } label: {
                // your cell view
            }
            .onAppear {
                if index == friendsModel.friends.count - 2 {
                    // load more items
                }
            }
        }
        
        if friendsModel.isLoading {
            ProgressView()
               .padding())
        }
    }
}
.refreshable {
    friendsModel.refresh()
}

Use ScrollView with LazyVStack and you will still be able to use features like refreshable, etc.

Related