0

I'm using the Objective-C API, though I would imagine that this question is valid regardless of language.

I'm trying to write my code so that my app observes when child objects are removed from a particular node. When a child object is removed, I make local changes so that the local version of that child object is deleted.

I also have observers set for each child object's children on Firebase. This is so that I can propagate changes to those values locally as soon as they are made.

My observers are (finally!) working just fine – except that when I delete an object from Firebase, it seems like the observers for the object's children are triggered before the observer for the object's parent. E.g.,

If I have several blog posts listed under $userId/blogs/ according to their $blogId, and if each blog post has properties such as name and text, it seems like the FEventTypeValue observers at $userId/blogs/$blogId/name and $userId/blogs/$blogId/text are triggered before the FEventTypeChildRemoved observer at $userId/blogs when I set a particular $blogId to nil.

Any idea how I can set it so that the FEventTypeChildRemoved observer triggers before the FEventTypeValue observers? The alternative would be to always check whether the parent object exists when observing a change to a property on Firebase, but this to me seems like overkill.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ken M. Haggerty
  • 24,902
  • 5
  • 28
  • 37
  • Why are you using Value events? Added, Changed and removed will cover it and be way more efficient! Value loads everything in the node which is/may be a LOT of data whereas the other three just work with the data that was added, changed or removed. – Jay May 10 '16 at 17:21
  • Hi Jay – That sounds great! Missed that `FEventTypeChildChanged` only includes the data that was changed in the snapshot. But then I suppose I will have to check the keys in `FDataSnapshot.value` to see exactly which values were changed? If that's the case then I'll have to rewrite a lot of my code :-/ – Ken M. Haggerty May 10 '16 at 17:34
  • All three observers return the node that was added, changed or removed - the key and child values are included so it's super easy to update your dataset from that. – Jay May 10 '16 at 17:37
  • I think where I will have to rewrite a lot of my code is in assuming a 1:1 observer-to-property architecture, whereas FEventTypeChildChanged (if I understand it correctly) is a 1:many observer-to-property architecture, so if I want to remove the observer for one property but not another, I can no longer just remove the observer itself. – Ken M. Haggerty May 10 '16 at 17:40
  • That may be overcomplicating it - you can always remove observer handles from specific nodes. But that in itself may be working at too much of a granular level. I would really need to see your Firebase structure and code to get a better understanding of how you app flows. Update your question with a code snippet and your Firebase structure (as text please; Firebase Dashboard->Export) and maybe we can make some suggestions. – Jay May 10 '16 at 17:47

0 Answers0