简体   繁体   中英

How to present a view after a request with URLSession in SwiftUI?

I want to present a view after I receive the data from a request, something like this

var body: some View {
          VStack {
            Text("Company ID")
            TextField($companyID).textFieldStyle(.roundedBorder)

            URLSession.shared.dataTask(with: url) { (data, _, _) in
                guard let data = data else { return }

                DispatchQueue.main.async {
                    self.presentation(Modal(LogonView(), onDismiss: {
                        print("dismiss")
                    }))
                }
            }.resume()
        }

    }

Business logic mixed with UI code is a recipe for trouble.

You can create a model object as a @ObjectBinding as follows.

class Model: BindableObject {

    var didChange = PassthroughSubject<Void, Never>()

    var shouldPresentModal = false {
        didSet {
            didChange.send(())
        }
    }

    func fetch() {
        // Request goes here
        // Edit `shouldPresentModel` accordingly
    }
}

And the view could be something like...

struct ContentView : View {

    @ObjectBinding var model: Model

    @State var companyID: String = ""

    var body: some View {
        VStack {
            Text("Company ID")
            TextField($companyID).textFieldStyle(.roundedBorder)
            if (model.shouldPresentModal) {
                // presentation logic goes here
            }
        }.onAppear {
            self.model.fetch()
        }
    }
}

The way it works:

  • When the VStack appears, the model fetch function is called and executed
  • When the request succeeds shouldPresentModal is set to true, and a message is sent down the PassthroughSubject
  • The view, which is a subscriber of that subject, knows the model has changed and triggers a redraw.
  • If shouldPresentModal was set to true, additional UI drawing is executed.

I recommend watching this excellent WWDC 2019 talk: Data Flow Through Swift UI

It makes all of the above clear.

I think you can do smth like that:

var body: some View {
        VStack {
            Text("Company ID")
        }
        .onAppear() {
            self.loadContent()
        }
    }

    private func loadContent() {
        let url = URL(string: "https://your.url")!
        URLSession.shared.dataTask(with: url) { (data, _, _) in
            guard let data = data else { return }
            DispatchQueue.main.async {
                self.presentation(Modal(ContentView(), onDismiss: {
                    print("dismiss")
                }))
            }
            }.resume()
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM