0

I’m now connecting to a BLE device via my Android APP, and add the received data to an ArrayList. When a button is pressed, I’m writing the data in this ArrayList to Firebase. However, I’m wondering if there is any better way to perform the writing process since the ArrayList size might be large when I hit the button and start to write. The following is my code:

List<HashMap> dataBufferList = new ArrayList();

HashMap<String, String> data = new HashMap<>();
data.put("HR", ","80");
data.put("GPS","20.75,56.98");
data.put("Time", getCurrentTime(sdf)); 
dataBufferList.add(data);

for(int  i = 0 ; i < dataBufferList.size();++i){
myRef.push().setValue(dataBufferList.get(i));
}
dataBufferList.clear();
Alison
  • 431
  • 6
  • 19

1 Answers1

1

What is "better" is subjective. But if you'd like to send the entire update in one call, you can do that with updateChildren():

List<HashMap> dataBufferList = new ArrayList();

HashMap<String, Object> updates = new HashMap<>();

HashMap<String, String> data = new HashMap<>();
data.put("HR", ","80");
data.put("GPS","20.75,56.98");
data.put("Time", getCurrentTime(sdf)); 
dataBufferList.add(data);

for(int  i = 0 ; i < dataBufferList.size();++i){
    updates.put(myRef.push().getKey(), dataBufferList.get(i))
}
myRef.updateChildren(updates);
dataBufferList.clear();

The performance difference between this and what you do will be minimal, since all requests are already pipelined over a single connection (see my answer here). But sending them in one call may allow you to do additional validations on the server, if all data belongs together.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807