iOS Question Second Download Conditional On Successful First

RichardN

Active Member
Licensed User
Step 1: Download a database containing a list of files on a server
Step 2: (Only when step 1 complete) Open that database.
Step 3: Download the files listed in that database

How? In the example below I am only get the .db and the last file in the list of files. What an I doing wrong?

B4X:
Sub UpdateFiles

   Dim Job1 As HTTPjob
   Dim URL Prefix As String = "http://www.anysite.com/files/"
   
   Job1.Initialize("FileList",Me)
   Job1.Download(URLprefix & "MyDatabase.db")
   
End Sub


Sub JobDone (Job As HttpJob)
   
   If Job.Success = True Then
     
     Select Job.JobName
       
       Case "FileList"                     'Handle the database then open it
         
         Dim out As OutputStream = File.OpenOutput(File.DirLibrary,"MyDatabase.db", False)
         File.Copy2(Job.GetInputStream, out)
         out.Close

         Main.SQL1.Initialize(File.DirLibrary,"MyDatabase.db",False)     'Re-initialize the database       
         Dim Files As ResultSet = Main.SQL1.ExecQuery("SELECT * FROM FileList ORDER BY File")       
         Dim fname as string

         Do While Files.NextRow                  'Initiate the remaining downloads
            fname = Files.GetString("File")
            Job1.Initialize(fname,Me)
            Job1.Download(URLprefix & fname)
         Loop


       Case Else                       'Save the files listed in the database when they arrive
               
         Dim out As OutputStream = File.OpenOutput(File.DirLibrary,Job.JobName, False)
         File.Copy2(Job.GetInputStream, out)
         out.Close 

     End Select
     
   Else
     Progress("Error: " & Job.ErrorMessage)
   End If
   
End Sub
 

RichardN

Active Member
Licensed User
Erel... I have modified the code as you suggested but it has made no difference. (I also added Job1 + Job2 for clarity). The database is downloaded OK but only the last file in the table is retrieved.

The Log() in the "CASE FileList" statement reports the list as expected.
The Log() in "CASE Else" part only ever reports (n times) the last file name in the table.

B4X:
Private Sub UpdateFiles

    Main.SQL1.Close  
  
    Dim Job1 As HttpJob
    Job1.Initialize("FileList",Me)
    Job1.Download(URLprefix & "MyDatabase.db")
    Log ("Downloading Database")
      
End Sub


Sub JobDone (Job As HttpJob)
  
    If Job.Success = True Then
      
        Select Job.JobName
          
            Case "FileList"                                        'Handle the database
              
                Dim out As OutputStream = File.OpenOutput(File.DirLibrary,"MyDatabase.db", False)
                File.Copy2(Job.GetInputStream, out)
                out.Close

                Log(Job.JobName & "Download")
              
                Main.SQL1.Initialize(File.DirLibrary,"MyDatabase.db",False)
                Dim Files As ResultSet = Main.SQL1.ExecQuery("SELECT * FROM FileList ORDER BY File")          
                Dim fname As String
                Dim Job2 As HttpJob 
  
                Do While Files.NextRow
                    fname = Files.GetString("File")
                    Log("Downloading.... " & fname)          'Reports the correct filename every pass
                    Job2.Initialize(fname,Me)
                    Job2.Download(URLprefix & fname)
                Loop  
              
            Case Else                                            'Handles the rest
                                          
                Log(Job.JobName & " .....Downloaded")      'Only ever reports that last file in the ResultSet is downloaded
              
                Dim out As OutputStream = File.OpenOutput(File.DirLibrary,Job.JobName, False)
                File.Copy2(Job.GetInputStream, out)
                out.Close      
          
        End Select
      
    Else
        Log("Error: " & Job.ErrorMessage)
    End If
  
End Sub
 
Upvote 0
Top