Swift3 Firebase检索数据

时间:2016-10-13 16:02:12

标签: ios swift firebase firebase-realtime-database

我正在努力从我的Firebase获取一些数据。这就是结构的样子:

enter image description here

这是我用以下代码检索数据的代码:

var followDatesDict = [String:String]()

databaseRef.child("FollowGroups").child(selectedFollowGroupId as String).observeSingleEvent(of: .value, with: { (snapshot) in
                if snapshot.hasChildren(){

                    let name = snapshot.childSnapshot(forPath: "followgroupTitle").value as! String
                    let sDate = snapshot.childSnapshot(forPath: "startDate").value as! String
                    let eDate = snapshot.childSnapshot(forPath: "endDate").value as! String

                    followDatesDict = snapshot.childSnapshot(forPath: "followDates")......



                }
            })

我可以检索namesDateeDate就好了。但有谁知道我如何填写按日期排序的followDatesDict?正如您在followDates的结构中看到的那样,键是一个日期字符串和一个字符串值。

谢谢!

1 个答案:

答案 0 :(得分:3)

好的,我设法得到了数据。我还将它们添加到结构中。

let enumerator = snapshot.childSnapshot(forPath: "followDates").children
                    while let rest = enumerator.nextObject() as? FIRDataSnapshot {
                        let key_followDate = rest.key
                        let value_UserId = rest.value as! String

                        //add to dictionary
                        self.followDatesDict.insert(followgroupOverview(followDate:key_followDate, followUserId:value_UserId), at: 0)
                    }