Android - 内存成本 - Firebase 实时数据库 x 房间持久性库

Android - Memory Cost - Firebase Realtime Database x Room Persistence Library

调试 Firebase 实时数据库方法 ValueEventListener.onDataChangeDataSnapshot 从云端加载所有数据。

考虑到低内存成本,处理大量数据(从设备角度来看)的最佳解决方案是什么?

我希望同时实现这两个功能,使用 Firebase 实时数据库每天更新以使用 Room 数据库存储本地数据。对于这种方式,只有必要的数据才会根据用户交互加载到设备的内存中。

Thinking in low memory cost, what is the best solution to work with the large amount of data ( in device perspective )?

最好的解决办法是使用限制。这是关于 Firebase 实时数据库中 filtering data 的官方文档。如您所见,有几种方法可以帮助您实现这一目标。

如果您尝试加载大量数据,您可能会遇到类似这样的错误:

OutOfMemoryError. You may need to reduce the amount of data you are syncing to the client. java.lang.OutOfMemoryError: Failed to allocate a 28701016 byte allocation with 16777216 free bytes and 18MB until OOM

在这种情况下,您应该在所需 属性 上创建索引。没有它,服务器不能为你做 ordering/filtering,它会在客户端完成。这意味着客户端必须读取所有数据并将其保存在内存中,这解释了巨大的内存使用量。

解决方法很简单。使用 Firebase Security Rules 您应该在 desired 属性 上定义一个索引。它可以很简单:

{
  "rules": {
    "codes": {
      ".indexOn": "yourProperty"
    }
  }
}