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

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 ?

2      

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.

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.