Android Question is regex.split need to be flushed ?

Addo

Well-Known Member
Licensed User
Longtime User
i am trying to handle multiable in Asyncstream by doing the following

B4X:
Public Sub NewData (data() As Byte)
 
Dim msg As String


msg = BytesToString(data, 0, data.Length, "UTF-8")
msg = msg.Trim



Dim param As String = msg
Dim paramnum() As String = Regex.Split("\&", param)


If paramnum(0) = "Command1" Then
 
For i = 1 To paramnum.Length-1
If paramnum(i) <> "" Then
 
log(paramnum(i))

End If

Sleep(0)

Next


If connected = True Then
SendData(Bconv.StringToBytes("1"&"Command2~", "UTF-8"))
End If

End If

If paramnum(0) = "Command2" Then
Log(paramnum(1))
End If

End Sub

in the newdata first i receive command1 and parse it then send command2 and wait for response then

if the paramnum equals command2 then parse it but when command2 arrived and paramnum(0) equals command2 the log show old data from paramnum(1) not the one that expected is what i am doing is correct ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Why aren't you formatting the code? Ctrl + A, Ctrl + F

Regex.Split doesn't need to be flushed and will never return incorrect results.

Why are you calling Sleep(0)? Test performance in Release mode. It will most probably be fast enough.

If you are calling Sleep then it is possible that NewData will be raised again while the previous instance is still running.
 
Upvote 0

Addo

Well-Known Member
Licensed User
Longtime User
In my previous question about xcustomlistview I have suggested to call sleep(0) I will possibly use it through this loop

If I remove it the hanging ui will back again

Should I use else if statement ? So the loop code couldn't effect the rest of code ?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I think you need to re-arrange your code because it does not quite do what you think it does:
B4X:
Public Sub NewData (data() As Byte)

Dim msg As String
msg = BytesToString(data, 0, data.Length, "UTF-8")
msg = msg.Trim

'Sanity check
If msg.Length = 0 Then
   Log("Error: Empty message received")
   Return
End If

'Dim param As String = msg
Dim paramnum() As String = Regex.Split("\&", msg)

'Let's see what command came in
If paramnum(0) = "Command1" Then
   'Let's process our parameters for Command1
   For i = 1 To paramnum.Length-1
      'Log all non-empty parameters
      If paramnum(i) <> "" Then
         log(paramnum(i))
      End If
      'Sleep(0)
   Next
   'Let's send out for Command2
   If connected = True Then
      SendData(Bconv.StringToBytes("1"&"Command2~", "UTF-8"))
   End If
Else If paramnum(0) = "Command2" Then
   'Command2 came in, let's log the single parameter associated with it
   'Let's make sure it came in correctly
   If paramnum.Length = 2 Then
      Log(paramnum(1))
   Else
      Log("Error: Expected 1 parameter with Command2 but received: " & paramnum.Length - 1 & " commands")
      Log("Error (cont.): Message received was: " & msg)
   End If
Else
   'We neither received Command1 nor Command2, something is up
   Log("Error: Neither Command1 nor Command2 was received.")
   Log("Error (cont.): Message received was: " & msg)
End If
End Sub
Note: Not tested. Coding/semantic errors may exist.
 
Upvote 0
Top