Populating firebase information into tableView

浪子不回头ぞ 提交于 2020-06-17 04:21:25

问题


Hi I am wanting to populate some data from my firebase to my tableView but do not really understand how to go about doing it. Below is a snippet from my code detailing where the data is coming from and the current tableView arrangement I have:

Edited Attempt

func fetchPosts(completion:@escaping (_ theposts:[Posts])->()){




      oldPostQuery.queryLimited(toLast: 20).observeSingleEvent(of: .value, with: { snapshot in

          var tempPosts = [Posts]()

          let lastPost = self.theposts.last
          for child in snapshot.children {
          if let childSnapshot = child as? DataSnapshot,
              let data = childSnapshot.value as? [String:Any],
              let theposts = Posts.parse(childSnapshot.key, data),
              childSnapshot.key != lastPost?.id {
                  guard let user = Auth.auth().currentUser else { return }
                         let uid = user.uid
                         let ref = Database.database().reference().child("posts")
                         let query = ref.queryOrdered(byChild: "author/uid").queryEqual(toValue: uid)
                         query.observeSingleEvent(of: .value, with: { snapshot in
                             let allPosts = snapshot.children.allObjects as! [DataSnapshot]
                             for postSnap in allPosts {

                                 let textUser = postSnap.childSnapshot(forPath: "text").value as? String ?? "No Text"
                                 print(textUser)
                                //same as above
                             }
                         })

                      tempPosts.insert(theposts, at: 0)
              }



          }

          return completion(tempPosts)
      } )

  }

I am aware that the tableView I am using is empty cell.set(theposts: theposts[indexPath.row]) but I was using this prior as this is the only way I know how to get the data. The information appears in my console but does not populate on the display.

来源:https://stackoverflow.com/questions/62199497/populating-firebase-information-into-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!