Android Question High Demand Foreground Service and App Freeze

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello,
I have a problem here. I need to run a foreground service that requires about 1 or two minutes and some considerable level of processing and as in B4A it runs at the same thread of the gui this is freezing. The service has a loop (for ... next) which loads a json and decodes it, storing the results in a sqlite local database.
How can I run this service in background (using a foreground service with a icon - no problem) without to affect the user experience and navigation in GUI? Is it possible to run it in a separated thread ?
 

JohnC

Expert
Licensed User
Longtime User
Maybe this would help...

 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Maybe this would help...

Thanks @JohnC ... I already used this lib but it's a very old resource... I'm not sure that will work and even it works I'm sure that we can't garante that will generate a widely compatible or even a stable code.
Do you already used this library in Android X?
 
Upvote 0

thetahsk

Active Member
Licensed User
Longtime User
look here and read the last post from Erel

 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
No, I never used it, but I did not see any recent posts that indicate the lib doesn't work with newer versions of Android.

Also, [U]agraham[/U] (the lib creator) does frequent this forum.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hello,
I have a problem here. I need to run a foreground service that requires about 1 or two minutes and some considerable level of processing and as in B4A it runs at the same thread of the gui this is freezing. The service has a loop (for ... next) which loads a json and decodes it, storing the results in a sqlite local database.
How can I run this service in background (using a foreground service with a icon - no problem) without to affect the user experience and navigation in GUI? Is it possible to run it in a separated thread ?

Hi Marcos

You are running a service ? If so what are you doing in the service ? I do a lot of processing in services in B4a and have not had issues with the Main activity or any activity freezing.

I would like to help you.

Regards

John.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hi Marcos

You are running a service ? If so what are you doing in the service ? I do a lot of processing in services in B4a and have not had issues with the Main activity or any activity freezing.

I would like to help you.

Regards

John.
Hello @Jmu5667 ,

thanks for reply! I have a very big json that I need to store in a database (sqlite). Sometimes more than 3000 records . When the user selects an item in a list, the list references a business with full products stock and commercial data. This data MUST to be stored locally in the phone - this is not so much space (less than 200Kb, but there are many fields and records.)
I'm testing the sql async functions as I think that to use threads library will cause some unexpected behaviors (like @Erel already told )... if sqlasync couldn't help, I will tell you here - your support will be welcome 👍

Thanks my friend!
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Since a service appears to run in it's own process/thread, maybe as @Jmu5667 mentioned, trying running your json routine in a service.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Since a service appears to run in it's own process/thread, maybe as [U]Jmu5667[/U] mentioned, trying running your json routine in a service.
Yes, I'm running in a service. But a service runs at the SAME process of GUI . A B4A app has only one thread, excepting when you use specific features like network (httpjob), async functions (sqlasync, copyasync) and permission methods.
You can recognize an async call (that runs in other thread) because if you want to receive the answer from the async call must to use the waitfor statement...
 
Last edited:
Upvote 0

priusfan

Member
Licensed User
Longtime User
Long time ago (2012), I extracted data from a DBII to create a sqlite DB.
Size of this sqlite DB: 32MB. 23 tables, some tables with many records (> 72 000).
This process was done automatically @5:00 AM.
There were 10 users and before going on the road and using the B4A application, they connected their tablet to their PC and using a small b4a application they just refreshed the local db from a central file-server.
Yes this is oldfashioned, but it was fast and easy for the users...
so, if you prepared your sqlite db instead of using json, maybe you would not suffer from the delay you have now...
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Long time ago (2012), I extracted data from a DBII to create a sqlite DB.
Size of this sqlite DB: 32MB. 23 tables, some tables with many records (> 72 000).
This process was done automatically @5:00 AM.
There were 10 users and before going on the road and using the B4A application, they connected their tablet to their PC and using a small b4a application they just refreshed the local db from a central file-server.
Yes this is oldfashioned, but it was fast and easy for the users...
so, if you prepared your sqlite db instead of using json, maybe you would not suffer from the delay you have now...
Hello,
I have no option for json - this is loaded from internet from a server over which we don't have control...
But I have good news - sqlasync worked fine running without to affect the user experience in GUI ! The only problem that I had is to change every call from traditional sql to sqlasync... but that's ok!
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
But I have good news - sqlasync worked fine running without to affect the user experience in GUI ! The only problem that I had is to change every call from traditional sql to sqlasync... but that's ok!
This is the correct solution and it is much simpler than implementing it yourself with the Threading library.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hello @Jmu5667 ,

thanks for reply! I have a very big json that I need to store in a database (sqlite). Sometimes more than 3000 records . When the user selects an item in a list, the list references a business with full products stock and commercial data. This data MUST to be stored locally in the phone - this is not so much space (less than 200Kb, but there are many fields and records.)
I'm testing the sql async functions as I think that to use threads library will cause some unexpected behaviors (like @Erel already told )... if sqlasync couldn't help, I will tell you here - your support will be welcome 👍

Thanks my friend!
Can you fetch block's of records; request the record count from your server, and then create a block size that suits your needs, this might balance the load for you.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Can you fetch block's of records; request the record count from your server, and then create a block size that suits your needs, this might balance the load for you.
The server basically sends the entire json... unfortunately. there is no option to choose the size or even get the record count from server.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
This is the correct solution and it is much simpler than implementing it yourself with the Threading library.
Thanks @Erel . You're correct as always 👍 . Like I told, I tested to use sqlasync and the delay is gone! So, to change to async mode fixed the issue.
 
Upvote 0
Top