I'm trying to sort my data by date, but it seems like the data comes out random. Can anyone tell me why my queryOrdered(byChild: "startDate")
doesn't work? And how i get the data sorted?
You can see my data structure here: Complex Query
Code:
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()
})
}
}
})
Period:
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)
}
}