【问题标题】:Wrong listeners for FIRDatabaseReference fired触发了 FIRDatabaseReference 的错误侦听器
【发布时间】:2017-01-19 13:07:36
【问题描述】:

根据 Firebase 的文档

FIRDataEventTypeChildAdded :This event is triggered once for each existing child and then again every time a new child is added to the specified path.

但是当我使用方法updateChildValues更新指定节点的子节点时甚至会触发它

updateChildValues Documentation

我的代码:

[_followersReference observeEventType:FIRDataEventTypeChildAdded
                            withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
                               //increment the badge here
                                //add in local DB
                                //can fire a local notification
                                RCFollowerFireBaseModel *remoteFollower = [RCFollowerFireBaseModel parseDictionary:snapshot.value];
                                GMSMarker *marker = [GMSMarker markerWithPosition:remoteFollower.location.coordinate];
                                marker.title = remoteFollower.name;
                                marker.snippet = remoteFollower.time;
                                marker.appearAnimation = kGMSMarkerAnimationPop;
                                marker.map = self.mapView;

                            }
                      withCancelBlock:^(NSError * _Nonnull error) {

                      }];

[_followersReference observeEventType:FIRDataEventTypeChildRemoved
                            withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
                               //decrement the badge here
                                //remove followers

                                NSLog(@"%@",snapshot);
                            }
                      withCancelBlock:^(NSError * _Nonnull error) {

                      }];


[_followersReference observeEventType:FIRDataEventTypeChildChanged
                            withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
                                //update the marker with the updated coordinates here
                                //can apply 
                            }
                      withCancelBlock:^(NSError * _Nonnull error) {

                      }];

我的问题是,当我在 _followerReference 更新孩子时不应触发 FIRDataEventTypeChildAdded,而应该触发 FIRDataEventTypeChildChanged,但在 _followerReference 更新孩子会同时触发 FIRDataEventTypeChildAddedFIRDataEventTypeChildChanged

是我做错了什么还是 Firebase 中的错误?

【问题讨论】:

  • 您的代码都没有触发任何更改,很难说您看到了什么。请提供minimal complete code that is needed to reproduce the problem。这将包括您正在阅读/修改位置的 JSON(作为文本,没有屏幕截图)、导致问题的侦听器代码以及更改值的代码。
  • 弗兰克提供的答案很幸运地解决了我的问题顺便说一句感谢您的编辑,我会处理重现问题所需的最少代码。

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


【解决方案1】:

这不是错误,我遇到了同样的问题。 确保您没有递归调用此方法

- (FIRDatabaseHandle)observeEventType:(FIRDataEventType)eventType withBlock:(void (^)(FIRDataSnapshot *snapshot))block withCancelBlock:(nullable void (^)(NSError* error))cancelBlock;

【讨论】:

  • 这个方法是每 30 秒调用一次 NSTimer 选择器。删除解决了我的问题谢谢。
猜你喜欢
  • 1970-01-01
  • 2022-06-10
  • 2015-12-27
  • 2019-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多