Firebase Data:
{
"data": {
"entry-1": {
"created": 1484634400,
"status": 1
},
"entry-2": {
"created": 1482612312,
"status": 0
},
"entry-3": {
"created": 1481623400,
"status": 1
},
"entry-4": {
"created": 1485613233,
"status": 1
},
"entry-5": {
"created": 1489513532,
"status": 0
},
"entry-6": {
"created": 1483123532,
"status": 1
},
"entry-7": {
"created": 1481282376,
"status": 1
},
"entry-8": {
"created": 1432321336,
"status": 1
},
"entry-9": {
"created": 1464282376,
"status": 0
}
}
}
I'm trying to count how many active entries (status
= 1) were created before entry-4
, and keeping the count updated live.
Today I listen to every change in the database, but it is consuming a lot of unnecessary data. Is there a better way to do this?
Code:
FIRDatabaseQuery *query = [self.firebase child:@"data"];
[query observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
int count = 0;
for (FIRDataSnapshot *child in snapshot.children) {
if (child.value[@"status"] == 1 && child.value[@"created"] < 1485613233) {
count++;
}
}
}];