【发布时间】:2016-11-03 20:02:19
【问题描述】:
我正在尝试按日期对数据进行排序,但数据似乎是随机出现的。谁能告诉我为什么我的queryOrdered(byChild: "startDate") 不起作用?以及我如何对数据进行排序?
你可以在这里看到我的数据结构: Complex Query
代码:
DataService.ds.REF_USER_CURRENT.child("logs").observeSingleEvent(of: .value, with: {(userSnap) in
if let SnapDict = userSnap.value as? [String:AnyObject]{
//Run through all the logs
for each in SnapDict{
FIRDatabase.database().reference().child("logs/\(each.key)").queryOrdered(byChild: "startDate").observeSingleEvent(of: .value , with : {(Snap) in
let period = Period(logId: each.key, postData: Snap.value as! Dictionary<String, AnyObject>)
self.periods.append(period)
print(period.duration)
print(period.startDate)
self.tableView.reloadData()
})
}
}
})
期间:
struct Period {
var _periodId: String!
var _startDate: String!
var _duration: Int!
var logs = [Log]()
var _periodRef: FIRDatabaseReference!
init() {
}
init(logId: String, postData: Dictionary<String, AnyObject>) {
self._periodId = logId
if let startDate = postData["startDate"] {
self._startDate = startDate as? String
}
if let duration = postData["duration"] {
self._duration = duration as? Int
}
_periodRef = DataService.ds.REF_PERIODS.child(_periodId)
}
}
【问题讨论】:
-
你在使用结构吗,如果是的话,用你的结构更新你的问题..
-
完成@Dravidian :)
-
当您将快照转换为字典时,它的顺序变得不确定。见stackoverflow.com/questions/40166483/…
标签: ios swift xcode firebase firebase-realtime-database