【问题标题】:Firebase Sort array of class by valueFirebase 按值对类数组进行排序
【发布时间】:2017-04-18 12:32:04
【问题描述】:

我正在使用 Firebase。在我的应用程序中,我通过传入一个 bottleID 来获取一个子值,并从快照中获取该值的详细信息。然后我将详细信息分配给MyCollection_Class 的对象并将其添加到数组中。在获得每个瓶子值之后,我想在重新加载表格视图之前使用 created_at 标记对该数组进行排序。请告诉我如何按特定实例变量对对象数组进行排序。

let Collection = MyCollection_Class()
FireBaseConstants.AUCTIONS_REF.child(bottleID).observeSingleEvent(of: .value, with: { (snap) in
    if !(snap.value is NSNull) {
        Collection.id = bottle_dict["id"] as? String
        Collection.item_number = bottle_dict["item_number"] as? Int
        Collection.created_at = bottle_dict["created_at"] as? String
        if !(self.MyCollectionsIDArr.contains(Collection.id! as String)) {
             self.MyCollectionsArr.append(Collection)
             self.MyCollectionsIDArr.append(Collection.id!)
             // I want to sort the MyCollectionsArr using created_at here
             self.tbl_Latest.reloadData()
        }
    }
})

【问题讨论】:

标签: ios firebase swift3 firebase-realtime-database


【解决方案1】:

您可以使用

检索已从 Firebase 排序的数据

queryOrderedByChild。

一个例子是:

ref.child(bottleID).queryOrderedByChild("created_at").queryEqualToValue(0).observe  SingleEventOfType(.Value, withBlock: { snap in
    print("snap \(snap)")
    expectation.fulfill()
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多