Android Question When should I use a service

dieterp

Active Member
Licensed User
Longtime User
I have been making use of a service for several years now in my B4A app to upload information every 20 seconds to a server for a live scoring system . It has generally worked quite well, although there are times when the service hangs and the user is kicked out of the app (This is quite erratic and difficult to pinpoint why this happens). I had a whole bunch of DoEvents in the service which I have since removed. Upon coming back to testing though I seem to be encountering even more issues now.

The B4A app was recently ported to B4i and since there are no services in B4i, I am using HttpJob and a timer to do the uploads from the same code module that the user scores on. I initially thought that this was going to affect the UI scoring process when the timer runs to upload the live scoring (Which can process quite a bit) at the same time that the user enters a score, but it seems like this does not interrupt the user experience at all. My question is, will it be feasible for me to now adopt the same approach in B4A, or should I still stick with the original service that I created?
 

KMatle

Expert
Licensed User
Longtime User
It is always good to seperate the UI from "higher" functions (like OkHttp) and - of course - the data itsself. So I always use services for non UI tasks and just call a Sub inside the Activities when I need to (e.g. to display data). Store the data in a List with maps and from there show it in the UI (= separation of data and UI). If you don't mix functions, UI and data you quickly see how good and easy your code will be. A lot of code can be copied then uf you need a new app with similar content.

To answer your question: Yes, use services as often as you can. Put all functions which are non UI related to these services. Work with events only (timers are ok, too). Do not use DoEvents at all (as you mentioned). Mostly it's a design problem/mistake if you need to use it.
 
Upvote 0

dieterp

Active Member
Licensed User
Longtime User
Thanks for the feedback KMatle. I managed to get the service working quite nicely again (Missed something that was causing the issues). I'm hoping the removing of DoEvents will also now help to stop the app crashes. If not I will post here again to see if anyone has any suggestions
 
Upvote 0
Top