c0nman
c0nman

Reputation: 309

How do I present an alert from a custom class in swiftui?

In SwiftUI, I have a network request running in scenedelegate, scenedidbecomeactive. I don't know which view the user will be on when the app becomes active, but I want to present an alert if the data in the network request changes. I simplified the code below, so it's easy to read...

 func sceneDidBecomeActive(_ scene: UIScene) {
            let customClass = CustomClass()
            customClass.performNetworkRequest()

In CustomClass, i have...

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?
}

Any help is much appreciated!

Upvotes: 1

Views: 1335

Answers (1)

akim
akim

Reputation: 93

Paul has a point - here's a possible implementation:

// In CustomClass.swift

import Combine

class CustomClass : ObservableObject {

    @Published var dataRecieved = PassthroughSubject<DetailResponse, Never>()

    init() {
        performNetWorkRequest()
    }

    func performNetWorkRequest() {
        URLSession.shared.dataTask(with: url) { (data, response, error) in

            let response = try JSONDecoder().decode(DetailResponse.self, from: data)

            DispatchQueue.main.async {
                self.dataRecieved.send(response)
            }
        }
        .resume()
    }
}

// In SomeView.swift

import SwiftUI
import Combine

struct ContentView: View {

    @State var showAlert = false
    var customClass = CustomClass()

    var body: some View {
        Text("Hello, World!")
            .onReceive(customClass.dataRecieved) { _ in
                self.showAlert = true
            }
            .alert(isPresented: $showAlert) {
            // your alert
            }
    }
}

Notice I didn't mention the SceneDelegate in any of these - this approach (called MVVM) is more flexible, in my opinion - besides, the way it is set up, performNetWorkRequest() will be executed as soon as your view is initialized, anyway. You can also tweak the PassthroughSubject - I didn't know if you needed the DetailResponse or not. Hope this helped!

Edit:

I just reread your question and it seems that this implementation is at fault as you noted there was no way to know what view the user would be on in the case of a network change. In that case, you can feed the same instance of CustomClass in your SceneDelegate as an EnvironmentObject.

Upvotes: 1

Related Questions