1

Why is FEventTypeChildAdded getting called for all found children, not just added children?

m_firebaseRef  = [[Firebase alloc] initWithUrl:fullChatPath];

FQuery* messageListQuery = [m_firebaseRef queryLimitedToNumberOfChildren:100];
[messageListQuery observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
    NSLog( @"Name %@ with %d children.", snapshot.name, snapshot.childrenCount );

    for( FDataSnapshot *child in snapshot.children )
        [self addFirebaseSnapshotToCache:child andNotifyObservers:NO];
    [self addFirebaseSnapshotToCache:nil andNotifyObservers:YES];              // Notify everything was added

    // What kind of stinks is that I have to go through multiple passes of the data because FEventTypeChildAdded is triggered every time,
    // a list is loaded as opposed to only being triggered for new nodes being added!
    [messageListQuery observeEventType:FEventTypeChildAdded andPreviousSiblingNameWithBlock:^(FDataSnapshot *snapshot, NSString *prevNodeName) {
        [self addFirebaseSnapshotToCache:snapshot andNotifyObservers:YES];     // Notify if something new was added
    }];
}];

It seems like since the children have already been loaded that the inner observeEventType should only be called when new children are added. However, it is being called for all children in the queried node.

The addFirebaseSnapshotToCache will output a string with "Added" if it was a new node or "Exists" if it was an existing node. You can see from the output it is having to go though the data twice.

Here is the example output:

Name Live with 15 children.
Added  = FLS3 (G:1386838476): Test
Added  = FLS3 (G:1386838476): Hello, I think this will work just fine
Added  = FLS3 (G:1386838476): 12345678901234567890123
Added  = FLS3 (G:1386838476): 12345678901234567890123
Added  = FLS3 (G:1386838476): How long of a text message
Added  = FLS3 (G:1386838476): Let see how will this do?
Added  = FLS3 (G:1386838476): Hello, this is a really long message to test 
Added  = FLS3 (G:1386838476): This is another really long test of characters in
Added  = FLS3 (G:1386838476): WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
Added  = << FLS4 >> (G:1386838660): test
Added  = FLS3 (G:1386838476): Test back
Added  = FLS3 (G:1386838476): Message #12
Added  = FLS3 (G:1386838476): Ok
Added  = FLS3 (G:1386838476): Test again
Added  = FLS3 (G:1386838476): Wow it works
Notify of Changes
Exists = FLS3 (G:1386838476): Test
Exists = FLS3 (G:1386838476): Hello, I think this will work just fine
Exists = FLS3 (G:1386838476): 12345678901234567890123
Exists = FLS3 (G:1386838476): 12345678901234567890123
Exists = FLS3 (G:1386838476): How long of a text message
Exists = FLS3 (G:1386838476): Let see how will this do?
Exists = FLS3 (G:1386838476): Hello, this is a really long message to test 
Exists = FLS3 (G:1386838476): This is another really long test of characters in
Exists = FLS3 (G:1386838476): WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
Exists = << FLS4 >> (G:1386838660): test
Exists = FLS3 (G:1386838476): Test back
Exists = FLS3 (G:1386838476): Message #12
Exists = FLS3 (G:1386838476): Ok
Exists = FLS3 (G:1386838476): Test again
Exists = FLS3 (G:1386838476): Wow it works

How should this be constructed to only make one pass thought the data while also allowing for new entries to be observed?

Tod Cunningham
  • 3,691
  • 4
  • 30
  • 32
  • This is by design. Take a look at http://stackoverflow.com/questions/18270995/how-to-retreive-only-new-data/18283441#18283441 for a (JavaScript) solution that only retrieves items added after the initial data is received. – Anant Aug 20 '13 at 21:36
  • 3
    Any advice for Objective-C on this one? – Tod Cunningham Aug 25 '13 at 16:24

1 Answers1

1

I know this might be too late for you but for newcomers i have this solution

[firebase observeSingleEventOfType: FEventTypeValue withBlock: ^(FDataSnapshot *snapshot)//fetch 50 last comments
                   {
                       __block BOOL initialLoad = YES;
                       for (FDataSnapshot *child in snapshot.children)
                       {
                           //process your initial data
                       }                           
                       //register observer for new data
                       FQuery* newCommentsQuery = [firebaseRoot queryLimitedToLast:1];
                       _commentsHandle = [newCommentsQuery observeEventType: FEventTypeChildAdded withBlock: ^(FDataSnapshot *snapshot)
                                          {
                                              if(initialLoad)//we are not interested in comments which are already loaded
                                              {
                                                  initialLoad = NO;
                                              }
                                              else
                                              {
                                               //process your new data
                                              }


                                          }];

                   }];
beretis
  • 899
  • 1
  • 9
  • 24