简体   繁体   中英

Fetch data from firebase using childAdded in firebase

I'm trying to retrieve some data using the observe(.childAdded) in order to get the new "posts" of my project and add it to the tableview correctly. I actually got the result I need using this function.

        databaseHandle = ref?.child("Posts").observe(.childAdded, with: { (snapShot) in

        let post = snapShot.value as? String
        if let actualPost = post {

            self.postData.insert(actualPost, at: 0)
            self.tableView.reloadData()
        }

    })

But it only works for a child that has a value on it like this in FireBase:

*Root
  *Posts
     *ChildByAutoId: "post text value"

Actually what I'd like to have is a child with attributes like this:

*Root
  *Posts
    *ChildByAutoId 
       -Text: "post text value"
       -UserId: "userId"
       -CreationDate: "date"
       -PostId: "PostId"
       -Likes: Int
       -Author: "name"

I'd like to have the same result as using .observe(.childAdded) but I'm unable to do it. The only way I found that its currently working is this:

    func recuperarData(){
    let ref = self.ref

    ref?.child("Posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapShot) in
        if let posts = snapShot.value as? [String : AnyObject]{
            for (_, value) in posts{
                let post = Posts()

                if let autor = value["Author"] as? String ,let likes = value["Likes"] as? Int, let fecha = value["CreationDate"] as? String, let postId = value["PostID"] as? String, let texto = value["Text"] as? String, let userID = value["UserId"] as? String {
                    post.autor = autor
                    post.likes = likes
                    post.fecha = fecha
                    post.PostId = postId
                    post.texto = texto
                    post.UserId = userID
                    self.posts.append(post)                        
                }                    
            }
            self.tableView.reloadData()
        }            
    })        
    ref?.removeAllObservers()
} 

But this only retrieve all the data Once each time I "reload" the tableview and does not add the posts correctly in the order I want.

Does someone knows a correct way to fetch this data using .observe(.childAdded) so I can upgrade the tableview automatically.

I am using Xcode 8.2.1 and swift 3 Thanks

I am shocked no one was able to call out the issue in your original .childAdded function.

let post = snapShot.value as? String let post = snapShot.value as? String is the problem.

Firebase Database is a key-value pairing so you can't just cast the whole result into a String. You fixed this problem by casting it into a dictionary when doing .value . Do the same thing in the .childAdded function.

I had a similar answer explaining dictionary handling in Firebase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM