1

I set an observer for some directory, represented as ref in this case like so (simplified code sample):

ref.observe(.value, with: { snap in if snap.exists(){ let some = MyStructure(snap: snap) print(some.date) } }) 

The problem is, even though the data in that location (ref) does never change, handler gets called multiple times (usually 2 times) and, what's bugging me most, with different data for just one field (the date one). Any thought on why this could be happening?


P.S. I usually remove all observers for a reference inside its callback handler, but, in this case, I can't do that, since the first call returns data with wrong date field, yet the next one tends to be the right one.

P.S.S. I cannot use '.observeSingleEvent' method, since it uses cached data (stored by Firebase, I believe), and here it is essential to retrieve only relevant data.

4
  • 1
    Are you using disk caching? Because if so, the first event could be coming from the disk cache. Commented Aug 30, 2017 at 23:35
  • @FrankvanPuffelen I do, yet, how come I get data with date set to 1970 when it is never the case? Commented Sep 4, 2017 at 16:06
  • That sounds like you have a location with a 0 value for a timestamp. Without seeing how you got into this situation, it's hard to say anytjing more than that. Commented Sep 4, 2017 at 22:44
  • @FrankvanPuffelen Thank you of the lead with 'disk caching'. It must be faulty, since it kept values for those fields as those which they never were. Commented Oct 11, 2017 at 15:38

1 Answer 1

1

Reason why

After some digging I've discovered that double-firing is an intended behaviour for Firebase SDK. First - cached snapshot for a reference, Second - retrieved from Realtime Database.

How to handle

  1. Set isPersistenceEnabled to false. This will ensure that all the .observe calls do not use data cached locally on the device

  2. Update data with the second call (if there will be one)

Sign up to request clarification or add additional context in comments.

1 Comment

How do you update data with the second call when it fires automatically?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.