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

时间:2013-08-20 16:54:57

标签: firebase

为什么要为所有找到的孩子调用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将输出一个字符串"添加"如果它是一个新节点或" 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

这应该如何构建,只考虑数据一次,同时也允许观察新的条目?

1 个答案:

答案 0 :(得分: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
                                              }


                                          }];

                   }];