Swift如何从Firebase排序数据?

时间:2018-11-22 16:02:26

标签: swift firebase uitableview firebase-realtime-database

我在Firebase中具有以下结构:

"OpeHS4UhH6YWcK4aPHJWumueCQP2" : {
      "Orders" : {
        "Date" : {
          "-LRw0wJMtbYQmy34F3SS" : 1542900401608,
          "-LRw1-MpaWeQkCxkV08_" : 1542900418212,
          "-LRw11xb7PhD-iYPTz8P" : 1542900428823,
          "-LRw13xl0p3gxOIsmKdI" : 1542900437024
        },
        "Order" : {
          "-LRw0wJMtbYQmy34F3SR" : "1 Orange ",
          "-LRw1-Mo8sXFCugSzSuN" : "2 Banana ",
          "-LRw11xb7PhD-iYPTz8O" : "3 Apple ",
          "-LRw13xkb9z5TsCg5eUq" : "4 Tomato "
        }
      }
    }    

首先写入1个橙色,然后写入2个香蕉,依此类推。在Firebase中,数据处于完美的顺序。
 同样,Date节中的时间戳以相同的顺序编写。
但是,当我从Firebase读取并放入tableView中时,时间戳是按顺序排列的,最旧的位于底部,最新的位于顶部。但是Order节似乎是随机添加的?
Screenshot of tableView

数据读取如下:

class OrderHistoryTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var orderDateHistoryArray = [Any]()
    var orderItemsHistoryArray = [String]()


    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        tableView.estimatedRowHeight = 80
        tableView.rowHeight = UITableView.automaticDimension
    }

    override func viewDidAppear(_ animated: Bool) {
        getOrderHistory()
        getOrderDates()
    }


     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return orderItemsHistoryArray.count

    }



     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "OrderHistoryCell") as! OrderHistoryTableViewCell

        cell.orderHistoryItemsLabel.text = orderItemsHistoryArray[indexPath.row]
        cell.orderHistoryDateLabel.text = "\(orderDateHistoryArray[indexPath.row])"

        return cell

    }


    func getOrderHistory() {
        //Get userinfo from database

        let uid = Auth.auth().currentUser!.uid
        let orderDateHistoryRef = Database.database().reference().child("users/\(uid)/Orders/")

        orderDateHistoryRef.observeSingleEvent(of: .value, with: { (snapshot) in

            let value = snapshot.value as? NSDictionary
            if  let orderItem = value?["Order"] as? [String:String] {
                self.orderItemsHistoryArray += Array(orderItem.values)
                //print(orderItem)
            }
            self.tableView.reloadData()

            // ...
        }) { (error) in
            print(error.localizedDescription)
        }
    }

    func getOrderDates() {

        let uid = Auth.auth().currentUser!.uid
        let orderDateHistoryRef = Database.database().reference().child("users/\(uid)/Orders/")

        orderDateHistoryRef.observeSingleEvent(of: .value, with: { (snapshot) in

            let value = snapshot.value as? NSDictionary
            if  let orderDate = value?["Date"] as? [String:Int] {

                let dates = orderDate.values.map {Date(timeIntervalSince1970: TimeInterval ($0/1000)) }
                let formatter = DateFormatter()
                formatter.calendar = Calendar(identifier: .iso8601)
                formatter.locale = Locale(identifier: "nb_NO")
                formatter.timeZone = TimeZone(secondsFromGMT: 0)
                formatter.dateFormat = "E, d MMM yyyy HH:mm:ss Z"
                let readableDates = dates.map {formatter.string(from: $0)}
                self.orderDateHistoryArray = readableDates

                print(dates)
            }
            self.tableView.reloadData()
            // ...
        }) { (error) in
            print(error.localizedDescription)
        }
    }
}

0 个答案:

没有答案