将观察值转换为swift中的.childAdded [英] Convert observe .value to .childAdded in swift

查看:147
本文介绍了将观察值转换为swift中的.childAdded的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在将这些代码从firebase数据库加载到数组中。由于这是在viewDidLoad里面,我必须清空我的数组食品= [] 之前加载数据到它,如果我不这样它就会复制所有的对象,我将有双重复第二次加载,第三次三倍等...但是,这不是一个很好的解决多种原因,所以我想是它只会添加新的对象从数据库与 .childAdded 然而,如果我只是用 .childAdded 切换 .value ,它会崩溃,我得到一个线程1:信号SIGABRT 在这一行: let dict = user_snap.value as! [String:String?] 。我很新,迅速,不知道如何解决这个问题,真的很感谢一些帮助。
$ b $ pre $ let parentRef =数据库.database()。reference()。child(Recipes)
let storage = Storage.storage()

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

if(snapshot.value is NSNull){

// DATA未找到
print( - - - Data was found - - - )

} else {

//清除数组,使其不会加载重复数据
food = []

//数据发现$ b (snapshot.children){

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

//定义标签变量
let recipeName = dict [Name] as?String
let recipeDescription = dict [Description] as?String
让downloadURL = dict [Image]为?字符串

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

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

let recipeImage = UIImage(data:data!)
$ b food.append(元素(名称:recipeName !,描述:recipeDescription !,图像:recipeImage!))
self.tableView.reloadData()
}
}
}
})


解决方案

 让dict = user_snap.value as! [字符串:字符串?] 

而不是

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

也许你可以做空测试:

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

如果让recipeName = dict [Name]作为字符串!,让recipeDescription = dict [Description] as String !,让downloadURL = dict [Image] as String!
{
let storageRef = storage.reference(forURL:downloadURL)
$ b $ storageRef.getData(maxSize:1 * 1024 * 1024){(data,error) - >无效

let recipeImage = UIImage(data:data!)

food.append(元素(名称:recipeName,描述:recipeDescription,图像:recipeImage !, downloadURL:downloadURL ))
self.tableView.reloadData()
}
}
else
{
print(Error!Could not decode data)
}


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

解决方案

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

这篇关于将观察值转换为swift中的.childAdded的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆