1

i am working on a project where I am pulling data from firebase (which is "queued"). 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. 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. 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:

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

Swift code (2.3):

Case1:

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:

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. 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.

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! For anyone experiencing issues, first place to check! also .ChildAdded paired with observeEventType did the trick. (takes all previous data already in DB + adds new data as it comes in).

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

1 Answer 1

0

Im silly-- had some extraneous code after the block which was throwing things off. DOH! For anyone experiencing issues, first place to check! also .ChildAdded paired with observeEventType did the trick. (takes all previous data already in DB + adds new data as it comes in).

self.databaseRef.child("App_Queue/\(Category1)").queryLimitedToLast(15).observeEventType(.ChildAdded, withBlock: { (snapshot) in
...
2
  • 1
    I'm running into a similar problem like you - I get the first batch of existing data with a observeSingleEvent/.value call, then I get duplicate data with a observe/.childAdded call from within the first finish block. Now the second call returns ALL data, so I get double data on controller load.
    – Jonny
    Mar 27, 2017 at 9:46
  • I am not sure what happened (Firebase is new to me, but it seems like it behaves a bit unexpected) but for now I ditched the first call, and only when with observe/.childAdded. Ie no more observeEventType/.value, this way I don't get data twice at startup.
    – Jonny
    Mar 27, 2017 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.