按日期对Firebase数据排序

时间:2016-12-30 07:29:58

标签: ios swift firebase firebase-realtime-database

我一直收到此错误,但无法调试:

  

无法将'FIRDatabaseQuery'(0x10b32b700)类型的值转换为'FIRDatabaseReference'(0x10b32b520)。

该错误来自常规.swift文件:

import Foundation
import Firebase
import FirebaseDatabase

let DB_BASE = FIRDatabase.database().reference()

class DataService {

    static let ds = DataService()

    private var _REF_BASE = DB_BASE
    private var _REF_INCOMES = DB_BASE.child("incomes").queryOrdered(byChild: "date")
    private var _REF_USERS = DB_BASE.child("users")

    var REF_BASE: FIRDatabaseReference {
        return _REF_BASE
    }

    var REF_INCOMES: FIRDatabaseReference {
        return _REF_INCOMES as! FIRDatabaseReference // Thread 1: signal SIGABRT
    }

    [...]
}

在添加.queryOrdered(byChild: "date")as! FIRDatabaseReference之前,一切正常,但我无法按日期排序。

class IncomeFeedVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    var incomes = [Income]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self

        DataService.ds.REF_INCOMES.observe(.value, with: { (snapshot) in

            if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
                for snap in snapshot {
                    if let incomeDict = snap.value as? Dictionary<String, AnyObject> {
                        let key = snap.key
                        let income = Income(incomeId: key, incomeData: incomeDict)
                        self.incomes.append(income)
                    }
                }
            }
            self.tableView.reloadData()
        })
    }

   [...]

}

我在追求什么?首先,我需要对日期进行排序,然后按照草图视图进行操作:

enter image description here

你怎么排序?我看到的教程很少使用CoreData。我正在使用Firebase。

1 个答案:

答案 0 :(得分:2)

您的FIRDatabaseQuery FIRDatabaseReference不是var REF_INCOMES: FIRDatabaseQuery { return _REF_INCOMES } ..

{{1}}

请检查此document以对数组进行排序