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}
.....
}
This codes object has close to 5 million entries. I was thinking I would set status to 2 once a code is in use.
I'm using this to get a code from Firebase:
db.getReference("codes")
.orderByChild("status")
.limitToFirst(1)
.addListenerForSingleValueEvent { ..... }
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. I have tried my hands on Firebase FireStore. It doesn't allow importing huge JSON data as of now without exceeding 20K daily limits.
Edit - I've even set db.setPersistenceEnabled(false)
Exact error is :
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
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