0

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)
    }
}
Community
  • 1
  • 1
Grumme
  • 794
  • 2
  • 9
  • 31

1 Answers1

1

Change your dataStructure.

Keep an index of your logs of like:-

logs
 -KVMbJKN1i2vAxzutiYq
   duration: 14
   index : 1 
   startDate: 28/10/2016

 -KVMbLL4i_9dwaRZ9REB
   duration: 28
   index : 2
   startDate: 01/09/2016

 -KVMiLwoSY34TZpf8mST
   duration: 14
   index : 3
   startDate: 2/2/2016
totalNoOfLogs : 3

If you are using struct before reloading your data in your tableView just sort the

self.periods.sort(by: {$0.index_Initial > $1.index_Initial})

Your struct:-

struct Period {

var _periodId: String!
var _startDate: String!
var _duration: Int!
var index_Initial : Int!

 ....}

Where index_Initial is your index of logs. Retrieved and stored into your struct.

For index you can keep a separate node in your database keeping track of the totalNoOfLogs and increment it every time your user makes a logs.

or you can try:-

FIRDatabase.database().reference().child("periods").observeSingleEvent(of: .value, with: {(userSnap) in

        for each in userSnap.children{

            if let eachSnapDict = (each as! FIRDataSnapshot).value as? [String:AnyObject]{


                print(eachSnapDict)

            }
        }
    })

But this approach will only work if you have the data stored in your database in a sorted manner(not a preffered option)

Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • Thanks for your answer. But can you tell me how the totalNoOfLogs is going to work, since the logs is for all users. How should i keep count of the logs for the specific user if i'm going to save it to the database? @Dravidian – Grumme Nov 03 '16 at 20:57
  • @Dravidan - I can see it's sorted now, by it's only by the index i assigned to it. But how to sort it by the date? – Grumme Nov 03 '16 at 21:59
  • Change the date format you save to **timestamp** and then use that instead of the index – Dravidian Nov 04 '16 at 07:03