SwiftUI Async data fetch in onAppear

后端 未结 1 820
醉梦人生
醉梦人生 2021-01-23 23:55

I have class getDataFromDatabase which has func readData() thats read data from Firebase.

class getDataFromDatabase : ObservableObject {         


        
相关标签:
1条回答
  • 2021-01-24 00:44

    So the problem is in your completion handler. It returns before you retrieve the data.

    Solution is to make your arrays @Published and read the data in real time from the view. You have to remove the completion handler.

    Call the function on ‚onAppear()‘ and use @ObservedObject to bind to your ViewModel (getDataFromDatabase). This is how it’s done in SwiftUI.

    Please capitalize the first letter and use something more generic like „YouViewName“ViewModel.

    Your name is fine for a method/function but not for a Class

    0 讨论(0)
提交回复
热议问题