Firebase 'observe' fires multiple times with different data

时间:2017-08-30 20:46:09

标签: ios firebase-realtime-database

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

<body>

The problem is, even though the data in that location (Document doc = Jsoup.parseBodyFragment(html); doc.outputSettings().prettyPrint(false); System.out.println(doc.select("body").html()); ) 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 ref 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 ref.observe(.value, with: { snap in if snap.exists(){ let some = MyStructure(snap: snap) print(some.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.

1 个答案:

答案 0 :(得分:1)

原因

经过一番挖掘后,我发现双击是Firebase SDK的预期行为。第一个 - 参考的缓存快照,第二个 - 从实时数据库中检索。

如何处理

  1. isPersistenceEnabled设为false。这将确保所有.observe调用不使用设备本地缓存的数据

  2. 使用第二个电话更新数据(如果有的话)