Marian Petrisor
Marian Petrisor

Reputation: 302

Adding Firestore to my project give an error to my Realtime Database Queries

I've been using Firebase Real-time Database in my project for quite some time now. Recently I added the pod for Firestore to use it together with the other database and all my queries and only the queries for Real-time Database are getting the following error: "Ambiguous use of 'subscript'".

If I uninstall the pod everything comes back to normal with absolutely no errors. This is how I was using the code before: (working)

func retriveObjects() {
    ref.child("Dinner").queryOrderedByKey().observeSingleEvent(of: .value) { (snapshot) in
        let allPosts = snapshot.value as! [String : AnyObject]
        for (_, item) in allPosts {
            if let itemCount = item["itemCount"] as? String {
                print(itemCount)
            }
        }
    }
}

Now this line is giving me the error:

if let itemCount = item["itemCount"] as? String

What can I do to make it work again but keeping both databases?

Upvotes: 0

Views: 57

Answers (1)

Jay
Jay

Reputation: 35658

As mentioned in the comments, the value of the snapshot is being cast as a Dictionary of [String: AnyObject] and then you're trying to use item as a Dictionary but it's cast to AnyObject (which is not a dictionary). In other words item["itemCount"] doesn't work for AnyObject.

It's unclear what is throwing that error but we have a number of projects with both and it's not throwing that error. You may want to consider changing the code up a bit;

self.ref.child("Dinner").queryOrderedByKey().observeSingleEvent(of: .value) { (snapshot) in
    let allPosts = snapshot.children.allObjects as! [DataSnapshot]
    for post in allPosts {
        let itemCount = post.childSnapshot(forPath: "itemCount").value as? String ?? "No Count"
        print(itemCount)
    }
}

Upvotes: 1

Related Questions