UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Getting data over the network for a screen

Forums > SwiftUI

If I have two screens where one is a list of users and the second is a detail screen with that users's information, what is the best way to get the detailed information and display it. Currently I'm making a network call to get the list of user's and displaying it in a list. When tapping on a user I'm navigating to the next screen an onAppear i'm making a network call to get the user's detailed info and display it however I'm using State to hold the user's info and when the screen initially loads that info doesn't exist so the state is an optional. I figured another option is to get the user's info when they are tapped and then pass it to the screen to display it. Most tutorials only show getting data to populate a list. Is there a "best" way to handle this scenario?

3      

Firstly depends if you are using the data else where in your app then you need to use @ObservedObject or @EnvironmentObject, however in this case

Have a var user: User in the detail screen

then have a NavigationLink(destination: DetailView(user: user) in the List/ForEach.

List {
    ForEach(users, id: \.id) { user in
        NavigationLink(destination: DetailView(user: user)) {
            Text(user.name)
                .font(.title)
        }
    }
}

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.