2013-08-20 26 views
1

爲什麼FEventTypeChildAdded對所有找到的孩子而不僅僅是添加的孩子進行調用?爲什麼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將輸出一個字符串,如果它是新節點,則會輸出「已添加」;如果它是已有節點,則輸出「已存在」。你可以從輸出中看到它必須經過兩次數據。

下面是示例輸出:

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): 123456789
Added = FLS3 (G:1386838476): 123456789
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): 123456789
Exists = FLS3 (G:1386838476): 123456789
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 

如何來構造只能使一個通認爲該數據同時還允許待觀察新的項目?

+0

這是由設計。查看http://stackoverflow.com/questions/18270995/how-to-retreive-only-new-data/18283441#18283441,瞭解一個(JavaScript)解決方案,該解決方案僅檢索收到初始數據後添加的項目。 – Anant

+3

Objective-C對此有何建議? –

回答

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 
               } 


              }]; 

        }]; 
相關問題