High Firebase Downloads

Viewed 1624

I have a React Native app that has 2k users. All user data is stored in Firebase in the following format: Firebase data structure

So each user has a list of Fasts, where they generally create one fast per day. A very small amount of data. This is what my Database Usage graph looks like:

Firebase Usage

Some observations:

  • On the 27th, 134MB was downloaded. The total database size is .5MB. 134MB/.5MB = 268 times that the entire database was downloaded. This means each user would have downloaded their data 268 times, which is highly unlikely. My Firebase rules: Firebase rules So it's not possible users are downloading other users' data. I ran Firebase Profiler for ~11 mins: Firebase Profiler

I'm stumped. Finally, here's my one file that does almost every Firebase operation besides logging in/out:

import * as firebase from 'firebase';
import moment from 'moment'
import {
  currentFastChanged,
  setGraphData,
  setAllData,
  targetFastChanged,
  setLastFastEnd,
usernameChanged,} from '../actions/actions'
// Initialize Firebase
var firebaseConfig = {
};
export const firebaseApp = firebase.initializeApp(firebaseConfig);

function syncFirebase(store) {

  firebaseApp.auth().onAuthStateChanged(function(user) {
    if (user)
    {
      getUserRef().child('TargetFast').on('value', (snap) => {
        store.dispatch(targetFastChanged(snap.val() ? snap.val() : 15));
      });

      getUserRef().child('Fasts').on('value', (snap) => {
        var items = [];
        snap.forEach((child) => {
          items.push({
            start: child.val().Start,
            end: child.val().End,
            _key: child.key
          });
        });

        items.sort(function(a, b) {
           return parseInt(b.start) - parseInt(a.start);
       });
       store.dispatch(setGraphData(createGraphData(items, store.getState().myReducer.targetFast)));
       store.dispatch(setLastFastEnd(getLastFastEnd(items)));
       store.dispatch(setAllData(items));
     });

     getUserRef().child('CurrentFast').on('value', (snapshot) => {
       store.dispatch(currentFastChanged(snapshot.val()));
     });

   });

    }
  })

}

function getUserRef()
{
  return firebaseApp.database().ref('Users/' + firebaseApp.auth().currentUser.uid);
}

function createGraphData(items, targetFast) {

 var graphItems = [];
 var idx = 0;
 for(idx = 0; idx < 7 && idx < items.length; idx++)
 {
   var item = items[idx];
   graphItems.push({
     duration : moment.duration(item.end - item.start).asHours(),
     label : moment(item.start).format('ddd'),
     item : item
   });
 }

 while(graphItems.length < 7)
 {
   var add = targetFast * .2 * (Math.random() - .5);
   var randomVal = targetFast + add
   graphItems.push({duration : randomVal, label : "na", color : 'lightgrey', });
 }
  return graphItems.reverse();
}

function getLastFastEnd(items) {
  return items.length && items[0].end;
}

export function setCurrentFastRef(val)
{
  getUserRef().child('CurrentFast').set(val);
  //firebaseApp.database().ref('Users/' + firebaseApp.auth().currentUser.uid + '/CurrentFast').set(val);
}

export function pushNewFast(start, end)
{
  getUserRef().child('Fasts').push({'Start': start, 'End': end, 'Publish' : true})
}

export function editFastRef(key, start, end)
{
  getUserRef().child('Fasts').child(key).set({'Start' : start, 'End' : end});
}

export function deleteFastRef(key)
{
  getUserRef().child('Fasts').child(key).remove();
}

export function setTargetFastRef(val)
{
  getUserRef().child('TargetFast').set(val);
}

module.exports = firebaseApp;
module.exports.sync = syncFirebase;
module.exports.serverTimestamp = firebase.database.ServerValue.TIMESTAMP

My bill is already at $1 for about 15 days for what seems like a trivial amount of data. My current suspicions are:

  • Listeners are just stacking up and downloading data many times. The listeners aren't being deleted

  • Overhead that's not my fault (SSL?)

  • ??? Any ideas?

0 Answers
Related