SwifUI onAppear被调用两次 [英] SwifUI onAppear gets called twice
问题描述
Q1:为什么onAppears会被调用两次?
Q1: Why are onAppears called twice?
Q2:或者,我可以在哪里拨打网络电话?
Q2: Alternatively, where can I make my network call?
我在代码中的几个不同位置放置了onAppears,它们都被调用了两次.最终,我会尝试在显示下一个视图之前进行网络调用,因此如果您知道不使用 onAppear 的方法,我会全力以赴.
I have placed onAppears at a few different place in my code and they are all called twice. Ultimately, I'm trying to make a network call before displaying the next view so if you know of a way to do that without using onAppear, I'm all ears.
我还尝试过在列表中放置和删除ForEach,但它没有任何改变.
I have also tried to place and remove a ForEach inside my Lists and it doesn't change anything.
Xcode 12 Beta 3->目标iO 14CoreData已启用但尚未使用
Xcode 12 Beta 3 -> Target iOs 14 CoreData enabled but not used yet
struct ChannelListView: View {
@EnvironmentObject var channelStore: ChannelStore
@State private var searchText = ""
@ObservedObject private var networking = Networking()
var body: some View {
NavigationView {
VStack {
SearchBar(text: $searchText)
.padding(.top, 20)
List() {
ForEach(channelStore.allChannels) { channel in
NavigationLink(destination: VideoListView(channel: channel)
.onAppear(perform: {
print("PREVIOUS VIEW ON APPEAR")
})) {
ChannelRowView(channel: channel)
}
}
.listStyle(GroupedListStyle())
}
.navigationTitle("Channels")
}
}
}
}
struct VideoListView: View {
@EnvironmentObject var videoStore: VideoStore
@EnvironmentObject var channelStore: ChannelStore
@ObservedObject private var networking = Networking()
var channel: Channel
var body: some View {
List(videoStore.allVideos) { video in
VideoRowView(video: video)
}
.onAppear(perform: {
print("LIST ON APPEAR")
})
.navigationTitle("Videos")
.navigationBarItems(trailing: Button(action: {
networking.getTopVideos(channelID: channel.channelId) { (videos) in
var videoIdArray = [String]()
videoStore.allVideos = videos
for video in videoStore.allVideos {
videoIdArray.append(video.videoID)
}
for (index, var video) in videoStore.allVideos.enumerated() {
networking.getViewCount(videoID: videoIdArray[index]) { (viewCount) in
video.viewCount = viewCount
videoStore.allVideos[index] = video
networking.setVideoThumbnail(video: video) { (image) in
video.thumbnailImage = image
videoStore.allVideos[index] = video
}
}
}
}
}) {
Text("Button")
})
.onAppear(perform: {
print("BOTTOM ON APPEAR")
})
}
}
推荐答案
我遇到了同样的问题.
我所做的是以下事情:
struct ContentView: View {
@State var didAppear = false
@State var appearCount = 0
var body: some View {
Text("Appeared Count: \(appearrCount)"
.onAppear(perform: onLoad)
}
func onLoad() {
if !didAppear {
appearCount += 1
//This is where I loaded my coreData information into normal arrays
}
didAppear = true
}
}
这通过确保onLoad()的if条件内部的内容仅运行一次来解决.
This solves it by making sure only what's inside the the if conditional inside of onLoad() will run once.
更新:Apple Developer论坛上的某人已提交了票证,Apple知道了此问题.我的解决方案是暂时的措施,直到Apple解决问题为止.
Update: Someone on the Apple Developer forums has filed a ticket and Apple is aware of the issue. My solution is a temporary hack until Apple addresses the problem.
这篇关于SwifUI onAppear被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!