Firebase iOS-快照未更新,但数据库已更新? [英] Firebase iOS - Snapshot isn't updated but database is?

查看:85
本文介绍了Firebase iOS-快照未更新,但数据库已更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一段时间在随机时间上遇到这个问题了,在那里我将物理上看我的Firebase控制台,看看我已经删除了一条数据,然后在代码中我将调用print(snapshot. ref)并查看正确的引用(也可以在浏览器中复制并粘贴以进行再次检查),但是当我尝试获取快照的值/对其子对象进行迭代时,快照以某种方式包含了不再存在于数据库中的旧数据.

let key2 = ref.child(users).child("Employees").observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in


            print(snapshot)

            for child in snapshot.children
            {

                self.nameList.append((child as AnyObject).value)
            }


     })

所以这里我的数据库看起来像这样:(图片被剪掉了,但是下面没有孩子)

无论如何,当我打印快照时,都会得到:

Snap (Employees) {
    0 = "";
    1 = "name1";
    2 = "name1";
}

这让我很沮丧,似乎与旧快照值有某种关系,以某种方式存储在本地或以某种方式看不到数据库的最新版本.如果很重要,我在此文件中对.observeSingleEvent的调用类似,则上面粘贴的一个副本将嵌套在另一个副本中.即使这是一个同步问题,我仍然不知道该如何使打印值成为旧值.

任何帮助都会非常感激.

解决方案

此行为显然是设计使然.真奇怪,我实际上联系了Firebase支持,并被告知他们将考虑修改行为或文档,但无法保证日期,因此我应该监视他们的observeEventOfType,除非我有一个非常具体的,与理论效率无关的原因来使用observeSingleEvent.应用程序的性能一直很差,然后数据可以按您期望的方式工作.

I've been having this issue at random times for quite a while, where I will physically be looking at my firebase console and see that I have deleted a piece of data, and then in code I will call print(snapshot.ref) and see the correct reference (copy and pasted in browser to double check too), yet somehow when I try to get the values of the snapshot/iterate over its children the snapshot is containing old data that is not in the database anymore.

let key2 = ref.child(users).child("Employees").observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in


            print(snapshot)

            for child in snapshot.children
            {

                self.nameList.append((child as AnyObject).value)
            }


     })

So here my database looks like this: (picture is cut off but there's no children under it)

Yet somehow when I print snapshot I get:

Snap (Employees) {
    0 = "";
    1 = "name1";
    2 = "name1";
}

This has been frustrating me for a while, it seems like it could have something to do with old snapshot values somehow being stored locally or somehow not seeing the most up to date version of the database. If it matters I have similar calls to .observeSingleEvent in this file, the one copy and pasted above is nested within another. Even if it were a synchronization problem, I still don't know how that could make the printed value the old value.

Any help would be so so appreciated.

解决方案

This behavior is apparently by design. It's so strange that I actually contacted Firebase Support about it, and was told that they'd consider revising either the behavior or the docs, but couldn't promise a date and I should monitor their Release Notes URL for updates to it.

It makes a little sense if you consider it from the SDK point of view. You're calling observeSingleEvent. To Firebase this means they should only call you ONE TIME. Developers would probably find it confusing if a method with that name produced more than one callback, right?

But if you have persistence enabled things get a little weird. Just like with observeEventOfType, Firebase will give you the on-disk value immediately so you get the fastest UI update, but then it will call the server for a fresher value to be sure it has the latest data from then on. The problem is, since you're telling it not to call you back with this data, it will remember it (so you WILL see it in the future, which is why it seems confusing) but not tell you that it's arrived.

What I've discovered through some trial and error is that the instinctive drive to use observeSingleEvent may be misguided with Firebase anyway. Both iOS and Android uses "recycler" view mechanisms for table/collection views such that only a handful of items are actually in-memory at a time anyway, even on screens with a lot of data. Beyond this built-in efficiency from the platform, Firebase itself seems to work just fine even managing many dozens of in-memory refs at a time. In my apps, I've taken to just using observeEventOfType for all of my use-cases except where I have a very specific, and not theoretical-efficiency-related reason, to use observeSingleEvent. The app performance has been minimal, and the data then works much more the way you expect.

这篇关于Firebase iOS-快照未更新,但数据库已更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆