Pre-Background: I'm using Firebase for database storage, for those of you who don't know how that works: when an object is stored/deleted/modified in Firebase (whether it be a String, int, etc), Firebase sends out an updated packet of info to the mobile devices that are listening with all of the new information. The thing is that Firebase doesn't send only the new object that was stored, it sends over everything in that node again.
Background: In order to find out if something was added to Firebase, I decided to make two int variables. One called "oldCount" and one called "newCount". They were both set to 0 onCreate(). Everytime Firebase sent a new update packet ("onDataChange") I did the following:
oldCount = currentCount;
currentCount = list.size();
"list" is an arraylist that I create from the info that Firebase sends. If currentCount>oldCount then data was added and I can proceed with my code.
Problem: After adding these two lines my memory usage increased, the latency between my device and Firebase increased, and a simple animation I had didn't work, my thread was skipping around ~200 frames. It sounds ridiculous, but after removing those two lines everything works fine, memory OK, no frame skips. I plan on looking for a different way to check if data was added, but I just found this error to be so strange, I was hoping someone could shed some light on what's going on.
Relevant Code:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private GoogleMap mMap;
private boolean mapReady = false;
private boolean firstLoad = true;
private int loadCount;
private ArrayList<> list;
private DatabaseReference Reference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
list = new ArrayList<>();
loadCount = 0;
setUpData();
private void setUpData() {
Query query = openTicketsReference.orderByChild("variableX").equalTo("variableY");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
listOfInspections = new ArrayList<>();
for (DataSnapshot ref : dataSnapshot.getChildren()) {
//get data and add to arraylist here
}
loadCount++; //see how many times data has been retrieved from server
}
setUpMarkers();
}
private void setUpMarkers() {
if(loadCount<2){
Log.v("loadCount<2",""+loadCount);
//do things for first data retrieval
}
else if (loadcount>1){
Log.v("loadCount>1",""+loadCount);
//do things for subsequent retrievals
}
}
}
What I'm seeing in these logs at the end is a bit confusing too:
(This is after adding data to firebase)
V/loadCount>1: 33
V/loadCount>1: 11
V/loadCount>1: 8
V/loadCount>1: 34
V/loadCount>1: 34
V/loadCount>1: 12
V/loadCount>1: 9
V/loadCount>1: 35
V/loadCount>1: 35
V/loadCount>1: 13
V/loadCount>1: 10
V/loadCount>1: 36
V/loadCount>1: 36
V/loadCount>1: 14
V/loadCount>1: 11
They should never be decreasing as I never subtract from the variable, only add (after setting it to 0 onCreate()).