Android Question HttpJob - WebService Synchronous

STIV RAEL GIACON

Member
Licensed User
Hello, good morning everyone

Estous making an application that will download via webservice the data of a remote bank to a local database in the mobile.

I am launching Job to do this inclusion but I would wait for the first Job to finish and then start the next Job. It is possible? In other words, I would like the App to be stopped by finishing the first job and then continuing to the next ones. Or a way to stop until all the jobs are completed.

B4X:
Sub atualizarTabelasApoio(bForce As Boolean)
    Dim rec As List
    rec.Initialize
    
    DBUtils.SQL.BeginTransaction
    Try
        ProgressDialogShow2("Sincronizando tabelas com a nuvem...", False )
        If bForce Then
            DBUtils.SQL.ExecNonQuery( "DELETE FROM Deposito" )
            DBUtils.SQL.ExecNonQuery( "DELETE FROM Empresa" )
        End If
        
        ' verifica se a tabela de deposito tem registro
        rec = DBUtils.ExecuteMemoryTable(DBUtils.SQL, "SELECT ID FROM Deposito",  Null, 0)
        
        Log("Rec Deposito local: " & rec.Size )
        If rec.Size = 0 Then
           sincronizeTabela( SINCRONIZE_DEPOSITO  )
        End If
        
        ' verifica se a tabela de empresas tem registro
        rec = DBUtils.ExecuteMemoryTable(DBUtils.SQL, "SELECT ID FROM Empresa",  Null, 0)
        
        Log("Rec Empresa local: " & rec.Size )
        If rec.Size = 0 Then
           sincronizeTabela( SINCRONIZE_EMPRESA )           
        End If
        
        DBUtils.SQL.TransactionSuccessful
    Catch
        ProgressDialogHide
        ToastMessageShow( "ERRO: " & LastException.Message, True)
    End Try
    DBUtils.SQL.EndTransaction   
    
    ProgressDialogHide
End Sub

Sub sincronizeTabela(JobName As String)
    Dim job As HttpJob
    job.Initialize(JobName, Me)
    
    Select Case JobName
    Case SINCRONIZE_DEPOSITO                       
       job.PostString( "https://www./xxx.php", _
                       "SELECT id, descricao FROM estoque_deposito ORDER BY id" )
       job.co
    Case SINCRONIZE_EMPRESA       
       job.PostString( "https://www./xxx.php", _
                       "SELECT id, razaosocial, fantasia, apelido, depositoid FROM empresa WHERE (Excluido=False) ORDER BY id" )   
    End Select
    
End Sub

Sub JobDone(Job As HttpJob)
    Dim parser As JSONParser
    Dim res    As String
    Dim l      As List
    
    ProgressDialogHide   
    Try
        If Job.Success Then           
           res = Job.GetString
           parser.Initialize(res)
          
           Log("Resposta do servidor: " & res)
          
           ' transforma em uma lista
           l = parser.NextArray
            
           Log("Num Rec:" & l.Size )
                    
           If l.Size > 0 Then       
              Select Job.JobName
              Case SINCRONIZE_DEPOSITO
                 DBUtils.InsertMaps( DBUtils.SQL, "Deposito", l)
                 ToastMessageShow("Tabela de Depósito concluída...", False)
                
              Case SINCRONIZE_EMPRESA
                 DBUtils.InsertMaps( DBUtils.SQL, "Empresa", l)
                 ToastMessageShow("Tabela de Depósito concluída...",  False)
                
              End Select
           End If     
        Else
            Log(Job.ErrorMessage)
            ToastMessageShow("Erro: " & Job.ErrorMessage, True)
        End If
        Job.Release
    Catch
        Log(LastException)
        ToastMessageShow("Erro: " & LastException, True)
    End Try
End Sub


In other words, I would like the user to be stopped until the two or more tables are updated when necessary.
 

STIV RAEL GIACON

Member
Licensed User
Erel, thank you very much for your response ...

I have already found this article even in it has a video showing a stream of execution of a routine very similar to mine.
But the execution flow in this example behaves the way I do not want it to launch the service of both Job and Activity and follows the execution releasing the APP to the user

What I want is that the code

B4X:
ProgressDialogShow2 ("Synchronizing Tables with the Cloud ...", False)

Keep running on the screen until the two Job is completed, ie, I want the APP to be stopped / stopped until all this process is finished, then use the command

B4X:
ProgressDialogHide

To release the run again to the user

The way this is not happening.

Forgive me the mistakes I'm using the translator ....
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Have you tried Wait For? If so and it still does not work as intended, post your project with your Wait For code (either as code here or even better include the project as an attachment via File->Export As Zip ). Just saying it does not work is not a lot to go by.
 
Upvote 0

STIV RAEL GIACON

Member
Licensed User
Hi Olivera thank you very much for your response.

And I ended up bypassing the problem I do not know if it was the right way or if there is a more elegant way but it worked perfectly for what I wanted.

I created a Public Map on the module there when I shoot a Job I jot down on this map that the job was started.
When Job finishes I mark on the Map that the job is finished.
That's easy, when all the Job on the Map are marked as completed I know the process is over.
With this template if I have ten Job running the APP standstill until all jobs are finished or some error occurs.
 
Upvote 0
Top