New feature: Resumable Subs - simpler asynchronous programming

LucaMs

Expert
Licensed User
Longtime User
Anyway, that quiz shows that you can lose yourself, like in "spaghetti programming".

Here you might think that I am criticizing the new features; go with the flow can be hard but to have a simulated multi-thread can be helpful.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
More "real world" example of using the new Wait For feature to get information from a remote database using jRDC2:

The standard way to send a request with jRDC2:
1. Call req.ExecuteQuery.
2. Handle JobDone event and call Manager.HandleJobAsync.
3. Handle the Result event.
All the results are handled by the same two subs so you use the Tag parameter to distinguish between different requests. It becomes more and more complicated when you add more types of requests.

With the new Wait For feature everything can be handled locally inside the sub. Note that it can be called multiple times safely.
B4X:
Sub GetRecord (id As Int)
   Dim req As DBRequestManager = CreateRequest
   Dim cmd As DBCommand = CreateCommand ("select", Array(id))
   Dim j As HttpJob = req.ExecuteQuery(cmd, 0)
   Wait For (j) JobDone(j As HttpJob) '<---- catch the relevant JobDone event here
   If j.Success Then
     req.HandleJobAsync(j)
     Wait For (req) Result(res As DBResult) '<---- catch the relevant Result event here
     Log ("Result for id: " & id)
     req.PrintTable(res)
   Else
     Log("ERROR: " & j.ErrorMessage)
   End If
   j.Release
End Sub

No need to use tags or to use any global variable to understand which request has returned.
 

Widget

Well-Known Member
Licensed User
Longtime User
Erel, keep those examples coming because it stimulates our brain cells. :)

Could it also be used to fill a CustomListView (from SQLite or remotely) in the background when the user scrolls in that direction? If there is a large database table of 100k rows, ideally only around 200 entries in the CustomListView needs to be filled at any one time in order to conserve memory and save time loading the data. As he scrolls to the end of the loaded data, new data is added to that end of the CustomListView and the older CLV entries are removed.
 

LWGShane

Well-Known Member
Licensed User
Longtime User
@susu - I'm in agreement. I have a big project planned and these features would do wonders for the project.
 

susu

Well-Known Member
Licensed User
Longtime User
The B4J implementation is almost ready to be released as a beta. It will probably be released in two weeks.
And how about B4A? :rolleyes:
 

LWGShane

Well-Known Member
Licensed User
Longtime User
And how about B4A? :rolleyes:
Probably like this:
B4J: Beta: April/May - GM: May/June
B4A: Beta: May/June - GM: June/July
You get the idea.

Note that "GM" means "Golden Master".
 

susu

Well-Known Member
Licensed User
Longtime User
How about:

B4J: Beta: Today - GM: Today+1
B4A: Beta: Tomorrow - GM: Tomorrow+1
;):p:D:rolleyes::)

Just kidding, I know Erel is super-man but he need to take a rest before continue to save our world :cool:
 

XbNnX_507

Active Member
Licensed User
Longtime User
B4X:
Sub Resumable

sql.ExecQueryAsync(eventSQL, QUERY, Null)

wait for  eventSQL_QueryComplete (Success As Boolean, Crsr As ResultSet) ' <- is this method gonna be IDE automcompleted or we have to know it beforehand?

End Sub
 

ilan

Expert
Licensed User
Longtime User
i just downloaded b4j beta and tried the resumeablesub function.

before i downloaded b4j beta i was wondering if i could use the Sleep function in games inside a timer_tick or any game engine update event.
and i was surprised that it worked. the tick event kept working and the code after the sleep function was called after the chosen time.

so thumbs up for that! great work erel
i hope the b4i update is near :)
 
Top