1

为什么会为所有找到的孩子调用 FEventTypeChildAdded,而不仅仅是添加的孩子?

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
    }];
}];

似乎因为已经加载了孩子,所以只有在添加新孩子时才应该调用内部的 observeEventType 。但是,它被查询节点中的所有子节点调用。

如果是新节点,addFirebaseSnapshotToCache 将输出带有“Added”的字符串,如果是现有节点,则输出“Exists”字符串。您可以从输出中看到它必须遍历数据两次。

这是示例输出:

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

应该如何构造它以仅对数据进行一次思考,同时还允许观察新条目?

4

1 回答 1

1

我知道这对你来说可能为时已晚,但对于新手来说,我有这个解决方案

[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
                                              }


                                          }];

                   }];
于 2015-04-16T14:32:51.180 回答