Android Question CallSub or Activity.Finish prior to Job Release

swabygw

Active Member
Licensed User
Longtime User
In the course of updating my code for the new WaitFor feature (which is fantastic, by the way), I came across some instances where code is processing the HttpJob result and, either, calls another sub (e.g., CallSub(...)) or closes the activity (i.e., Activity.Finish) as a result of the value of returned from the HttpJob. So, I think that the normal implementation with the new WaitFor goes like this:
B4X:
Wait For (SomeHttpJob) JobDone(j As HttpJob)
If j.Success Then
End If
j.Release

But, occasionally, I have this/these situation(s):
B4X:
Wait For (SomeHttpJob) JobDone(j As HttpJob)
If j.Success Then
    CallSub(Main, "SomeSub")
    '...or...
    Activity.Finish
End If
j.Release  'Is this still needed?  Should it go before the CallSub or Activity.Finish?

So, my question is: for this/these latter situation(s), where does the j.Release go? Is j.Release still even necessary?

Is this a possible solution:
B4X:
Wait For (SomeHttpJob) JobDone(j As HttpJob)
If j.Success Then
    j.Release
    CallSub(Main, "SomeSub")
    '...or...
    Activity.Finish
End If

Out of curiosity, what happens if j.Release is never called, i.e., just omitted altogether?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that you don't need to use CallSub to call a sub. You can call it directly.

To answer your question:
Activity.Finish doesn't do anything immediately. It sends a message to the internal message queue. Only when this message will be processed will the activity finish. This will always happen after the current code completes.
So there is nothing special that you need to do here.

It also doesn't matter whether you call another sub. Just make sure that j.Release is eventually called. It deletes the temporary file that holds the downloaded data.
 
Upvote 0

swabygw

Active Member
Licensed User
Longtime User
Thanks - I used CallSub because the code is executing in another Activity (sorry, I should have mentioned this), which, in this example, is not Main...but needs to execute a Sub which is in Main (e.g., this code executes in Starter and, depending on a value returned from the HttpJob, does something over in Main). But, knowing now that the j.Release just deletes a temporary file and no other memory manipulations, I'm thinking that putting the j.Release within the j.Success clause should be okay, like this:
B4X:
Wait For (SomeHttpJob) JobDone(j As HttpJob)
If j.Success Then
    '...finish using "j"...
    j.Release
    CallSub(Main, "SomeSub")'
    '...or...
    Activity.Finish
End If
(I wasn't sure if j.Release would actually do anything if it was within a section being tested by j.Success...like, as if "j" was held in memory until the section is completed)
 
Upvote 0

udg

Expert
Licensed User
Longtime User
What if j.Success returns False?
Since Activity.Finish will end your app only after the current code section is over, I'll stay with your former scheme that calls j.Release in any condition:
B4X:
Wait For (SomeHttpJob) JobDone(j AsHttpJob)
If j.Success Then
  CallSub(Main, "SomeSub")
  '...or...
  Activity.Finish
End If
j.Release
 
Upvote 0

swabygw

Active Member
Licensed User
Longtime User
If j.Success returns False, I'm guessing that there's nothing to be released...because it failed...making the release unnecessary. Just a guess...
 
Upvote 0

swabygw

Active Member
Licensed User
Longtime User
Actually, to add a further note, I used the HttpJob to do a PostString - retrieve text. I'm not sure if the PostString also creates a temporary file, like Download might.
 
Upvote 0
Top