SwiftUI:5.9 代码:15.0.1 iPhone 11 iOS:17.0.3
我正在构建一个带有几个屏幕的应用程序。在其中一个视图中,我尝试使用 cca 3 - 6 个水平滚动视图来实现垂直滚动视图。它们将显示缩略图,单击它们应导航到其他屏幕/视图(相机)。只有当我第一次进入这个屏幕时,这才可以正常工作。一旦我进入相机视图并按下返回按钮,我就会导航到第一个屏幕,但随后应用程序冻结,我无能为力。没有显示任何错误,应用程序冻结,我无能为力。我在某处读到,在 Swift 中从滚动视图导航时存在错误,但我需要找到解决方案。这是代码:
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)
}
}
}
}
如果有人可以帮助并解释我可以做什么来克服这个问题,那就太好了。
啊,我想我找到原因了。我有一些 ML 模型在后台运行,出于某种奇怪的原因,我有一个代码应该从主线程中停止 AVSession,而该代码是在主线程中启动的。还是很奇怪,我在冻结之前没有消息。