2017-08-29 70 views
0

我現在用的是FIRDataEventTypeValue觀察,並根據文件塊將得到觸發檢測缺失:火力地堡使用observeEventType FIRDataEventTypeValue

「你的塊將被觸發的初始數據,並再次每當數據的變化。」

我將數據的本地緩存保存在NSMutatbleArray中,並且當事件觸發時 我搜索緩存,並且如果找到條目,數據將被更新爲新值。

如果在緩存中找不到條目,​​我將數據添加到緩存中。

但我該如何照顧刪除?我不想使用單獨的觀察者,或者這是唯一的方法。

[_myRef observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) { 
    NSDictionary *dict = snapshot.value; 
    if (dict == (id)[NSNull null]) { 
     [_cache removeAllObjects]; 
     [self dataEvent]; 
     return; 
    } 
    [dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { 
    // extract values from obj and store it in a cache 

回答

0

的方法observeEventType:withBlock將調用與所有包含在快照中的數據,這樣你就可以通過重建整個緩存「刪除」的數據,也沒有必要更新緩存條目。

[_myRef observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) { 

    [_cache removeAllObjects]; 

    NSDictionary *dict = snapshot.value; 
    if (dict == (id)[NSNull null]) { 
     [self dataEvent]; 
     return; 
    } 

    [dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { 
    // process the dictionary and its values and store in the cache