Android Question Run Sub in Background

Ferdari

Active Member
Licensed User
Longtime User
I have read a lot in the forum about ResumableSubs, Threads, AsyncTasks but i still dont get it, or dont know what i need.

My goal:
Open a custom dialog with layout that has a preview image and a empty combobox, then when layout loads, fill combobox in a background while the user sees preview image, then when sub finishes enable the combobox and a button in the layout.

What currently have:
Load the layout for custom dialog
load the preview image
show the custom dialog
call the sub i want to load in background
UI and App lags until the sub is finished loading data and filling the combobox then shows Dialog

Sub that loads the combobox list is called from a Code module:

B4X:
Sub ListAvailStickerPacksNames(Module As Object) As ResumableSub '<----Tried resumable sub expectin background task but same results
    Dim SPS As List=Starter.sc.getAllStickerPacks '<-----READS AND Return a LIST FROM OWN LIBRARY, IT READS VERY FAST
    Dim AvailableSPS As List
    AvailableSPS.Initialize
    For Each SP In SPS
        If Starter.sc.isAnimated(SP) = True Then
            If Starter.sc.getAllStickers(SP).Size < 30 Then
                AvailableSPS.Add(Starter.sc.GetStickerPackName(SP))
            End If
        End If
    Next
    CallSub2(Module,"SetComboBoxNames", AvailableSPS)
    'Return AvailableSPS
End Sub

Which is the best way to load a sub in Background after layout is loaded?

Im very confused
 
Last edited:

MicroDrie

Well-Known Member
Licensed User
Longtime User
What I find confusing about your story is that you can collect the images very quickly, but apparently there is a problem with rendering so that you let the user look at an image first. Then the user is suddenly presented with a choice.

That all seems very confusing for that user. I think it would be much more convenient to first take a look at the various lazy loading examples on the forum. Then you can have a choice made directly from the images to be chosen, and you will not run into limitations of dialogues.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
What I find confusing about your story is that you can collect the images very quickly, but apparently there is a problem with rendering so that you let the user look at an image first. Then the user is suddenly presented with a choice.

That all seems very confusing for that user. I think it would be much more convenient to first take a look at the various lazy loading examples on the forum. Then you can have a choice made directly from the images to be chosen, and you will not run into limitations of dialogues.
Agree. Something like preoptimized xCustomListview.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
If you find some examples are too difficult to understand then I recommend to check my first attempt on working with PCLV.
 
Upvote 0

Ferdari

Active Member
Licensed User
Longtime User
If you find some examples are too difficult to understand then I recommend to check my first attempt on working with PCLV.
Thanks for your help,
I'm already using a lazy loading, it loads a list of pictures very well, when user clicks an image the preview dialog(custom dialog) shows but need to load a the combobox first to do some actions, like copying, clone, edit, etc, first it needs to load the available packs to copy/clone/edit the picture, i found that when having a lot of Packs(of images
sc.getAllStickerPacks) it takes some time to load and lags/pauses/freezes the app for some seconds until the combobox is filled, i want to separate this task so the user don't experience this lag. it don't use internet to load, it loads the combobox from the app local files.

So the goal is to run this Sub in a Background task:
B4X:
Sub ListAvailStickerPacksNames(Module As Object) As ResumableSub '<----Tried resumable sub expectin background task but same results
    Dim SPS As List=Starter.sc.getAllStickerPacks '<-----READS AND Return a LIST FROM OWN LIBRARY, IT READS VERY FAST
    Dim AvailableSPS As List
    AvailableSPS.Initialize
    For Each SP In SPS
        If Starter.sc.isAnimated(SP) = True Then
            If Starter.sc.getAllStickers(SP).Size < 30 Then
                AvailableSPS.Add(Starter.sc.GetStickerPackName(SP))
            End If
        End If
    Next
    CallSub2(Module,"SetComboBoxNames", AvailableSPS)
    'Return AvailableSPS
End Sub
Do you know if its possible?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I don’t know does it help if to use background sub. What I can think of to improve the performance is to resize the images to smaller size or preload them before showing the page.
 
Upvote 0

Ferdari

Active Member
Licensed User
Longtime User
I don’t know does it help if to use background sub. What I can think of to improve the performance is to resize the images to smaller size or preload them before showing the page.
Thanks for your suggestion but it is not related to images, im returning information from my lib from local data but when doing it lags the app(it returns only a list of available packs(strings with ids and names) and sets the names to a combobox), thats why i need to do this task on background, but i really dont know if it is possible, maybe @Erel has a possible solution.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
If it just reading simple data it shouldn’t be lag. You may just load the data in foreground and doesn’t need it run at background. How much items in the list? My guess is the getAllStickers(SP).Size sub need to make calculation thus causing the lag. Without clearly see what the sub does it is hard to tell.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
Ferdari we all wants to help you. This statement
it don't use internet to load, it loads the combobox from the app local files.
Rise the question of why is your program slow. The only way to help you to solve this issue is to make a small program which has your routines and demonstrate it. Most likely you have made a mistake and overlooked it over and over again like we all do sometimes.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I’ve had the same issue with CLV and listing all applications on a phone. My work around was to (on program start) load the application information into a SQLite database. I then used the information in that database to populate the CLV. The lag issues resolved itself using this method. Note: this works very well for sources that do not update themselves very often. If updates do happen now and then, you need a mechanism to refresh the database.
 
Upvote 0

Ferdari

Active Member
Licensed User
Longtime User
Im rechecking the code of my Libray, maybe something is slowing the process, thanks for your suggestions, but i still want Run Sub in background, it could be useful also for other applications.
 
Upvote 0
Top