Downloading large files with nsurlsesssiontask






















Initialized code emulation system, which passes in the block of download progress and the block at completion, to be invoked when appropriate. Your email address will not be published. This site uses Akismet to reduce spam. Learn how your comment data is processed. Leave a Comment Cancel Reply Your email address will not be published.

So we need some way for the session to figure out which operation to complete when didCompleteWithError is called. So, to handle that, I maintain a dictionary, keyed by the task's taskIdentifier , which identifies the appropriate operation. That way, when the download finishes, you can "complete" the correct asynchronous operation. See revision history for Swift 2 implementation. Then you can do something like:. Now, unfortunately, Apple supplies a DataTaskPublisher which loads the full asset into memory which is not acceptable solution for large assets , but one can refer to their source code and adapt it to create a DownloadTaskPublisher :.

But one could conceivably adapt it for delegate pattern. Also, this will stop downloads when one fails. If you don't want it to stop just because one fails, you could conceivably define it to Never fail, and instead replaceError with nil :.

Here is rather minimalistic and pure-swift approach. Rob's answer has shown a right way to do this. I achieved it the delegate-based way to track the download with a progress view. You can check out the source code here. Multiple download with progress bar Github. More than one code in the background situation. I can learn by used global variable and NSTimer. You can try too. How are we doing? Please help us improve Stack Overflow.

Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 6 years, 2 months ago. Active 11 months ago. Viewed 27k times. Honey CraigH CraigH 1, 3 3 gold badges 27 27 silver badges 45 45 bronze badges.

Add a comment. Active Oldest Votes. Operation To address this, you can wrap the requests in asynchronous Operation subclass. The default implementation of this function throws an exception. Then you can do something like: var downloadRequests: AnyCancellable? Sequence sequence: urls. Output, Downstream. Failure private let lock: NSLocking private var parent: Parent? Rob Rob k 67 67 gold badges silver badges bronze badges. BTW, this answer shows how to wrap Alamofire request in asynchronous operation.

In those cases, you have to go old school, initiating the next request in the completion handler of the other request. CraigH - I've expanded my answer, showing you how you would do this. I hesitated to include all of this in my original answer, because it's a bit much, but given you had problems implementing this which I'm entirely sympathetic to What a mind-blowing answer. It gives you the entire logic behind how you can download multiple files simultaneously. It does not mean that you could not do this in another way, but the logic will be the same.



0コメント

  • 1000 / 1000