具有后台任务的 SwiftUI 架构

发布于 2025-01-15 18:19:23 字数 184 浏览 2 评论 0原文

我正在编写我的第一个 SwiftUI 应用程序,它需要在后台执行各种任务;例如偶尔从 API 获取数据、响应 Web 套接字事件等。

我的问题是,启动此类后台任务的最佳位置在哪里?一种想法是任务块挂在 WindowGroup 上,但我确实没有正确的线索。任何人都可以推荐有关 SeiftUI 应用程序架构的资源吗?我正在遵循 MVI 模式。

I am writing my first SwiftUI app which needs to be performing various tasks in the background; such as occasionally fetching data from an API, responding to web socket events, stuff like that.

My question is, where is the best place to kick off such background tasks? One thought is Task block hanging off the WindowGroup but I really have no proper clue. Any resources anyone can recommend about the architecture of SeiftUI apps? I’m following the MVI pattern.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路弥 2025-01-22 18:19:23

您可以在视图中使用新的 .task() 修饰符。这是异步下载图像的示例:

struct MyView: View {

    @State private var myImage: UIImage? = nil
    
    var body: some View {
        Group {
            if myImage == nil {
                ProgressView()
            } else {
                Image(uiImage: myImage!)
                    .resizable()
                    .scaledToFit()
            }
        }

        // The code in this closure will run asynchronously
        // when the view launches
        .task {

            // Create your usual URLRequest in some function
            // and use it here
            // (URLRequest to return data)
            let downloadedImage = await UIImage(data: myURLRequestHere())) ?? UIImage()

            // This code will run only after downloadedImage has finished
            // the task marked with "await"
            updateImageWith(image: downloadedImage)
        }
    }

    // Using @MainActor ensures that you are updating the
    // UI in the main thread
    @MainActor func updateImageWith(image: UIImage) {
        withAnimation {
            myImage = image
        }
    }
}

You can use the new .task() modifier to the view. Here's an example of downloading an image asynchronously:

struct MyView: View {

    @State private var myImage: UIImage? = nil
    
    var body: some View {
        Group {
            if myImage == nil {
                ProgressView()
            } else {
                Image(uiImage: myImage!)
                    .resizable()
                    .scaledToFit()
            }
        }

        // The code in this closure will run asynchronously
        // when the view launches
        .task {

            // Create your usual URLRequest in some function
            // and use it here
            // (URLRequest to return data)
            let downloadedImage = await UIImage(data: myURLRequestHere())) ?? UIImage()

            // This code will run only after downloadedImage has finished
            // the task marked with "await"
            updateImageWith(image: downloadedImage)
        }
    }

    // Using @MainActor ensures that you are updating the
    // UI in the main thread
    @MainActor func updateImageWith(image: UIImage) {
        withAnimation {
            myImage = image
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文