Android Question Calling a resumableSub in MapFragment1_MarkerClick()

toby

Well-Known Member
Licensed User
Longtime User
If I understand correctly, while the return type of MapFragment1_MarkerClick() is expected to be Boolean, calling a resumableSub within this event (MapFragment1_MarkerClick) requires that the return type of MapFragment1_MarkerClick() to be changed to ResumableSub.

Did I miss anything? How to solve this problem?

B4X:
Sub MapFragment1_MarkerClick (SelectedMarker As Marker) As Boolean 'Return True to consume the click
  
   Wait For(Sum(1, 2)) Complete (Result As Int)
   Log("result: " & Result)
   Log("after sum")

    Return True
End Sub

Sub Sum(a As Int, b As Int) As ResumableSub
   Sleep(100)
   Log(a + b)
   Return a + b
End Sub

:oops:

TIA
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Remember the first rule of resumable subs: from the calling method perspective, Wait For and Sleep are equivalent to Return. This means that you cannot call Wait For in a sub that is supposed to return a value (except of the special ResumableSub type).

This means that you cannot call Wait For or Sleep inside this event sub. You can call a resumable sub that will do whatever you need.
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
You can call a resumable sub that will do whatever you need.

The above sentence confuses me. Could you clarify? You meant outside of event Subs such as the one in question?

Any sub calling a resumableSub becomes a resumableSub itself. This behavior is causing me problems. Is there a way around?

In my case, I retrieve database data in the resumableSub. So the resumableSub is indeed needed.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub MapFragment1_MarkerClick (SelectedMarker As Marker) As Boolean 'Return True to consume the click  
  AnotherSub
    Return True
End Sub

Sub AnotherSub
 'Do whatever you like here.
  Wait For(Sum(1, 2)) Complete (Result As Int)
End Sub

Sub Sum(a As Int, b As Int) As ResumableSub
   Sleep(100)
   Log(a + b)
   Return a + b
End Sub

Any sub calling a resumableSub becomes a resumableSub itself
No. Only if calling it with Wait For.
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
B4X:
Sub MapFragment1_MarkerClick (SelectedMarker As Marker) As Boolean 'Return True to consume the click 
  AnotherSub
    Return True
End Sub

Sub AnotherSub
 'Do whatever you like here.
  Wait For(Sum(1, 2)) Complete (Result As Int)
End Sub

Sub Sum(a As Int, b As Int) As ResumableSub
   Sleep(100)
   Log(a + b)
   Return a + b
End Sub


No. Only if calling it with Wait For.

Now MapFragment1_MarkerClick event
can finally call Sum() indirectly, however it can not depend on the result from Sum(). Any suggestions?
 
Upvote 0
Top