简体   繁体   English

无法使用swift中添加的.Value / .Child从Firebase检索数据

[英]Trouble retrieving data from Firebase using .Value/.Child added in swift

i am working on a project where I am pulling data from firebase (which is "queued"). 我正在开发一个项目,我从firebase(“排队”)中提取数据。 Essentially data is being saved w/ a time stamp so when it's called, it can be ordered sequentially (first in, first out). 基本上数据是用时间戳保存的,因此当它被调用时,它可以按顺序排序(先进先出)。

The problem I am facing is when I retrieve the data on my app. 我面临的问题是当我在我的应用程序上检索数据时。 From my research, on Stack overflow as well as firebase docs, .Value gives a snapshot and continues to listen to data when new data is added. 根据我的研究,在Stack overflow以及firebase文档中,.Value提供了一个快照,并在添加新数据时继续监听数据。 However when new data is added it will take a new snapshot of the entire set (hence "duplicating data" on my app's array). 但是,当添加新数据时,它将获取整个集合的新快照(因此在我的应用程序阵列上“复制数据”)。

To get around this I have tried to instead use .ChildAdded, which works well to add new data to my array when new data is added to the firebase database. 为了解决这个问题,我尝试使用.ChildAdded,它可以在将新数据添加到firebase数据库时将新数据添加到我的数组中。 However it isn't adding the full data set (data that is already existing in the database), which is what I need in addition to new data being added. 但是它没有添加完整的数据集(数据库中已经存在的数据),这是我需要的,除了添加新数据。

Suppose firebase nodes as such: 假设firebase节点如下:

App_Queue:
         Category1:
                        Timestamp1:
                                  Uid: User_1_uid
                         Timestamp2:
                                   Uid: User_2_uid

Swift code (2.3): Swift代码(2.3):

Case1: 情况1:

self.databaseRef.child("App_Queue/\(Category1)").queryLimitedToLast(15).observeEventType(.Value, withBlock: { (snapshot) in
                if let userDict = snapshot.value as? [String:AnyObject]{
                    for each in userDict{

                        let timeIdExtract = each.0 as! String 

                        self.timeIdArray.append(timeIdExtract)
                        print(self.timeIdArray)
                    }

//this gives me full snapshot of time stamps & a userId
          //but if timestamp3 and user_3_uid is added to firebase, the array is appended with a new snapshot thus, duplicating items.

Case2: 案例2:

self.databaseRef.child("App_Queue/\(Category1)").queryLimitedToLast(15).observeEventType(FIRDataEventType.ChildAdded, withBlock: { (snapshot : FIRDataSnapshot) in
                if let userDict = snapshot.value as? [String:AnyObject]{
                    for each in userDict{

                        let timeIdExtract = each.0 as! String // Every follwers ID.

                        self.timeIdArray.append(timeIdExtract)
                        print(self.timeIdArray)
                    }

           //this gives me only new items added, but not already added. 
          // if timestamp3 and user_3_uid is added the array is appended with this new item. But timestamp1 & timestamp2 not added

Case 3: I have tried a hybrid of Case1 (instead use .observeSingleEventOfType()) & Case2, by adding a self.databaseRef.removeAllObservers() after the code in case 1, and then turning on the .ChildAdded observer for case2. 案例3:我尝试了Case1的混合体(而不是使用.observeSingleEventOfType())和Case2,在案例1中的代码之后添加了self.databaseRef.removeAllObservers() ,然后为case2打开.ChildAdded观察者。 It almost works.... added initial snapshot via case 1, then listens however if say, timestamp2 is recently added it will append this again when Case2 is called, thus duplicating the array. 它几乎可以工作....通过案例1添加初始快照,然后监听,如果说最近添加了timestamp2,它​​将在调用Case2时再次追加它,从而复制数组。

So my question: how the heck do I go about getting existing objects in the database AND adding on new ones, without duplicating data in the array? 所以我的问题是:如何在数据库中获取现有对象并添加新的对象,而不重复数组中的数据?

EDIT 编辑

Im silly-- had some extraneous code after the block which was throwing things off. 我很傻 - 在块之后有一些无关紧要的代码。 DOH! DOH! For anyone experiencing issues, first place to check! 对于遇到问题的人,首先要检查! also .ChildAdded paired with observeEventType did the trick. 还。与observeEventType配对的.ChildAdded完成了这个技巧。 (takes all previous data already in DB + adds new data as it comes in). (获取DB +中已有的所有先前数据,在添加新数据时)。

self.databaseRef.child("App_Queue/\(Category1)").queryLimitedToLast(15).observeEventType(.ChildAdded, withBlock: { (snapshot) in
...

Im silly-- had some extraneous code after the block which was throwing things off. 我很傻 - 在块之后有一些无关紧要的代码。 DOH! DOH! For anyone experiencing issues, first place to check! 对于遇到问题的人,首先要检查! also .ChildAdded paired with observeEventType did the trick. 还。与observeEventType配对的.ChildAdded完成了这个技巧。 (takes all previous data already in DB + adds new data as it comes in). (获取DB +中已有的所有先前数据,在添加新数据时)。

self.databaseRef.child("App_Queue/\(Category1)").queryLimitedToLast(15).observeEventType(.ChildAdded, withBlock: { (snapshot) in
...

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

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