Syncing offline data from TinyDB to Firebase

Hi Developers,

I’m developing an application to be used in a industrial area where there is no Internet connection. The app should store the data in TinyDB and when the user returns to the building where there is WiFi, he presses a syncing button, the app verifies if there is Internet connection, and if true, it uploads the data from TinyDB to Firebase.

The data I’m storing are users entrys from a screen with Spinners, Textboxes, Datepicker and Timepicker, as shown bellow:

I start in TinyDB a tag called numberOfEntrys starting from 0 and incrementing by 1:

I create a list with each item being each user entry. I store the list in the tag that is the number of the entry, for exemple, if I have 3 lists:
Tag 0: data from list 0
Tag 1: data from list 1
Tag 2: data from list 2
Tag numberOfEntrys: 2

In Firebase I have the same. Several tags being the number of each list and a tag called number_entrys:
Imagem2

What I’m not being able to do is to store more than 1 tag at the same time in Firebase. In the block bellow, when I click the syncing button I first call a function to verify if there is Internet connection. If true, then I verify if the numberOfEntrys in TinyDB is greater or igual to 0. If true, I use a For to sweep the TinyDB1 from 0 to the numberOfEntrys. Inside the For, I call a get value in FirebaseDB1 retrieving the number_entrys in Firebase and storing (number_entrys + 1) in the numberOfEntrysFirebase tag in TinyDB1.
Then I store in FirebaseDB1 in the tag being the number I just stored as numberOfEntrysFirebase tag in TinyDB1. The value to store is the list in the tag get number.
Then I store in FirebaseDB1 the updated number of entrys.

What happens when I run this code is that only the last entry in the TinyDB1 is stored. For example if TinyDB1 is:
Tag 0: data from list 0
Tag 1: data from list 1
Tag 2: data from list 2
Tag numberOfEntrys: 2

It stores in FirebaseDB1 only the list in Tag 2. It simply ignores Tags 0 and 1. I tried to add time delays of 5000 ms after each FirebaseDB1 access imagining that the app was trying to store data more fast than Firebase could handle, but that didn’t solved the problem.

Another attempt was using if-then blocks to try to force it to pass for all entries. When I have only entry 0 in TinyDB1 it runs fine, but if I have entrys 0 and 1 it stores only the entry 1, again. If I have entrys 0, 1 and 2 it stores only entry 2 and so on.

The last try was using do-while and storing backwards, that is, first the last entry in TinyDB1 then I clear the last tag and decrement the number of tags. Guess what? Is always stores only the last entry, i.e., the entry 0 of TinyDB1.

What am I doing wrong?

An idea.


Borr_TinyFirebase.aia (4.1 KB)

Your ideia solved my problem. Now everything is working. Thank you very much.
I implemented this way:



This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.