Array of struct not updating outside the closure

后端 未结 2 1607
小蘑菇
小蘑菇 2021-01-25 20:27

I have an array of struct called displayStruct

struct displayStruct{
let price : String!
let Description : String!
} 

I am reading

2条回答
  •  无人共我
    2021-01-25 20:53

    Firebase observe call to the database is asynchronous which means when you are requesting for the value it might not be available as it might be in process of fetching it.

    That's why your both of the queries to count returns 0 in viewDidLoad and DataSource delegeate method.

      databaseRef.child("Post").queryOrderedByKey().observe(.childAdded, with:  { // inside closure }
    

    Inside the closure, the code has been already executed and so you have the values.

    What you need to do is you need to reload your Datasource in main thread inside the closure.

       databaseRef.child("Post").queryOrderedByKey().observe(.childAdded, with:  { 
           // After adding to array
           DispatchQueue.main.asyc {
               self.tableView.reloadData()
           } 
    
        }
    

提交回复
热议问题