Firebase数据库:在Android应用程序上查询大量数据时出现内存不足异常 [英] Firebase Database: Out of Memory Exception on querying huge amount of data on Android app

查看:58
本文介绍了Firebase数据库:在Android应用程序上查询大量数据时出现内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的Android应用程序使用Firebase数据库.我需要一个唯一的5位数代码,有效期为3天.为了解决这个问题,我在Firebase数据库中插入了所有可能的5位代码,其中每个条目的初始状态均为1.所以结构看起来像这样:

I'm using Firebase database for my Android app. I needed a unique 5-digit code which should be valid for 3 days. To resolve this I inserted all possible 5-digit codes in Firebase database where each entry initially has status as 1. So structure looks something like this:

codes: {
  'abcde': {status:1},
  'zhghs': {status:1}
  .....
}

codes对象具有将近500万个条目.我当时想在使用代码后将状态设置为2.

This codes object has close to 5 million entries. I was thinking I would set status to 2 once a code is in use.

我正在使用它从Firebase获取代码:

I'm using this to get a code from Firebase:

db.getReference("codes")
                .orderByChild("status")
                .limitToFirst(1)
                .addListenerForSingleValueEvent { ..... }

我只需要1个代码,其状态为1.上面的代码导致OutOfMemoryException,所以我认为它正在尝试将所有数据加载到设备上.由于limitToFirst(1)约束,它不应该只在设备端加载一个元素吗?

I just need 1 code which has status as 1. The above code is causing OutOfMemoryException so I think it is trying to load all the data on device. Shouldn't it just load one element on device side due to limitToFirst(1) constraint?

P.S.我已经尝试过Firebase FireStore.到目前为止,不允许导入庞大的JSON数据且每天的限制不超过20K.

P.S. I have tried my hands on Firebase FireStore. It doesn't allow importing huge JSON data as of now without exceeding 20K daily limits.

编辑-我什至设置了db.setPersistenceEnabled(false) 确切的错误是:

Edit - I've even set db.setPersistenceEnabled(false) Exact error is :

Firebase数据库遇到OutOfMemoryError.您可能需要 减少要同步到客户端的数据量. java.lang.OutOfMemoryError:无法分配28701016字节 分配有16777216个空闲字节和18MB直到OOM

Firebase Database encountered an 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

推荐答案

听起来您可能没有在status属性上定义索引.在这种情况下,服务器无法为您进行排序/过滤,而将在客户端中完成.这意味着客户端必须读取所有数据并将其保存在内存中,从而解释了巨大的内存使用情况.

It sounds like you might not have an index defined on your status property. In that case, the server can't do the ordering/filtering for you, and it will be done in the client. That means the client has to read all data and keep it in memory, explaining the huge memory usage.

解决方案非常简单:在status属性上定义一个索引.

The solution is quite simple: define an index on your status property.

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

另请参阅有关索引数据的文档.

这篇关于Firebase数据库:在Android应用程序上查询大量数据时出现内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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