random1234
random1234

Reputation: 817

Convert observe .value to .childAdded in swift

Currently I'm having some problems with this bit of code that is loading data from firebase database into an array. Since this is inside of viewDidLoad I have to empty my array food = [] before loading the data into it, if I don't then it will duplicate all the objects and I will have double duplicates the second time it loads, triple the third time and etc... However this was not a good fix for multiple reasons so what I would like is that it would only add new objects from the database with .childAdded however if I just switch out .value with .childAdded it will crash, I get a Thread 1: signal SIGABRT on this line: let dict = user_snap.value as! [String: String?]. I am pretty new to swift and don't know how to fix this, would really appreciate some help.

let parentRef = Database.database().reference().child("Recipes")
let storage = Storage.storage()

parentRef.observe(.value, with: { snapshot in

        if ( snapshot.value is NSNull ) {

            // DATA WAS NOT FOUND
            print("– – – Data was not found – – –")

        } else {

            //Clears array so that it does not load duplicates
            food = []

            // DATA WAS FOUND
            for user_child in (snapshot.children) {

                let user_snap = user_child as! DataSnapshot
                let dict = user_snap.value as! [String: String?]

                //Defines variables for labels
                let recipeName = dict["Name"] as? String
                let recipeDescription = dict["Description"] as? String
                let downloadURL = dict["Image"] as? String

                let storageRef = storage.reference(forURL: downloadURL!)

                storageRef.getData(maxSize: 1 * 1024 * 1024) { (data, error) -> Void in

                    let recipeImage = UIImage(data: data!)

                    food.append(Element(name: recipeName!, description: recipeDescription!, image: recipeImage!))
                    self.tableView.reloadData()
                }
            }
        }
    })

Upvotes: 0

Views: 545

Answers (2)

Emre YILMAZ
Emre YILMAZ

Reputation: 152

let dict = user_snap.value as! [String: String?]

Instead of

let dict = snapshot.value as! Dictionary<String, String>

and maybe you can do null test :

let dict = snapshot.value as! Dictionary<String, String>

if let recipeName = dict["Name"] as String!, let recipeDescription = dict["Description"] as String!, let downloadURL = dict["Image"] as String! {
    let storageRef = storage.reference(forURL: downloadURL)

    storageRef.getData(maxSize: 1 * 1024 * 1024) { (data, error) -> Void in

        let recipeImage = UIImage(data: data!)

        food.append(Element(name: recipeName, description: recipeDescription, image: recipeImage!, downloadURL: downloadURL))
        self.tableView.reloadData()                        
    }                    
}else {
    print("Error! Could not decode data")                    
}

Upvotes: 3

Sai Sandeep
Sai Sandeep

Reputation: 149

try this. It should work

for child in snapshot.children.allObjects as! [FIRDataSnapshot] {

let dict = child.value as! Dictionary<String, Any>

//.....
}

Upvotes: 0

Related Questions