Convert observe .value to .childAdded in swift

限于喜欢 提交于 2019-12-24 18:51:38

问题


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

回答1:


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



回答2:


try this. It should work

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

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

//.....
}


来源:https://stackoverflow.com/questions/46549312/convert-observe-value-to-childadded-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!