【问题标题】:Firebase detect deletion using observeEventType FIRDataEventTypeValueFirebase 使用 observeEventType FIRDataEventTypeValue 检测删除
【发布时间】:2017-08-30 01:35:12
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: ios objective-c firebase firebase-realtime-database


    【解决方案1】:

    方法 observeEventType:withBlock 将被调用 all 包含在快照中的数据,因此您可以通过 “删除” 数据重建整个缓存,也不需要更新缓存条目。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      相关资源
      最近更新 更多