iOS Question [Solved] Example with wait-for

D

Deleted member 103

Guest
Hi,

I have a problem with the use of "Wait-For".

When I start my example the first time, the entries in the scrollview are entered twice when the linewith "Wait-For" is activated .
But if I deactivate this line, my ProgressBar is not visible.
How can I solve this problem?
 

Attachments

  • ScrollView-Example.zip
    4.2 KB · Views: 164
  • iPhone6_Example_whit_Wait-for.png
    iPhone6_Example_whit_Wait-for.png
    73.4 KB · Views: 130
  • iPhone6_Example_without_Wait-for.png
    iPhone6_Example_without_Wait-for.png
    60.2 KB · Views: 130

OliverA

Expert
Licensed User
Longtime User
The Sleep(0) allows pnlRoot_Resize to fire early.

Without the Wait For/Sleep, pnlRoot_Resize is called, your list is cleared, the 15 items are displayed, pnlRoot_Resize is called, your list is cleared and the 15 items are displayed.

With the Wait For/Sleep, pnlRoot_Resize is called, your list is cleared, the first item is displayed, Wait For/Sleep happens, allowing pnlRoot_Resize to fire "early". Now it gets interesting: Your list is cleared (removing the first item that was placed by the first call to FillScrollView) and a second FillScrollView is filling 15 items and the first FillScrollView is continuing the remaining 14 items. With the Wait For/Sleep, they bounce back and forth between each other, creating duplicate entries.

Ninja'd by @Erel...
 
Upvote 0
D

Deleted member 103

Guest
1. Why waste your time with ScrollView??? Use xCLV.

2. If the list is loaded too slowly then use lazy loading and it will be displayed immediately.

3. Don't assume that pnlRoot_Resize will only be called once.
1. because ScrollView is simply used in my app.
2. it doesn't load too slowly, but it may take a 1-2 seconds to display everything, but that's not the problem.
3. therefore the scrollview is emptied every time ( see code below)
B4X:
Private Sub pnlRoot_Resize (Width As Float, Height As Float)
    ScrollView1.ContentWidth = Width
    FillScrollView
End Sub

Sub FillScrollView
    ScrollView1.Panel.RemoveAllViews
    ScrollView1.Panel.Height = 0

    'Starter.hd.ProgressDialogShow(starter.language.Value("strPleaseWait"))
    ShowProgressBar(True, 0)
    
    For i = 0 To 15
        Log("i=" & i)
        AddNewItem(i)
        
        Wait For (ShowProgressBar(True, i / 15)) complete(Result As Object)
    Next

    'Starter.hd.ProgressDialogHide
    ShowProgressBar(False, 0)
End Sub

@OliverA
With the Wait For/Sleep, pnlRoot_Resize is called, your list is cleared, the first item is displayed, Wait For/Sleep happens, allowing pnlRoot_Resize to fire "early". Now it gets interesting: Your list is cleared (removing the first item that was placed by the first call to FillScrollView) and a second FillScrollView is filling 15 items and the first FillScrollView is continuing the remaining 14 items. With the Wait For/Sleep, they bounce back and forth between each other, creating duplicate entries.
then I shouldn't use the Sleep function in my case, or what do you think?
 
Upvote 0
D

Deleted member 103

Guest
Ok. Your decision. Any other suggestion you get is not relevant as you are advancing in the wrong direction (backwards).
The same example now with xCustomListView, no change. :)
The double entries are only at the first start.
If you change the iphone orientation, there are no longer double entries.
 

Attachments

  • Example_whit_xCLV.zip
    4.3 KB · Views: 146
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
No reason to call Sleep(0) inside ShowProgressBar. Simpler to put it in FillScrollView.

However the actual solution is to use the "index pattern": [B4X] Resumable subs and the index pattern

B4X:
Sub FillScrollView
    ClvIndex = ClvIndex + 1 'global int variable
    Dim MyIndex As Int = ClvIndex
    xClv.Clear
    ShowProgressBar(True, 0)
    Dim count As Int = 15
    For i = 0 To count
        AddNewItem(i)
        ShowProgressBar(True, i / 15)
        Sleep(0)
        If MyIndex <> ClvIndex Then Return
    Next
    ShowProgressBar(False, 0)
End Sub

Private Sub ShowProgressBar(Visible As Boolean, Value As Double)
    pnlProgressBar.BringToFront
    pnlProgressBar.Visible = Visible
    lblProgress.Width = Value * (ProgressBar.Width - 2dip)
End Sub

Still, if it takes more than 300ms to fill the list then it is a good idea to switch to lazy loading. It will load immediately.

It is also not the best idea to refill the list whenever the page is resized.
A better solution is to create the items with the designer. This way the cell items will be resized automatically when the screen size changes.
 
Upvote 0
D

Deleted member 103

Guest
It is also not the best idea to refill the list whenever the page is resized.
A better solution is to create the items with the designer. This way the cell items will be resized automatically when the screen size changes.
Thank you, I try it.
 
Upvote 0
Top