1

I need to arrange my firebase data by date (unix). I thought queryOrdered(byChild: "date") would do the trick. Done a search and found this which makes sense:

But when you request the .value of the snapshot, the keys+data are converted to a Dictionary. Since a dictionary does not have an extra place to put the information about the order, that information is lost when converting to a dictionary.

Using the same json but modified with unix dates...:

{
  "users" : {
    "alovelace" : {
      "name" : "Last Year",
      "date" : 1480550400
    },
    "eclarke" : {
      "name" : "New Year Now",
      "date" : 1483228800
    },
    "ghopper" : {
      "name" : "New Year",
      "date" : 1483228800
    }
  }
}

... how to sort when my code is like this:

DataService.ds.REF_INCOMES.queryOrdered(byChild: "date").observe(.value, with: { (snapshot) in

    if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
        print(snapshot)
        for snap in snapshot {
            if let incomeDict = snap.value as? [String: AnyObject] { // What needs to change here?
                let key = snap.key
                let income = Income(incomeId: key, incomeData: incomeDict)
                self.incomes.append(income)
                self.incomes.reverse()
            }
        }
    }
    self.tableView.reloadData()
})

The image below, "Last Year" should be last but it's not:

enter image description here

I have a ruby mentality so Im lost with swift. Thanks.

Community
  • 1
  • 1
Sylar
  • 11,422
  • 25
  • 93
  • 166

2 Answers2

0

I think what you're missing here is the sort function, Please try the code below and let me know what are the results:

DataService.ds.REF_INCOMES.queryOrdered(byChild: "date").observe(.value, with: { (snapshot) in

    guard let usersSnapshot = snapshot.value as? [String:NSObject] else{

        return
    }

    let users = usersSnapshot["users"] as! [String:AnyObject]
    let sorted = users.sorted{($0.0.value["date"] as! Double) > ($0.1.value["date"] as! Double)}


    print(sorted) // It should be: New Year Now, New Year, Last Year
})
Jad
  • 2,139
  • 1
  • 16
  • 28
  • on line `let users`, I got `fatal error: unexpectedly found nil while unwrapping an Optional value` – Sylar Jan 01 '17 at 17:38
  • try `print(usersSnapshot)` before error line and see what does contains – Jad Jan 01 '17 at 17:39
  • It return the data. – Sylar Jan 01 '17 at 17:43
  • I'm loading your JSON in Playground and using the same code above...it works fine...weird. Does the data contains the "users" key when you print it? – Jad Jan 01 '17 at 17:46
  • Ok, How to assign `sorted` to my class: `var incomes = [Income]()`? I could with `self.incomes.append(income)` – Sylar Jan 01 '17 at 19:46
  • what is `Income`? – Jad Jan 01 '17 at 20:06
  • Ive sorted it. Took me awhile to figure what's going on Ive: `for snap in sorted {...}` In the curley brackets, my same code applies but I've used your `sorted` as the object to loop on. Thanks. – Sylar Jan 01 '17 at 20:08
0

I believe the problem is that you are observing by .value, which essentially ignores the order by. Try to observe by .childAdded, which does respect the order by operation.

For more info, read the first "Pro Tip": https://howtofirebase.com/collection-queries-with-firebase-b95a0193745d

C. Bess
  • 567
  • 7
  • 10