Quiz : A random question (not Erels)

thedesolatesoul

Expert
Licensed User
Longtime User
I would like to post a quiz like question too.

If you have the following code (pseudocode, dont kill me for syntax errors) when doing Http Requests.

B4X:
Sub ProcessNextTask
      If TaskCount > 0 Then
          TaskCount = TaskCount - 1
          req = CreateHttpRequest(FileDownloadLink)
          ExecuteRequest(req)
      End If
End Sub

Sub ExecuteRequest(req as HttpRequest)
     If File.ExternalWritable = False then
        'Abort Download but process Next Task
        ProcessNextTask
     Else
         HttpClient1.Execute(req, taskid)
     End If
End Sub

Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Response.GetAsynchronously("response", File.OpenOutput(TempFolder, TaskId, False), _
      True, TaskId)
End Sub

Sub Response_StreamFinish (Success As Boolean, TaskId As Int)
      ProcessNextTask
End Sub

Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
      ProcessNextTask
End Sub

When will you get an error with this code, what error will you get, and how to resolve the issue?
 

thedesolatesoul

Expert
Licensed User
Longtime User
When processing a large number of tasks and the external storage is not writable, you can get a stack overflow.
This is because ProcessNextTask and ExecuteRequest will be calling each other and never being able to complete the thread.
The solution to this is to use CallSubDelayed in ExecuteRequest to call back ProcessNextTask, this ensures the thread can exit before the next task is executed.
In the older versions of B4A I used a Timer to simulate CallSubDelayed to avoid this problem.
 
Upvote 0
Top