简体   繁体   English

从Firebase读取数据并将其保存到阵列中(快速)

[英]Read Data from Firebase and Save Into an Array (Swift)

I've been going around in circles about something that seems really simple, but I can't figure out it out. 我一直在兜圈子一些看起来很简单的事情,但我无法弄清楚。 I simply want to read data from Firebase and save it in array so I can use it in my app. 我只想从Firebase读取数据并将其保存在数组中,以便可以在我的应用程序中使用它。 I know how to read the data as the following works for me to print out in the console: 我知道如何读取数据,因为以下内容可以在控制台中打印出来:

 var ref = Firebase(url: "<MYFIREBASEURL>")

 ref.observeEventType(.ChildAdded, withBlock: { snapshot in

        print(snapshot.value.objectForKey("title"))

    })

I tried some of these approaches to save to an array, but I can't get it to work as it doesn't directly address my simpler question. 我尝试了其中的一些方法来保存到数组中,但是由于它不能直接解决我的简单问题,因此我无法使其工作。

Adding Firebase data into an array 将Firebase数据添加到阵列中

How to save data from a Firebase query 如何从Firebase查询保存数据

Thanks for your help! 谢谢你的帮助!

This is how I am retrieving values and appending to array with Firebase. 这就是我使用Firebase检索值并将其附加到数组的方式。 REF_POSTS is a url based reference to my posts object in Firebase. REF_POSTS是对Firebase中我的posts对象的基于URL的引用。 Remember that firebase objects are essentially dictionaries and you need to parse the data out of it and assign it to variables in order to use them. 请记住,firebase对象本质上是字典,您需要解析其中的数据并将其分配给变量才能使用它们。

    var posts = [Post]()// put this outside of viewDidLoad

    //put the below in viewDidLoad
    DataService.ds.REF_POSTS.observeEventType(.Value, withBlock: { snapshot in
        print(snapshot.value)  
        self.posts = []
        if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {
            for snap in snapshots {
                if let postDict = snap.value as? Dictionary<String, AnyObject> {
                    let key = snap.key
                    let post = Post(postKey: key, dictionary: postDict)
                    self.posts.append(post)
                } 
            }
        }
        self.postTableView.reloadData() 
    })

I figured it out after sleeping on it. 我睡在上面想通了。 Posting here to make it easier for the next person to figure out. 张贴在这里可以让下一个人更容易找到。

 // Class variables
 var ref = Firebase(url: "https://<MYFIREBASEURL>")
 var titlesArray = [String]()

 // Under viewDidLoad

 // "events" is the root, and "title" is the key for the data I wanted to build an array with.
 let titleRef = self.ref.childByAppendingPath("events")
    titleRef.queryOrderedByChild("title").observeEventType(.ChildAdded, withBlock: { snapshot in

        if let title = snapshot.value["title"] as? String {
            self.titlesArray.append(title)

            // Double-check that the correct data is being pulled by printing to the console.
            print("\(self.titlesArray)")

            // async download so need to reload the table that this data feeds into.
            self.tableView.reloadData()
        }
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM