Gregor Sotošek
Gregor Sotošek

Reputation: 417

SwiftUI - ScrollView Navigation issues

SwiftUI: 5.9 XCode: 15.0.1 iPhone 11 iOS: 17.0.3

I am building an app with couple of screens. In one of the View I try to implement Vertical ScrollView with cca 3 - 6 Horizontal ScrollViews. They will display Thumbnails, Click on them should navigate to other screen/view (Camera). This works ok only first time I get to this screen. Once I am in Camera View and I hit button back, I get navigated to the first screen, but then the app freezes and there's nothing I can do. There are no errors displayed, the app freezes and there's nothing I can do. I read somewhere that there are bugs when navigating from scroll views in Swift, but I need to find a solution. This is the code:

struct Home2View: View {

@StateObject var accountViewModel: AccountViewModel
@StateObject var myPozeGroupViewModel: MyPozeGroupViewModel

var body: some View {
    NavigationView {
        ScrollView(.vertical) {
            VStack (spacing: 50) {
    
                ScrollView(.horizontal) {
                    horizontalList(data: myPozeGroupViewModel.top10MyPozeGroups, backgroundColor: .green)
                    }

                ScrollView(.horizontal) {
                        horizontalList(data: myPozeGroupViewModel.freshest10MyPozeGroups)
                    }
            
                ScrollView(.horizontal) {
                        horizontalList(data: myPozeGroupViewModel.letters10MyPozeGroups)
                    }
            }
        }
        .onAppear {
            
            if myPozeGroupViewModel.top10MyPozeGroups.isEmpty {
                myPozeGroupViewModel.addListenerForTop10()
            }
            
            if myPozeGroupViewModel.freshest10MyPozeGroups.isEmpty {
                myPozeGroupViewModel.addListenerForFreshest10()
            }
            
            if myPozeGroupViewModel.letters10MyPozeGroups.isEmpty {
                myPozeGroupViewModel.addListenerForLetters10()
            }
        }
    }
}

func horizontalList(data: [MyPozeGroup], backgroundColor: Color = .blue) -> some View {
    HStack(spacing:5) {
        ForEach(data) { myPozeGroup in
            NavigationLink(destination: CameraView(
                myPozeGroup: myPozeGroup,
                myPozeGroupViewModel: myPozeGroupViewModel,
                accountViewModel: accountViewModel)
            ) {
                VStack(spacing: 0) {
                    AsyncImage(url: URL(string: myPozeGroup.thumbnail ?? "")) { image in
                        image
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width: 150, height: 150)
                    } placeholder: {
                        ProgressView()
                    }
                    
                    Rectangle()
                        .fill(Color.white)
                        .frame(height: 20)
                        .overlay(
                            HStack {
                                Text(myPozeGroup.name ?? "")
                                    .foregroundColor(.black)
                                Spacer()
                                Text("\(myPozeGroup.counter)")
                                    .foregroundColor(.gray)
                            }
                        )
                }
                .cornerRadius(20)
            }
        }
    }
    
}

If someone can help and explain what can I do to overcome the issue would be great.

Upvotes: 0

Views: 111

Answers (1)

Gregor Sotošek
Gregor Sotošek

Reputation: 417

Ah, I think I found the reason. I had some ML models running at the background and for some weird reason I had a code that was supposedly stopping AVSession from main thread, while that was starting at main. Still weird I had no messages before freeze.

Upvotes: 1

Related Questions