【发布时间】:2020-09-28 04:19:30
【问题描述】:
在 SwiftUI 中,我有一个网络请求在 scenedelegate 中运行,scenedidbecomeactive。我不知道当应用程序处于活动状态时用户将使用哪个视图,但我想在网络请求中的数据发生变化时发出警报。我简化了下面的代码,所以很容易阅读...
func sceneDidBecomeActive(_ scene: UIScene) {
let customClass = CustomClass()
customClass.performNetworkRequest()
在 CustomClass 中,我有...
func performNetWorkRequest() {
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let d = data {
let response = try JSONDecoder().decode(DetailResponse.self, from: d)
DispatchQueue.main.async {
//Here is where I want to either present an alert, but I can't figure out how to.
//OR do i put a func in SceneDeletegate to present the alert on the window.rootviewcontroller and then just call that func from here?
}
非常感谢任何帮助!
【问题讨论】:
-
理想情况下,您的网络请求类会呈现任何内容。它只会更新一些环境观察对象,相关视图会做出反应。
-
但我不确定用户在哪个视图上,因为它是scenedidbecomeactive,所以我必须将观察到的对象放入每个视图中吗?这似乎不太可扩展。我想我现在可以做到,因为只有 4-5 个视图。好的,让我试试看。
-
我可能会将它放在带有 zstack 的根视图中,该 zstack 显示“toast”视图和您的正常视图。当有更新时,您可以在显示的其他内容之上显示吐司几秒钟。这听起来不像是您想要打断用户正在执行的操作以实际显示警报的情况,但您可以这样做。
标签: ios swift xcode swiftui uialertcontroller