gpt4 book ai didi

How to trigger onAppear when returning from fullScreenCover(如何在从FullScreenCover返回时触发onspecar)

翻译 作者:bug小助手 更新时间:2023-10-26 22:37:47 25 4
gpt4 key购买 nike



onAppear doesn't trigger

ON外观不会触发


struct ProfileView: View {
@StateObject var viewModel = ProfileViewViewModel()
var body: some View {
NavigationView {
VStack {
if let user = viewModel.user {
profile(user: user)
} else {
Text("Loading Profile...")
}
}
.navigationTitle("Profile")
}
.onAppear {
viewModel.fetchUser() //this is the problem
}
.fullScreenCover(isPresented: $viewModel.showingPreview) {
PreviewAvatarView()
}
}
}

I realized that onAppear didn't trigger when I dismiss fullscreencover.

我意识到,当我关闭全屏封面时,onAppar并没有触发。


更多回答

Why do you need it to? If PreviewAvatarView has changed he user, then this should be reflected by the model - pass the model object to the preview view

你为什么需要它呢?如果PreviewAvatarView更改了用户,则模型应反映这一点-将模型对象传递到预览视图

How about using onDisappear on PreviewAvatarView?

在预览AvatarView上使用onDisplay怎么样?

@Paulw11 I save the image in firebase so i need it to update url to display the avatar. If not, i must switch back and forth among views to trigger onAppear instead.

@Paulw11我将图像保存在Firebase中,所以我需要它来更新url以显示头像。如果不是,我必须在视图之间来回切换以触发onAppar。

@BenzyNeez I use viewmodel to separate logic and view so it's not a good way.

@BenzNeez我使用视图模型来分离逻辑和视图,所以这不是一个好方法。

You are unnecessarily incurring network operation costs. Yes, you are saving the avatar in firebase, but your model can also be aware of this change. Your model is an observable object which should be shared to the sub views. It can use snapshot listeners and similar to publish changes and your view will update automatically

您正在不必要地招致网络运营成本。是的,您正在将头像保存在Firebase中,但您的模型也可以意识到这一变化。您的模型是一个可观察的对象,应该与子视图共享。它可以使用快照监听程序和类似程序发布更改,您的视图将自动更新

优秀答案推荐

Instead of using onAppear you can use task.

您可以使用TASK,而不是使用onOuar。


struct ProfileView: View {
@StateObject var viewModel = ProfileViewViewModel()
var body: some View {
NavigationView {
VStack {
if let user = viewModel.user {
profile(user: user)
} else {
Text("Loading Profile...")
}
}
.navigationTitle("Profile")
}
.task(id: viewModel.showingPreview) {
guard !viewModel.showingPreview else {return} //Check for false

viewModel.fetchUser() //this is the problem
}
.fullScreenCover(isPresented: $viewModel.showingPreview) {
PreviewAvatarView()
}
}
}


task will run onAppear and when the Bool is false.

任务将在出现时运行,并且在Bool为False时运行。



The onAppear doesn’t trigger because your underlying view is not appearing – the view that is covering it is going away, which isn’t the same thing.

OnAppar不会触发,因为底层视图没有出现--覆盖它的视图正在消失,这不是一回事。


However, fullScreenCover has a rarely-used optional argument, onDismiss, which may fulfil your needs, e.g.:

但是,fullScreenCover有一个很少使用的可选参数onDismi,它可能会满足您的需求,例如:


.fullScreenCover(
isPresented: $viewModel.showingPreview,
onDismiss: { viewModel.fetchUser() }
) {
PreviewAvatarView()
}

Apple documentation

Apple文档



You can try this solution.

你可以试试这个解决方案。


struct ProfileView: View {
@StateObject var viewModel = ProfileViewViewModel()
var body: some View {
NavigationView {
VStack {
if let user = viewModel.user {
profile(user: user)
} else {
Text("Loading Profile...")
}
}
.navigationTitle("Profile")
}
.onAppear {
viewModel.fetchUser() //this is the problem
}
.fullScreenCover(isPresented: $viewModel.showingPreview) {
PreviewAvatarView()
}
.onChange(of: viewModel.showingPreview) { isFullScreenOpen in
if !isFullScreenOpen {
viewModel.fetchUser()
}//Executed only when full screen cover is closed.
}
}
}

Whenever the sheet closes the viewModel.fetchUser() will be executed.

每当工作表关闭时,都将执行viewModel.fetchUser()。


更多回答

Thank you! I have tried and it actually did

谢谢!我试过了,真的试过了

Also remove the unnecessary state object

同时删除不必要的状态对象

@malhal that is still something I don’t agree with you I think Apple makes it clear every year that VMs are a choice here is yet another example

@Malhal这仍然是我不同意你的观点,我认为苹果每年都会明确表示,虚拟机是一个选择这里还有另一个例子

Unfortunately the Core Data team has never been good at UI code and this is another depressing example. View state shouldnt be centralised like that because it means the View hierarchy invalidation isn't efficient. And @State with a class is currently a memory leak because the init is not autoclosure, it briefly was in an early beta and removed.

不幸的是,Core Data团队从来不擅长UI代码,这是另一个令人沮丧的例子。视图状态不应该像那样集中,因为这意味着视图层次结构无效效率不高。而带有一个类的@State目前是一个内存泄漏,因为init不是自动关闭的,它曾短暂地处于早期测试版并被删除。

@malhal if there is a leak you should report but State and Observable are designed to work together. Observables can be put into the Environment now like any other struct.

@malhal如果有泄漏,你应该报告,但是State和Observable是设计来一起工作的。Observable现在可以像任何其他结构一样放入Environment中。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com