Android Question Database update from a service..

Derek Jee

Active Member
Licensed User
Longtime User
Hi there

I wish to create a service which fetches remote data and updates the local db ready for the app running to interrogate.. I also wish to do the reverse at some point too, update the remote database with data from the local db. I want the service to run every 5 minutes, but also want the user to be able to trigger the service to run too..

Is this possible? Also can the service run without the app running? Ideally I would like the service to be independent from the app if possible which is always running? Am I asking too much :)

Thank you,


Derek.
 

keirS

Well-Known Member
Licensed User
Longtime User
I do this in several apps so it is perfectly possible. Have a read of the service tutorial. One thing you need to be aware of is if you want a totally independent service then you will need to manage the service so it isn't running when the app is running. I manage this via intents but there are other ways of doing it.
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
Thank you KeirS

I have read the tutorial and have downloaded the demo. Stuck in my db sync into the demo too and have it running as a service. That did seem a bit too straightforward to do of course but I guess actually implementing it will be the interesting bit :).. I'm guessing if it is a total independent service it will be a different project completely to my app. I'll have to think about that but as long as the service runs even when the app is 'sleeping' that will do for me..

Thank you for the tips..

Derek
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
Service runs on the same thread with app, this mean some long operations on main app or some event in main app that halt the thread - like modal dialog, could / will halt service operations.

I think separating service app and main app could make service runs independently.
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
Thanks Erel

Nothing too heavy I hope.. One app with a service is the way to go with this project.. Thanks for your comments gents,

Derek.
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
You don't need to separate the app into two apps. It will only make things much more complicated.

If you are issuing heavy SQL statements then you should use the asynchronous methods.

Usually you don't. My service is generic and has a configuration file so it can grab data from multiple REST endpoints and update multiple databases. This means I only need to run one service for multiple apps. Probably something most developers don't need to do.
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
It is not only about SQL. Sometime, data return from SQL need to combine with local data and recalculated again before presented to users.
If calculations is quite complex or data to process is big, it could delay the app.
If just a simple operation, no need to separate it.
 
Last edited:
Upvote 0
Top