2017-08-29 39 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