B4J Question Wait For not calling method

MathiasM

Active Member
Licensed User
Hello

I had this code, it basically fetches usergroups from a platform (mInformat.GetClasses()) and pushes them to our Google Workspace. It works perfectly.:

B4X:
Sub AppStart (Args() As String)
    mInformat.Initialize()
    Start
    StartMessageLoop
End Sub

Sub Start()
    
    Wait For(mInformat.GetClasses()) Complete (ClassMap As Map)
    For Each classCode As String In ClassMap.Keys
        Dim sClass As StudentClass = ClassMap.Get(classCode)
        Wait For(Google.GroupExists(sClass.Email)) Complete (GroupExists As Boolean)
        If GroupExists = False Then
            Google.CreateGroup(sClass.Email, sClass.Classcode, "Generated by ITG on " & DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now))
            Log("Created " & sClass.Classcode)
        Else
            Log(sClass.Classcode & " already exists.")
        End If
    Next
    
End Sub

But now I want to extend this code and create users. But ofcourse, the group creation must be finished before trying to add users to groups.
Now, I wanted to solve this easily, so I created a resumable Sub "CreateGroups" and call it with Wait For in Start, and create another method "CreateUsers" right underneath it.

B4X:
Sub AppStart (Args() As String)
    mInformat.Initialize()
    Start
    StartMessageLoop
End Sub

Sub Start()
    
    Wait For CreateGroups
    
End Sub

Sub CreateGroups

Wait For(mInformat.GetClasses()) Complete (ClassMap As Map)
    For Each classCode As String In ClassMap.Keys
        Dim sClass As StudentClass = ClassMap.Get(classCode)
        Wait For(Google.GroupExists(sClass.Email)) Complete (GroupExists As Boolean)
        If GroupExists = False Then
            Google.CreateGroup(sClass.Email, sClass.Classcode, "Generated by ITG on " & DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now))
            Log("Created " & sClass.Classcode)
        Else
            Log(sClass.Classcode & " already exists.")
        End If
    Next

End Sub

Now nothing works. And the odd thing is, Sub CreateGroups is throwing a warning that it's not used, yet I'm calling it with the Wait For in the Start sub.

I was thinking I did something stupid with the StartMessageLoop again, altough it shouldn't be, as the Wait For in the Start method should create a return and go to the StartMessageLoop line as just before.

Everytime I think I really grasp Wait Fors, I still manage to do something wrong o_O
Can anyone point me to my obvious mistake? Why is the Sub CreateGroups not called with this line:
Wait For CreateGroups

Thanks for any help or guidance!
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Creategroups is not what you have to wait for. Its the result,

Do:

B4X:
Sub Start()
CreateGroups
wait for CreateGroups_finished
end sub

Sub CreateGroups
...
callsubdelayed(me,"creategroups_finished)
end sub

then it will work as you expect, and this is why CreateGroups shows as not used, you have an invalid syntax.
 
Upvote 0

MathiasM

Active Member
Licensed User
Creategroups is not what you have to wait for. Its the result,

Do:

B4X:
Sub Start()
CreateGroups
wait for CreateGroups_finished
end sub

Sub CreateGroups
...
callsubdelayed(me,"creategroups_finished)
end sub

then it will work as you expect, and this is why CreateGroups shows as not used, you have an invalid syntax.

Thanks @Enrique Gonzalez R
Your explanation is spot on!
 
Upvote 0
Top