B4J Question For Each ...... Next Statement

Declan

Well-Known Member
Licensed User
Longtime User
I am using the following in JobDone to retrieve data:
B4X:
            If result.Tag = "not sent" Then 'query tag
                For Each records() As Object In result.Rows
                    MyID = (records(result.Columns.Get("id")))
                    MyCity = (records(result.Columns.Get("city")))
                    MyProduct1 = (records(result.Columns.Get("product1")))
                    
                    Log("Not Sent: " & MyID & " " & MyCity & " " & MyProduct1)
                    Log("Gotcha")
                        
                    getFBtopic(MyCity) 
                Next
            End If
            
            If result.Tag = "get fbtopic" Then 'query tag
                For Each records() As Object In result.Rows
                    MyTopic = (records(result.Columns.Get("username")))
                    
                    Log("Topic: " & MyTopic)
                Next
            End If

This returns:
B4X:
Waiting for debugger to connect...
Program started.
HandleJob: 8
Not Sent: 35 Centurion Life Policy
Gotcha
Not Sent: 36 Centurion Life Policy
Gotcha
Not Sent: 37 Centurion Life Policy
Gotcha
HandleJob: 1
Topic: 0822206575
Topic: 0742355566
HandleJob: 0
Topic: 0822206575
Topic: 0742355566
HandleJob: 0
Topic: 0822206575
Topic: 0742355566

What I an attempting to achieve is to read each row of result.Tag = "not sent"
Then call getFBtopic(MyCity).
So, my output to Log should be:
B4X:
Not Sent: 35 Centurion Life Policy
Gotcha
Topic: 0822206575
Topic: 0742355566
Not Sent: 36 Centurion Life Policy
Gotcha
Topic: 0822206575
Topic: 0742355566
Not Sent: 37 Centurion Life Policy
Gotcha
Topic: 0822206575
Topic: 0742355566

Something is querky with my For Each ...... Next Statement
 

Declan

Well-Known Member
Licensed User
Longtime User
It seems that you are badly handling more than one "JobDone" (and you didn't post this part).

https://www.b4x.com/android/forum/threads/b4x-okhttputils2-with-wait-for.79345/#content
I did go through the link you sent, but could not get my head around it :(
I only have 1 "Sub JobDone(job As HttpJob)" in my project.
1).What I need to do is get the first row records in the above "If result.Tag = "not sent" Then"
2).I then call "Sub getFBtopic"
3).I then get the record in the above "If result.Tag = "get fbtopic" Then"
4).I then assemble a string and and call "Private Sub SendMessage"
I then repeat steps 1 through 4 until I have obtained all records in "If result.Tag = "not sent" Then" "For Each records() As Object In result.Rows"
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Because you are using JobDone by itself instead of Wait For. In your for next loop, you are firing off getFBTopic calls irregardless if the previous call to getFBTopic finished the call to whatever you are calling (jRDC2?). So the code is doing the following (and the logs reflect it)

1) result.tag = "not sent"
2) For each record returned:
2a) Log "Gotcha" and other info
2b) Call method getFBtopic(MyCity)
2c) Go to 2
This will finish (the looping of 2) before any calls are made to your JobDone routine where the tag is set to"get fbtopic". Why? Because the calls happen asynchronously. If you would use Wait For, you can actually synchronize your asynchronous calls and achieve the results you want. You can also use some global variables and ensure that you only make one call to getFBtopic(MyCity) and not make another until you have successfully processed a JobDone routine where the tag is set to "get fbtopic". You probably are not going to get support for this method from this forum.
 
Upvote 0
Top