gpt4 book ai didi

android - Firebase Android 离线性能

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:01:19 30 4
gpt4 key购买 nike

当单个节点下存储大约 5000 个子节点时,在使用离线功能时初始化 firebase 会变得非常慢。在执行第一个查询之前大约需要 30 秒。初始化后,执行后续查询(例如列出前 25 个子节点)只需不到一秒。

我正在使用以下属性来启用离线功能: Firebase.getDefaultConfig().setPersistenceEnabled(true); firebase.keepSynced(true);

我的结构是这样的:

<root>
|-my-app-name
|-<uid>
|-node
|-sub node 1
|-...
|-sub node 5000

保持同步设置在 <uid> 上节点。子节点显示在 Recycler View 中。最好,我想列出所有(而不是每页 25 个),但我知道这是不可能的,因为没有可用于与 Firebase 一起使用的 Cursor 之类的机制(Android 为 SQLite 提供)。

这是设计使然吗?我是否修改了我的数据结构?或者我可以通过其他方式减少初始化时间吗?

我在下面提供了一些日志记录。如您所见,正在进行大量垃圾收集。 Firebase 会在初始化时评估整个数据库吗?

谢谢!尼尔斯

04-01 15:59:12.029 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 43005(1717KB) AllocSpace objects, 0(0B) LOS objects, 4% free, 31MB/32MB, paused 5.674ms total 57.402ms
04-01 15:59:13.415 2222-2240/abcdef W/art: Suspending all threads took: 6.600ms
04-01 15:59:13.424 2222-2245/abcdef W/art: Suspending all threads took: 9.339ms
04-01 15:59:13.433 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 7097(281KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 32MB/32MB, paused 11.175ms total 27.105ms
04-01 15:59:13.821 2222-2245/abcdef I/art: Background partial concurrent mark sweep GC freed 101674(5MB) AllocSpace objects, 18(530KB) LOS objects, 35% free, 28MB/44MB, paused 3.400ms total 152.664ms
04-01 15:59:15.107 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 394024(15MB) AllocSpace objects, 0(0B) LOS objects, 20% free, 30MB/38MB, paused 1.865ms total 152.182ms
04-01 15:59:15.817 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 218328(8MB) AllocSpace objects, 0(0B) LOS objects, 19% free, 31MB/38MB, paused 1.711ms total 112.325ms
04-01 15:59:16.451 2222-2240/abcdef W/art: Suspending all threads took: 27.786ms
04-01 15:59:16.465 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 190591(7MB) AllocSpace objects, 0(0B) LOS objects, 18% free, 31MB/38MB, paused 1.832ms total 107.416ms
04-01 15:59:16.472 2222-2245/abcdef W/art: Suspending all threads took: 6.823ms
04-01 15:59:17.084 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 178714(6MB) AllocSpace objects, 0(0B) LOS objects, 15% free, 32MB/38MB, paused 1.717ms total 105.529ms
04-01 15:59:17.629 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 163584(6MB) AllocSpace objects, 0(0B) LOS objects, 14% free, 33MB/38MB, paused 1.743ms total 110.764ms
04-01 15:59:18.941 2222-2240/abcdef W/art: Suspending all threads took: 5.078ms
04-01 15:59:19.691 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 95627(3MB) AllocSpace objects, 0(0B) LOS objects, 8% free, 35MB/38MB, paused 7.190ms total 86.171ms
04-01 15:59:19.961 2222-2240/abcdef W/art: Suspending all threads took: 18.208ms
04-01 15:59:20.965 2222-2245/abcdef W/art: Suspending all threads took: 5.254ms
04-01 15:59:20.990 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 55899(2MB) AllocSpace objects, 0(0B) LOS objects, 5% free, 36MB/38MB, paused 6.799ms total 66.923ms
04-01 15:59:22.495 2222-2240/abcdef W/art: Suspending all threads took: 45.180ms
04-01 15:59:22.509 2222-2245/abcdef W/art: Suspending all threads took: 14.254ms
04-01 15:59:22.562 2222-2245/abcdef I/art: Background partial concurrent mark sweep GC freed 198174(6MB) AllocSpace objects, 3(487KB) LOS objects, 32% free, 33MB/49MB, paused 16.949ms total 215.369ms
04-01 15:59:23.811 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 392437(15MB) AllocSpace objects, 0(0B) LOS objects, 18% free, 35MB/43MB, paused 1.936ms total 168.222ms
04-01 15:59:24.480 2222-2240/abcdef W/art: Suspending all threads took: 22.464ms
04-01 15:59:24.497 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 227043(8MB) AllocSpace objects, 0(0B) LOS objects, 18% free, 35MB/43MB, paused 1.723ms total 117.855ms
04-01 15:59:25.173 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 203910(7MB) AllocSpace objects, 0(0B) LOS objects, 16% free, 36MB/43MB, paused 1.694ms total 112.618ms
04-01 15:59:25.181 2222-2245/abcdef W/art: Suspending all threads took: 7.301ms
04-01 15:59:25.784 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 185627(7MB) AllocSpace objects, 0(0B) LOS objects, 14% free, 37MB/43MB, paused 1.719ms total 115.362ms
04-01 15:59:26.345 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 167066(6MB) AllocSpace objects, 0(0B) LOS objects, 13% free, 37MB/43MB, paused 1.651ms total 106.055ms
04-01 15:59:26.865 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 154535(6MB) AllocSpace objects, 0(0B) LOS objects, 11% free, 38MB/43MB, paused 1.644ms total 104.888ms
04-01 15:59:28.357 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 151375(5MB) AllocSpace objects, 33(671KB) LOS objects, 9% free, 39MB/43MB, paused 2.740ms total 104.176ms
04-01 15:59:29.006 2222-2240/abcdef W/art: Suspending all threads took: 19.232ms
04-01 15:59:29.060 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 133554(5MB) AllocSpace objects, 29(580KB) LOS objects, 10% free, 39MB/43MB, paused 1.563ms total 100.220ms
04-01 15:59:30.173 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 131062(4MB) AllocSpace objects, 31(637KB) LOS objects, 9% free, 39MB/43MB, paused 1.653ms total 102.705ms
04-01 15:59:31.245 2222-2245/abcdef I/art: Background sticky concurrent mark sweep GC freed 122085(4MB) AllocSpace objects, 26(522KB) LOS objects, 8% free, 39MB/43MB, paused 2.380ms total 100.776ms
04-01 15:59:32.024 2222-2240/abcdef W/art: Suspending all threads took: 20.662ms

PS:这是一个交叉帖子:https://groups.google.com/forum/#!topic/firebase-talk/migEAwv26ns

最佳答案

初始化意味着 setValue 到那个节点对吗?因此,在单个节点下初始化 5000 个子节点需要大约 30 秒,这对我来说似乎很不寻常。我在单个节点下处理几乎相同大小的数据,性能要好得多。所以我不确定你在单个子节点下放置了多少属性,但无论如何,我想你需要再次检查性能。我认为您在 setValue 上使用 onCompleteListener 来计算初始化数据所花费的时间,因为 UI View 不会为您提供准确的时间,而且通常比实际操作时间。

Preferably, I would like to list all (instead of 25 per page) but I understand that this is not possible since there is no Cursor like mechanism (as Android provides for SQLite) available for working with Firebase.

虽然我不太确定您的目的,但对于此类情况,我的建议是同时维护 Sqlite 和 Firebase 数据库。让我澄清一下。

想法是为用户手机中的特定用户维护 Firebase 数据库的相同副本。这样本地数据库就可以在需要时完全满足您的目的。您可以查询数据库并可以使用您有一些经验的 CursorLoader

它还有其他一些优点。您可以使用自己的机制处理离线同步。当互联网中断时,将您想要同步的数据存储在您的本地 Sqlite 数据库中,然后当连接启动时,您将在 BroadcastReceiver 中收到回调。然后,您可以轻松地将离线数据 setValue 到 Firebase。 Firebase 当然使这更简单,但无论如何,由于您非常关心性能,您可以尝试一下。

当您的应用程序执行过多工作时,您发布的 GC 行为很常见。 Firebase 基本上使用 WebSocket 来维护与远程数据库的连接。所以我认为您需要检查是否与 Firebase 数据库保持不必要的连接。当不再需要监听器时,尝试使用 removeListener

Does Firebase evaluate the whole database when initializing?

我还不确定你所说的初始化是什么意思,但是是的,如果你再次将同一节点用于 setValue 到该节点,它会用新的数据替换以前的数据数据集。

关于android - Firebase Android 离线性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36401332/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com