Android Question Service & App does not respond

luke2012

Well-Known Member
Licensed User
Longtime User
Hi all,
I developed a (simple) service that process (put null) thousands of items within the @Erel KVS Cloud (https://goo.gl/doKFpg).

PROBLEM
The user got the "App does not respond - Wait or Close" message.
Note that probably this problem is related to my custom APIs implementation and not to the KVS Cloud APIs.

WISH
I wish that this service run always (background mode) to check the older items (ex. older than x days) and delete them without block the main thread.

The service is configured in this way:

STARTER
B4X:
Sub Service_Start (StartingIntent As Intent)
       StartService (srvDeleteOlderItems)
End Sub

SERVICE
B4X:
Sub Service_Create
       n.Initialize
       n.Icon = "icon"
       n.Sound = False
       n.AutoCancel = False
       n.OnGoingEvent = True
       n.SetInfo("", "", Main)

       Service.StartForeground(1, n)
End Sub

Sub Service_Start (StartingIntent As Intent)

StartServiceAt("", DateTime.Now + 21600 * DateTime.TicksPerSecond, True) 'schedule the next task to run in 21600 (6 ore) seconds.
'Sub that loop over the thousands of items and put the null
'Note that the following sub use custom APIs (to implement my app logic) developed on top of the KVS Cloud APIs

DeleteOlderItems (7)

End Sub

Is the a way to configure the service to avoid the main thread block ?

Thanks in advance for your help :)
 
Last edited:

XbNnX_507

Active Member
Licensed User
try to use sleep(0) inside your loop.:) And make sure to call
B4X:
StartServiceAt("", DateTime.Now + 21600 * DateTime.TicksPerSecond, True)
AFTER the loop is done. Bye.
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
No. Services code runs on the main thread.

What is your app doing that holds the main thread?

Loop on thousand of KVS cloud items to delete older (putted) items.

1) So isn't any way to assure that the app doesn't display "App does not respond - Wait or Close" ?
2) Also "Start at boot" option start the Service within the main thread?
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
1) There are many ways.
If the task is related to SQL processing then you should switch to the async methods.
You can also split the task and call Sleep(0) every second to allow the UI to be updated.

2) Yes.

1) Yes the task is related to SQL processing.
So I can create "DeleteOlderItems (7)" sub as async sub to avoid the "App does not respond - Wait or Close"?
 
Upvote 0
Top