Tarvo Mäesepp Tarvo Mäesepp - 10 months ago 149
Swift Question

Firebase and Swift 3 code not executing anymore

I tried to convert my code

to Swift 3. I have to say that I had fully working project before. Now I have problem where I have no errors and just some warnings but some of the functions are not being executed. What should cause this?

I only assume that those given functions are faulty because these are the parts where I am not getting anything even

These are some of my functions that worked before but not with Swift 3:

//With this I get selected brand products values like product name, nicotine, flavor etc..
let ref = FIRDatabase.database().reference().child("Snuses").queryOrdered(byChild: "Brand").queryEqual(toValue: brandName)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists(){
if let products = (snapshot.value as AnyObject).allValues as? [[String:AnyObject]]{
self.productsValue = products

//With this fucntion I get the products count.
let ref = FIRDatabase.database().reference().child("Snuses").queryOrdered(byChild: "Brand").queryEqual(toValue: filteredBrands[indexPath.row])
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists(){
if let products = (snapshot.value as AnyObject).allValues as? [[String:AnyObject]]{

var count = (snapshot.childrenCount)
snusProductCountLabel.text = "\(count) products"


//Parse snus brands
func parseSnuses(){
let ref = FIRDatabase.database().reference().child("Brands").queryOrderedByKey()

ref.observe(.childAdded, with: { (snapshot) in
}){ (error) in


Anything I can do different please tell me! Those functions are in different

Edit: this is my JSON tree

"Snuses" : {
"Catch Eucalyptus White Large" : {
"Brand" : "Catch",
"Products" : "Catch Eucalyptus White Large",
"PorionWeight" : 21.6,
"flavor" : "Tobacco, Eucalyptus",
"nicotine" : 8.0,
"PortionsCan" : 24,
"shipping weight" : 39

And these are security rules:

"rules": {
".read": "true",
".write": "true",
"Snuses": {
".indexOn": "Brand"

Jay Jay
Answer Source

I believe the

if let products = (snapshot.value as AnyObject)
                   .allValues as? [[String:AnyObject]]{

is the issue.

Try this as a test to see if it prints the data from the snapshot:

 let ref = FIRDatabase.database().reference().child("Snuses")
               .queryOrdered(byChild: "Brand").queryEqual(toValue: brandName)
      ref.observeSingleEvent(of: .value, with: { (snapshot) in
           if snapshot.exists() {

                let dict = snapshot?.value as! [String: [String:String]]
                let productsArray = Array(dict)

                for row in productsArray {

for a non-swifty test, you can also try this inside the closure instead of the above

let d2 = snapshot?.value as! NSDictionary
let a2 = d2.allValues

for r2 in a2 {

one more option:

let q = snapshot?.value as! [String: AnyObject]
let a3 = Array(q)

for r3 in a3 {

I don't know what your tableView is expecting in the array but one of those should cover it.