Giuseppe Roberto Rossi
Giuseppe Roberto Rossi

Reputation: 796

SwiftUI Picker desn't bind with ObservedObject

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 behave in the same way. I can't realize why. Any help ?

Upvotes: 3

Views: 3145

Answers (2)

Rob
Rob

Reputation: 438287

I filed bug report, FB7670992. Apple responded yesterday, suggesting that I confirm this behavior in iOS 14, beta 1. It appears to now have been resolved.

struct ContentView: View {
    @ObservedObject var viewModel = ViewModel()

    var body: some View {
        Picker("", selection: $viewModel.wheelPickerValue) {
            ForEach(viewModel.objects) { object in
                Text(object.string)
            }
        }
        .pickerStyle(WheelPickerStyle())
        .labelsHidden()
    }
}

Where

struct Object: Identifiable {
    let id = UUID().uuidString
    let string: String
}

class ViewModel: ObservableObject {
    private var counter = 0

    @Published private(set) var objects: [Object] = []
    @Published var segmentedPickerValue: String = ""
    @Published var wheelPickerValue: String = ""

    fileprivate func nextSetOfValues() {
        let newCounter = counter + 3
        objects = (counter..<newCounter).map { value in Object(string: "\(value)") }
        let id = objects.first?.id ?? ""
        segmentedPickerValue = id
        wheelPickerValue = id
        counter = newCounter
    }

    init() {
        let timer = Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { [weak self] timer in
            guard let self = self else { timer.invalidate(); return }
            self.nextSetOfValues()
        }
        timer.fire()
    }
}

Results in:

enter image description here

Upvotes: 3

lorem ipsum
lorem ipsum

Reputation: 29656

I can't put this into your code because it is incomplete but here is a sample. Pickers aren't meant to be dynamic. They have to be completely reloaded.

class DynamicPickerViewModel: ObservableObject {
    @Published private(set) var listOfApps: [YourModel] = []
    @Published private(set) var loading = false

    fileprivate func fetchAppList() {
            loading = true
            DispatchQueue.main.async() {
                self.listOfApps.append(YourModel.addSample())
                self.loading = false
            }

    }
    init() {
        fetchAppList()
    }

}
struct DynamicPicker: View {
    @ObservedObject var vm = DynamicPickerViewModel()
    @State private var selectedApp = ""

    var body: some View {
        VStack{
            //Use your loading var to reload the picker when it is done
            if !vm.loading{
                //Picker is not meant to be dynamic, it needs to be completly reloaded
                Picker(selection: self.$selectedApp, label: Text("")) {
                    ForEach(self.vm.listOfApps){ app in
                        Text(app.name!).tag(app.name!)
                    }
                }
            }//else - needs a view while the list is being loaded/loading = true


            List {
                ForEach(self.vm.listOfApps){ app in
                    Text(app.name!.capitalized)
                }
            }

            Button(action: {
                self.vm.fetchAppList()
            }, label: {Text("fetch")})

        }
    }
}

struct DynamicPicker_Previews: PreviewProvider {
    static var previews: some View {
        DynamicPicker()
    }
}

Upvotes: 0

Related Questions