Android Question Wait for the cycle For Each?

rscheel

Well-Known Member
Licensed User
Longtime User
How can I stop the cycle until it receives the answer from the function ComparaFecha.

B4X:
For Each quot As Map In quotes
            estado = quot.Get("estado")
            If estado <> 2 Then
                If estado == "null" Then
                    ComparaFecha(quot.Get("TABLE_NAME"), quot.Get("UPDATE_TIME"))
                    'Wait For ?
                End If
            Else
                ToastMessageShow(quot.Get("mensaje"), False)
                Log(quot.Get("mensaje"))
            End If
        Next

Thanks.
 

rscheel

Well-Known Member
Licensed User
Longtime User
I bring the name and the date of the last update of the tables that are in the server, Compare Date compare the date of each table of the sqlite bd of the telephone with the date that I get from the server, 5 are the names of the tables that I bring, If the date is different I delete the data from the telephone table and bring the updated data from the server.

What I want to achieve is that if a table is to be updated finish the process of updating that table before moving on to check the next one.
 
Upvote 0

rscheel

Well-Known Member
Licensed User
Longtime User
I guess since I already have the code it should already be asynchronous, this is the most complete code is the idea I'm looking for.

B4X:
Sub ObtieneUpdTablas
    Dim j As HttpJob
    Dim estado As String
    j.Initialize("", Me) 'name is empty as it is no longer needed
    j.download2(ModConn.Url_php_mysql("UPDTABLAS"), Array As String ("REQUEST_METHOD", "GET_UPDTABLA", "USER_ID", ModConn.IMEI.GetDeviceId))
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        'The result is a json string. We parse it and log the fields.
        Dim jp As JSONParser
        jp.Initialize(j.GetString)
        Dim quotes As List = jp.NextArray
        For Each quot As Map In quotes
            estado = quot.Get("estado")
            If estado <> 2 Then
                If estado == "null" Then
                    'Log("Nombre Tabla: "&quot.Get("TABLE_NAME"))
                    'Log("Fecha Tabla: "&quot.Get("UPDATE_TIME"))
                    ComparaFecha(quot.Get("TABLE_NAME"), quot.Get("UPDATE_TIME"))
                End If
            Else
                ToastMessageShow(quot.Get("mensaje"), False)
                Log(quot.Get("mensaje"))
            End If
        Next
    End If
    j.Release
End Sub

Sub ComparaFecha(Tabla As String, Fecha As String)
    Dim TablaName As String
    ModConn.c = ModConn.s.ExecQuery("SELECT TABLE_NAME FROM upd_tablas WHERE TABLE_NAME = '"& Tabla &"' AND UPDATE_TIME <> '"& Fecha &"';")
    If ModConn.c.RowCount <> 0 Then
        For i = 0 To ModConn.c.RowCount-1
            ModConn.c.Position = i
            TablaName = ModConn.c.GetString("TABLE_NAME")
        Next
    End If
    ModConn.c.Close
    Select TablaName
        Case "materiales"
            Log("Actualizar materiales")
            ObtieneMateriales(Fecha)
        Case "mecanico"
            Log("Actualizar mecanicos")
            ObtieneMecanicos(Fecha)
        Case "tipo_falla"
            Log("Actualizar tipos de fallas")
            ObtieneTpfalla(Fecha)
        Case "tipos_eventos"
            Log("Actualizar tipos de eventos")
            ObtieneTiposEventos(Fecha)
        Case "ubicacion_falla"
            Log("Actualizar ubicacion falla")
            ObtieneUbFalla(Fecha)
        Case Else
            Log("No hay tablas para actualizar.")
    End Select
End Sub

Sub ObtieneTiposEventos (FechaServer As String)
    Dim j As HttpJob
    Dim estado As String
    j.Initialize("", Me) 'name is empty as it is no longer needed
    j.download2(ModConn.Url_php_mysql("TPEVENTOS"), Array As String ("REQUEST_METHOD", "GET_TPEVENTOS", "USER_ID", ModConn.IMEI.GetDeviceId))
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        'The result is a json string. We parse it and log the fields.
        Dim jp As JSONParser
        jp.Initialize(j.GetString)
        Dim quotes As List = jp.NextArray
        ModConn.s.ExecNonQuery("DELETE FROM tipos_eventos;")
        For Each quot As Map In quotes
            estado = quot.Get("estado")
            If estado <> 2 Then
                If estado == "null" Then
                    ModConn.s.ExecNonQuery2("INSERT INTO tipos_eventos VALUES(?,?)", _
                                            Array As String(quot.Get("id_tipoevento"),quot.Get("evento")))
                End If
            Else
                ToastMessageShow(quot.Get("mensaje"), False)
                Log(quot.Get("mensaje"))
            End If
        Next
        ModConn.s.ExecNonQuery2("UPDATE upd_tablas SET UPDATE_TIME = ? WHERE TABLE_NAME = 'tipos_eventos';", _
                                    Array As Object(FechaServer))
        Log("tipos de eventos actualizados.")
    End If
    j.Release
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…