mzeeshanid / MZDownloadManager

This download manager uses NSURLSession api to download files. It can download multiple files at a time. It can download large files if app is in background. It can resume downloads if app was quit.
BSD 3-Clause "New" or "Revised" License
1.12k stars 238 forks source link

Bunch of downloads tracking #29

Closed andrealufino closed 7 years ago

andrealufino commented 7 years ago

Hi guys, what if I need to download a bunch of files? I don't know how many files I have to download, it depends on the server, but I'd like to be able to track the download of every single file understanding when it finished. Is it possible? For example, adding a whole array of urls and not one by one maybe.. do you have some suggestion?

mzeeshanid commented 7 years ago

what if I need to download a bunch of files? Yes you can download multiple files simultaneously. You can use the instance method addDownloadTask to add a task. I don't know how many files I have to download, it depends on the server You can add as many as tasks you want but it is recommended to apply some limit on running tasks. I'd like to be able to track the download of every single file understanding when it finished MZDownloadManager provides different delegate methods to inform about any event, check the docs or source.

andrealufino commented 7 years ago

You can add as many as tasks you want but it is recommended to apply some limit on running tasks. Is not possible to limit the number of concurrent downloads in some way? Is there maybe a var? MZDownloadManager provides different delegate methods to inform about any event, check the docs or source. Yes, I saw them, but it's impossible to handle the downloads using the index var because often the index has the same value for multiple resources. How can I avoid this?

mzeeshanid commented 7 years ago

Yes you can limit concurrent downloads. For example see the code sample below:

let ongoingTasks = mzDownloadingViewObj?.downloadManager.downloadingArray.filter({ (fileModel: MZDownloadModel) -> Bool in
            return fileModel.status == TaskStatus.downloading.description()
        })
        if (ongoingTasks?.count)! <= 5 {
            mzDownloadingViewObj?.downloadManager.addDownloadTask(fileName as String, fileURL: fileURL.addingPercentEscapes(using: String.Encoding.utf8.rawValue)!, destinationPath: myDownloadPath)
        }

When some task is finished it will call the delegate and you can add new task there.

More over you can use the fileURL property. Every file on your server must have unique url.