Code only retrieving one value from data in Firebase

后端 未结 2 1646
情歌与酒
情歌与酒 2021-01-24 15:31

As the title suggests, I\'m trying to retrieve some data from firebase database, but my code\'s not working. I have three children (I guess that\'s how you call them) inside \"P

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-24 16:14

    Try this – the code replaces what you currently have in the snapshot handler.

    if let firebaseList = snapshot.children.allObjects as? [FIRDataSnapshot] {
            if let swiftList = snapshot.value as? [String:AnyObject] {
                for firebaseItem in firebaseList {
                    let childID = firebaseItem.key as String
                    let swiftItem = swiftList[childID]
                    let postTitle = swiftItem?["title"] as? String
                    let postUsername = swiftItem?["username"] as? String
                    let postDesc = swiftItem?["description"] as? String
                    let postArray = postStruct(title: postTitle, description: postDesc, username: postUsername)
                    self.newPost.insert(postArray, at: 0)
                }
            }
        }
    }
    

提交回复
热议问题