I'm kind of confused about how to do this. I have string values in the realtime database in firebase console. They change when the app is running because the code is causing the values to change, but when the application is stopped, the values won't change anymore. I want the values to change even when the application is running or not. So for example, a counter variable in the database will go up every minute forever as long as the data is there.
From your explanation this seems to be an issue which can be easily solved by keeping the app running in background. For this purpose look here.
Quoting the answer from above link
A very simple answer for your problem is to use Service. It will allow you to perform variety of tasks while being in background and is your best bet for sending your location to server silently.
Read this answer for help.
Although both the answers are related to location update but they can be easily extended to fulfill your requirements.
Hint: The above quoted answers are sending the data to a server at frequent intervals. Send your data to Firebase server when the data-field is changed.
Hope this answers your query. Provide some code I might help with that too, if I get time.
Related
This question might be asked before. But I couldn't find any relevant information related to my query. So my question is can I use a firestore listener inside a service so that it will run in the background even when the application is minimized. Can we add a firestore listener inside a service class ?
Thanks :)
You can do whatever you want in an Android Service. A service is just a way to tell Android that you have some work to do that doesn't necessarily have a UI attached to it. If you want to leave a service running as much as possible, you will have to make it a foreground service, which means you're obliged to provide an icon for the notification curtain, so the user knows that your app is consuming resources on the device.
While your service is keeping the Android app process alive, you can certainly have a listener attached to some document or query, and it will update as the results change. But be aware that you will be charged for each of the updates, it will cost the user bandwidth, and it will drain their battery.
I do not have a sufficient reputation so I am writing here.
Yes you can use firestore listner in service.
I am developing an SMS app. I am getting messages from the Android Database every time user opens the App.
On a device with around 100 messages, it works fine. But with a device with 10,000 messages the app takes around 20 seconds to display the list after getting all the information.
To solve this, I am thinking about three possible options here:
Should I consider saving all the data to my own database? Will it make it faster?
Should I get some messages first, and then the rest in the background?
Any other option you might suggest.
Thanks in advance.
Below are the answers of your question
1. Should I consider saving all the data to my own database? Will it
make it faster?
No, don't do that. This will not make it faster. Same response you will get.
2. Should I get some messages first, and then the rest in the
background?
You can do this, but there is no need to do this to get all messages in background.Because User can see limited number of messages and chances are he will not see all messages down the bottom. So it will be useless to get those in background (untill or unless there is a business requirement)
Any other option you might suggest.
Yes, you need to implement pagination and need to keep track how many messages are needed to be loaded first time and while user scrolls then get more messages. In this case you need to keep track your own about how many messages you have already loaded and how many more you want to load. TO achive this, you will be required to implement PULL TO REFERESH mechanism of android. And a lot of tutorials can be found over the web.
Hope that answers your question.
I am using Firebase realtime database in my android app. And I have observed that if more than one phone is connected with database using value event listener, then the last connected phone receives data very late. Is it that firebase sends data to one by one client? then it will definitely cause delay.So how to cope with this?
No firebase database is realtime and all changes are reflected immediately all devices connected to a database gets data simultaneously. Check your network connectivity of device
I got it. Actually I got answer on Slack Firebase community from a helpful user, and my doubts are clear now. He explained that this behavior is normal and delays maybe be scaled or constant. And He also said that data will be guaranteed to be received by all listeners though the node is updated during delay period. And data is not lost but delay is certain.
I hope this may help somebody who faced same issue like me.
Im am developing a webservice for an App that I am writing. I want to make the App offline accessible.
I made that webservice so that if you request JSON from the webservice you can give a date:
/color/colors/date/2014-03-01T12:00/
If you don't give the date you will get everything that is inside the database and that is active. If you give the date you will only get everything that is updated after that date.
Now my problem is that if I remove something from the webservice of from the App then it will not be synced and the other devices will never know that it is removed.
I could work with a field where I say that a record is removed but then I need to keep every record and I can't delete any record.
Is there a better way to do the syncing? Or what is the best way?
I think there is no possible way to detect the deleted entry's change, unless you send the information that the entry is deleted. The best practise you set a field in the table with integer type, then you can set this value on updates. You don't have to set it only 1 or 0, you can use bigger numbers (for example I used 30 on that entries, that I deleted on 20th march , on this day was a big code logic change. after that i knew when the status integer is 30, then i deleted this row after that date.) It may be a silly example, but you can implement your own logic.
It depends on how important it is to update the rest of the devices when a change is made.
If it is critical, then it would be worth implementing push notifications or something similar to each device to let them know about the updated situation. Otherwise, you would simply have the other devices poll the server to check for changes on their own accord, and the frequency or the trigger of this poll would depend again on how critical it was that they get an update. Maybes they only need updating when they visit a certain activity, so in that case you would only poll when you reach the onResume() event of that activity
Update
If you don't need to keep a history of the deleted record, then why can't you just delete it, and then when the rest of the devices update, you clear all and download a fresh set? If that is too intensive, you would NEED to have a reference to the id, which you could do in a table or use a special value in the field (like null, 0 or -1) to mark it. Otherwise there would be no way to reference it
This is for an app for a clinic giving course in stop smoking and nicotine addiction,
We want to make sample app for android, where we scheduled the treatments course time plan, that’ remind the patients when to take the medicine.
So when the patient start their treatment, they just register at the day of starting the treatment, then automatically the app will scheduled all the following treatment, and send a reminder when next treatment arrive.
I know this topic is broad to answer but I just want to know how to make the database (schedules of the prescription) set for the timer to activate?? Can I use an external database that is network based..If so how to do that???Is sqlite a better option??? And how to automatically get those data and schedule it in the android app??
Since the data will be shown to all users like the time table so they can reserve if there is an empty date, then you require to have a cloud database and i suggest you to start learning how to connect android with MySQL here is a great video tutorial of how to connect android with MySQL by the help of PHP and JSON.
Now if you want this application to work offline, you require to have a SQLite database that when you connect to the internet once, you save these data in it. and whenever the device connects to the internet it updates the database entries.
for alerting the paitents you will have a service that runs to check the data , then start a notification. It may be possible to have an event listener for some given event that would start your service that would then check the date, and if so post your notification!
here is a good tutoial about it.
I hope I have covered most of your questions. There may be a better solutions around so dig more. Good luck with your app.