NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

SOLVED: SwiftUI Picker desn't bind with ObservedObject

Forums > SwiftUI

I'm trying to fill up a Picker with data fetched asynchronously from external API.

This is my model:

struct AppModel: Identifiable {
    var id = UUID()
    var appId: String
    var appBundleId : String
    var appName: String
    var appSKU: String
}

The class that fetches data and publish is:

class AppViewModel: ObservableObject {
    private var appStoreProvider: AppProvider? = AppProvider()
    @Published private(set) var listOfApps: [AppModel] = []
    @Published private(set) var loading = false

    fileprivate func fetchAppList() {
        self.loading = true
        appStoreProvider?.dataProviderAppList { [weak self] (appList: [AppModel]) in
            guard let self = self else {return}
            DispatchQueue.main.async() {
                self.listOfApps = appList
                self.loading = false
            }
        }
    }
    init() {
        fetchAppList()
    }
}

The View is:

struct AppView: View {
    @ObservedObject var appViewModel: AppViewModel = AppViewModel()
    @State private var selectedApp = 0

    var body: some View {
        ActivityIndicatorView(isShowing: self.appViewModel.loading) {
            VStack{
                // The Picker doesn't bind with appViewModel
                Picker(selection: self.$selectedApp, label: Text("")) {
                    ForEach(self.appViewModel.listOfApps){ app in
                        Text(app.appName).tag(app.appName)
                    }
                }
                // The List correctly binds with appViewModel
                List {
                    ForEach(self.appViewModel.listOfApps){ app in
                        Text(app.appName.capitalized)
                    }
                }
            }
        }
    }
}

While the List view binds with the observed object appViewModel, the Picker doesn't do the same staying empty. I can't realize why. Any help ?

   

I guess Picker has got a refressh issue, because adding .id(UUID()) to the Picker block:

          Picker(selection: self.$selectedApp, label: Text("")) {
              ForEach(self.appViewModel.listOfApps){ app in
                  Text(app.appName).tag(app.appName)
              }
          }.id(UUID())

it forces a refresh of the Picker view, showing the list of apps.

   

Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.