Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 3 Support (Qt 5) › Download files one by one

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #24488

    Martin

    Hi,

    I need some help. I want to download several files. The process should be: start download of file B when download of file A is finished.

    What is the best way to do that? Currently I create a new component on the finished signal of the previous download. But with big queues the app crashes, caused of memory consumption.

    Kind regards,
    Martin

    #24489

    Alex
    Felgo Team

    Hi Martin,

    what component are you using to download the file(s)? Are you also explicetly destroying the previous component before creating a new one, to potentially free up memory?

    Best,
    Alex

    #24490

    Martin

    Hi Alex,

     

    I have to correct myself. I only have one component left that I am reusing. With that I already wanted to bypass the crashes.

    Here is the relevant code:

    DownloadableResource {
            id: downloader
            headerParameters: ({
                                   "Authorization": "Basic " + Qt.btoa(AppSettings.remoteUser + ":" + AppSettings.remotePassword),
                               })
    
            // Shorthand property if status is busy
            property bool busy: status === DownloadableResource.Downloading || status === DownloadableResource.Extracting
    
            // Array of Files (Media IDs) to be downloaded
            property var fileQueue: []
    
            // Storage Location on local disk
            storageLocation: FileUtils.AppDataLocation
            extractAsPackage: false
    
            // If download status changes show debug message
            onStatusChanged: {
    
                console.debug("Aktueller Status: " + downloader.statusText)
    
                if(status === DownloadableResource.Available) {
                    console.log("Download beendet für Datei: " + storagePath + storageName)
                }
            }
    
            // Message if download starts
            onDownloadStarted: {
                console.log("Download gestartet. Quelle: " + source + ", StorageName: " + storageName)
            }
    
            // Download of file finished
            onDownloadFinished: {
    
                // Still files in queue
                while(downloader.fileQueue.length > 0) {
                    
                    // Take next file to download
                    var media = downloader.fileQueue.pop()
    
                    progressBar.value = progressBar.value + progressBar.interval
    
                    // Download source
                    downloader.source = AppSettings.remoteUrl + "/media/attachment/index/MEDIA_ID/" + media.MEDIA_ID
                    downloader.storageName = media.MEDIA_FILE_MD5
                    
                    // Valid filename
                    if(media !== undefined && media.OBJECT_SUB_ID !== undefined) {
    
                        var newFileName = media.MEDIA_FILE_MD5
    
                        // Für Pläne sind gesonderte Dateien zu holen
                        if(media.OBJECT_SUB_ID == 2) {
                            console.log("Plan gefunden: " + media.MEDIA_ID)
                            newFileName = media.MEDIA_FILE_MD5.replace(".pdf", ".jpg")
                            downloader.source = AppSettings.remoteUrl + "/defmgr/object/getplanimage/MEDIA_ID/" + media.MEDIA_ID
                        }
    
                        downloader.storageName = newFileName
                        console.log("Hole Planbild für ID: "  + media.MEDIA_ID)
                    }
    
                    // Trigger download if file doesn't exist on local disc
                    if(downloader.status === DownloadableResource.UnAvailable) {
                        console.log("Datei existiert noch nicht: " + downloader.storagePath)
                        downloader.download()
                        break
                    }
                    // File already exists; skip
                    else {
    
                        console.log("Datei existiert bereits: " + downloader.storagePath)
                        //fileUtils.removeFile(Qt.resolvedUrl(downloader.storagePath))
                        continue
                    }
                }
    
                // Prevent double messages
                if(downloader.fileQueue.length == 0 && progressBar.visible) {
                    //progressBar.value = 1
                    NativeDialog.confirm("Erfolg", "Projektdaten wurden erfolgreich heruntergeladen.", false, false)
                    progressBar.visible = false
                }
            }
        }

    The fileQueue is filled with some IDs. The downloader iterates over the IDs and downloads the files. But as I said, on big queues the app crashes.

    Kind regards,
    Martin

    #24493

    Alex
    Felgo Team

    Hi Martin,

    I just tried a small example to download a queue using a single DownloadableResource component das is dynamically created and destroyed. Observing the memory consuption, everything looks fine. Maybe that could be something you can try as well. Potentially the issue also does not come from downloading, but somewhere else in your application, maybe how you use the downloaded files?

    Here is the example:

    import Felgo 3.0
    import QtQuick 2.0
    
    App {
      id: app
      // You get free licenseKeys from https://felgo.com/licenseKey
      // With a licenseKey you can:
      //  * Publish your games & apps for the app stores
      //  * Remove the Felgo Splash Screen or set a custom one (available with the Pro Licenses)
      //  * Add plugins to monetize, analyze & improve your apps (available with the Pro Licenses)
      //licenseKey: "<generate one from https://felgo.com/licenseKey>"
    
      Component.onCompleted: {
        console.debug("DownloadableResource.UnAvailable " + DownloadableResource.UnAvailable)
        console.debug("DownloadableResource.Available " + DownloadableResource.Available)
        console.debug("DownloadableResource.Downloading " + DownloadableResource.Downloading)
        console.debug("DownloadableResource.Extracting " + DownloadableResource.Extracting)
      }
    
      property var baseUrl: "https://felgo.com/resources/"
      property var queue: [
        "resources_balloonpopfelgo.zip",
        "resources_crazycarouselfelgo.zip",
        "resources_doodlejump_felgo.zip",
        "resources_flappybirdfelgo.zip",
        "resources_flaskofrum_felgo.zip",
        "resources_juicysquash_felgo.zip",
        "resources_multiscenemultilevelfelgo.zip",
        "resources_rubesidescroller_felgo.zip",
        "resources_stacktheboxfelgo.zip"
      ]
      property int queueIndex: 0
      property var downloaderReference: undefined
    
      NavigationStack {
    
        Page {
          title: qsTr("Main Page")
    
          AppButton {
            anchors.centerIn: parent
            text: "Download"
            onClicked: {
              downloadAll()
            }
          }
        }
    
      }
    
      function downloadAll() {
        downloadNext()
      }
      function downloadNext() {
        console.debug("downloadNext " + queueIndex)
        if(downloaderReference) {
          console.debug("destroying reference")
          downloaderReference.destroy()
          downloaderReference = undefined
        }
        if(queueIndex >= queue.length) {
          console.debug("All done")
          return
        }
    
        var downloader = downloaderComponent.createObject(app,{source: baseUrl + queue[queueIndex]})
        downloaderReference = downloader
        //downloader.source = baseUrl + queue[queueIndex]
        console.debug("Calling download " + downloader.source)
        downloader.download()
        queueIndex++
      }
    
      Component {
        id: downloaderComponent
        DownloadableResource {
          storageLocation: FileUtils.AppDataLocation
          extractAsPackage: false
          onStatusChanged: {
            console.debug("DownloadableResource status " + status)
          }
          onDownloadFinished: {
            downloadNext()
          }
        }
      }
    }
    

    Best,
    Alex

    #24494

    Martin

    Hi Alex,

    thank you. I will try your code.

    Regards,
    Martin

    #24497

    Martin

    Hi Alex,

    I tried your solution. It works like a charm.

    Thank you
    Martin

Viewing 6 posts - 1 through 6 (of 6 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded