I'm trying to download data with Firebase. The issue is that I'm downloading the data based off of a timestamp. If their is only one item in my ListView that I'm populating, I want to get the next two hours worth of data from Firebase and if there isn't any data from the last two hours, I want to get the next two hours of data and so on until I get some additional data. The issue is that I don't exactly know how to determine if Firebase actually downloaded data. Because if data was downloaded, I can use an OnScrollListener for my listView to download more data since it will be scrollable after that point. Any help would be appreciated.
You cannot know when getting the data from the database is completed becase Firebase is a real-time database and getting data might never complete. That's why is named a realtime database because in any momemnt the database can be changed, items can be added or deleted.
The only way to partially know if you have all the data at a particular location is to perform a single 'value' type query on it. Even then, the data may change after that listener is invoked, so all you really have is a snapshot at a particular moment in time.
You can use a CompletionListener only when you write or update data in your database and you'll be notified when the operation has been acknowledged by the Firebase servers but you cannot use this interface when reading data.
Related
How to keep firebase realtime database in sync with firebase storage? I am trying to store images in firebase storage.
The current scenario
a POJO (which contain 3 images placeholder link) is saved in the realtime database.
Then the 3 images are uploaded in firebase storage and on successful upload the respected placeholder links in the realtime database are updated.
Now the problem is that sometimes when the user deletes the project in between the POJO is deleted from firebase but the links are still updated.
Is there a better way of doing this ? or is there any standard approch? Thankyou for your time.
There is not a standard approach to this, and it can't be solved by simple configuration. You will have to get creative and implement something that meets your specific needs.
One thing you can do is use Cloud Functions triggers to mirror and delete data between the two products, so that one data in one is added, modified, or deleted, that change is reflected in the other product. It would take at least two triggers, one database trigger, and one storage trigger, to make sure changes in either product are mirrored to the other product. You might also need some additional process to remove or correct invalid data periodically, if that is a concern.
Hello fellow developers,
I have a question about getting the data from Firebase. Trying to compare the transactions list of one user with another user in a function. Two separate hashmaps are used to store the data (transaction list) of each user. Then I loop through the list of records of first user and try to match with the records of the second user. Since Firebase is asynchronous, function skips before I get the data. I need some ideas to manage the asynchronous fetching of data from Firebase.
Thanks
I am using the firebase realtime database in my android app. I only want the last entry inserted into the database and not an entire database sync.I am doing the following.
FirebaseDatabase.getInstance().getReference("reference").limitToLast(1).addValueEventListener(this);
Here's what I could not clearly understand :
1.When I do a limitToLast and add a listener, does it download the entire data from the database into the local copy and pass me a snapshot of the last entry or does it download only the last entry?
2.When I remove the listener , does the sync stop between the realtime database and the local data or the sync keeps happening but the onDataChange is not called?
3.The difference between removing the Value Event listener and going Offline.
When you call limitToLast() you create a Firebase Database query. Such a query only synchronizes the data that it matches, so in your example it only downloads the last item and invokes onDataChange with it. Then when somebody adds a new item, it invokes onDataChange again with the new last item.
Removing a listener stops the data synchronization.
Going offline temporarily stops data synchronization, until you call goOnline().
I have a remote database that can change at any time. I need to find a way to keep my sqlite database update with all the changes in the better and optimised way possible.
I thought having a single timestamp per table and send only the updated table to the client would be one solution, or maybe having one timestamp per row and sending only the updated row to the client..
But
- how can i manage deleted items, for example?
- how can i manage the technical update on Android?
Basically, in a few words, at some point in the app, i need to download the changes with an API and update the local db.
Does anyone have some ideas?
Thanks
Since it seems this is a one-way only sync, I would:
Make each table on the server DB have a UUID column and a last
updated date/time column.
Create a REST call that will request updated data. Ideally, it would
take the time the client was updated. This date should come from the
server during the update call.
The server would scan each table searching for rows that have a date > than the date passed. Serialize the data and return it as JSON along with the server date/time for the next request.
Now tracking deletes is a bit more work. You can either:
Never delete data and only mark the rows as deleted, this is the easiest, but depending on your data may end up with lots of rows in the DB.
Delete the data and then track the deleted rows in another table. These can be deleted after some time and if/when all clients have been updated.
Have a second update call that will push down all the UUID values and time stamps for each row. The client could then figure out which rows need to be deleted.
Though it's the most complex, I'd probably opt for option 3, as I don't like old data hanging around. The direction I would go would also depend on how many clients will be syncing with the DB.
I've decided to follow another approach:
I've created an api that parse the Mysql db into an SQLITE db on the SERVER side. There is a "last_update_timestap" that will be updated every time that something will be changed.
Another api call, gives in the header an extra field containing that timestamp.
On the client side there is a Sync process that do the follow when needed:
Api call retrieving just the HEADER, getting the last update timestamp and checking if an update is needed;
If is needed download the database from the other api
Write the database into a temporary file and copy that temporary file at the position and with the same name of the real database
Use the database
Thanks everyone for the help but this turned out to be the suitable approach for my project structure.
I'm making an app which has to download some data, parse it and store it in a SQLite Database. However I'm having a problem where the downloadtask (an asynctask) executes everytime the app is launched and keeps appending the duplicate data to the database so I get multiple instances of the same data.
I only want to execute the download task if the database has values in it but still want to be able to run the downloadtask if the data file on the server is updated.
Other than checking if the number of rows in the table is greater than 0 how would I go about doing this? I'm not really sure what to search for. Any help would be appreciated!
Each reach should have a unique ID.
When loading the data check if the unique ID is in the database
If it exists update the row.
If it doesnt exist add it(append).
If you control the database in the website, you can put there a flag you update anytime the database is updated. So before reading the data, check your flag. If updated load new and append new data to the database.
For your local part of the app: Well you can easily update the data as you desires if a query on this database return more than one row. This is very easy using a cursor and getting its count after the query is executed. If you dont want to have duplicates just delete the database after you have made sure that your download task has retrieved the data from the remote source. In that way you won't have duplicates and you will be sure that you don't delete by accident the database if something goes wrong.
For the remote source:You can enable Push Notifications and send one after something is changed in the database.So by sending a notification you can trigger a service that will download the new data and parse it properly.