0

I keep getting this error and I failed to debug:

Could not cast value of type 'FIRDatabaseQuery' (0x10b32b700) to 'FIRDatabaseReference' (0x10b32b520).

That error comes from a regular .swift file with:

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
    }

    [...]
}

Before adding .queryOrdered(byChild: "date") and as! FIRDatabaseReference everything worked except that I could not get a sort by date.

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()
        })
    }

   [...]

}

What am I after? To start, I need to sort my date then work towards my Sketch view:

enter image description here

How do you sort? Few tutorials I see uses CoreData. Im using Firebase.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Sylar
  • 11,422
  • 25
  • 93
  • 166

1 Answers1

2

your private var _REF_INCOMES is FIRDatabaseQuery not FIRDatabaseReference ..

var REF_INCOMES: FIRDatabaseQuery {
    return _REF_INCOMES
}

And please check this Q&A to sort your array

Community
  • 1
  • 1
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
  • Ha! never saw that. Thanks. How to get ascending/descending? – Sylar Dec 30 '16 at 07:37
  • yes wait... I'll send you the link http://stackoverflow.com/questions/37144030/how-do-you-properly-order-data-from-firebase-chronologically – Bhavin Bhadani Dec 30 '16 at 07:40
  • 1
    Found it with `self.incomes.reverse()`, just after the append: http://stackoverflow.com/questions/36314374/swift-how-to-create-sort-query-as-descending-on-firebase – Sylar Dec 30 '16 at 07:46