Android Question array value from CLV get view result error when j.release

Sub getstokitem As ResumableSub
For i = 0 To ClvBO.Size - 1
Dim ABO As databackorder = ClvBO.GetValue(i)
Dim panel As B4XView = ClvBO.GetPanel(i)
panel.IsInitialized
Dim txtkodebo As B4XView = ClvBO.GetPanel(i).GetView(0)
If txtkodebo.Text.Length>0 Then
ABO.txtkodebarang=txtkodebo.Text
Dim req As DBRequestManager=CreateRequest
Dim cmd As DBCommand=CreateCommand("getstokitem", Array(ABO.txtkodebarang))
Wait for (req.ExecuteQuery(cmd,0,Null)) JobDone(j As HttpJob)
If j.Success Then
req.HandleJobAsync(j,"req")
wait for (req) req_result(res As DBResult)
If res.Rows.Size=0 Then
MsgboxAsync("Tidak ada data", "Info")
Else
For Each row() As Object In res.Rows
Dim SI As StokItem
SI.txtonhand=row(0)
Dim Pnl06 As B4XView = XUI.CreatePanel(Null)
Pnl06.Color = XUI.Color_Gray
Pnl06.SetLayoutAnimated(0, 0, 0, ClvBO.AsView.Width, 30dip)
PCLV6.AddItem (25dip, XUI.Color_Cyan, SI)
PCLV6.Commit
Next
End If
Else
Log("ERROR: " &j.ErrorMessage)
End If
End If
Next
j.Release
End Sub
 

JohnC

Expert
Licensed User
Longtime User
(Please code any code in code tags: <code>your code</code> by clicking the "</>" button in the editor toolbar)

Here is what ChatGPT says:

The issue you're encountering with the j.Release statement being flagged for an array value error is likely due to the positioning and usage of j.Release within the loop and potentially outside the scope where j is defined. In B4X (Basic4Android, Basic4iOS, Basic4J), j.Release is used to release the resources associated with an HttpJob after its completion. The problem arises because j.Release is called outside the loop and the If j.Success block where j is actually defined and used. This can cause scope and logic issues, especially when dealing with asynchronous calls within a loop.

Here's a revised version of your subroutine with corrections and adjustments to ensure j.Release is called in the correct scope and to prevent other potential issues:

B4X:
Sub getstokitem As ResumableSub
    Dim req As DBRequestManager = CreateRequest
    For i = 0 To ClvBO.Size - 1
        Dim ABO As databackorder = ClvBO.GetValue(i)
        Dim panel As B4XView = ClvBO.GetPanel(i)
        If panel.IsInitialized Then ' Ensure panel is initialized
            Dim txtkodebo As B4XView = panel.GetView(0)
            If txtkodebo.Text.Length > 0 Then
                ABO.txtkodebarang = txtkodebo.Text
                Dim cmd As DBCommand = CreateCommand("getstokitem", Array(ABO.txtkodebarang))
                Wait For (req.ExecuteQuery(cmd, 0, Null)) JobDone(j As HttpJob)
                If j.Success Then
                    req.HandleJobAsync(j, "req")
                    Wait For (req) req_result(res As DBResult)
                    If res.Rows.Size = 0 Then
                        MsgboxAsync("Tidak ada data", "Info")
                    Else
                        For Each row() As Object In res.Rows
                            Dim SI As StokItem
                            SI.txtonhand = row(0)
                            Dim Pnl06 As B4XView = XUI.CreatePanel(Null)
                            Pnl06.Color = XUI.Color_Gray
                            Pnl06.SetLayoutAnimated(0, 0, 0, ClvBO.AsView.Width, 30dip)
                            PCLV6.AddItem(25dip, XUI.Color_Cyan, SI)
                            PCLV6.Commit
                        Next
                    End If
                Else
                    Log("ERROR: " & j.ErrorMessage)
                End If
                j.Release ' Make sure to release the job here, inside the loop and if block
            End If
        End If
    Next
End Sub

Key changes and considerations:
  • HttpJob Release: The j.Release call is moved inside the loop and within the If j.Success block to ensure that it is called for each job created within the loop. This addresses the scope issue and ensures that each HttpJob is properly released after its completion.
  • Initialization Check: Added a check for panel.IsInitialized to prevent potential errors when interacting with uninitialized views.
  • DBRequestManager Reuse: Initialized DBRequestManager (req) outside the loop to reuse it for all database requests. This is more efficient than creating a new request manager for each iteration but depends on your application's architecture and the thread-safety of DBRequestManager.
  • Logic Flow and Error Handling: Ensured that logic flow and error handling are properly scoped and managed within the loop to address the asynchronous nature of the database requests and UI updates.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…