NSURLSession delegate and completionHandler

前端 未结 2 1002
日久生厌
日久生厌 2021-01-05 18:14

I am using NSURLSession + NSURLDownloadTask with completionHandler:

[session downloadTaskWithURL:downloadURL completionHandler:^(NSURL *location, NSURLRespon         


        
相关标签:
2条回答
  • 2021-01-05 18:16

    From the documentation:

    NSURLSession returns data in one of two ways, depending on the methods you call:

    • To a completion handler block that returns data to your app when a transfer finishes successfully or with an error.
    • By calling methods on your custom delegate as the data is received.

    One of two ways. Either use the completion handler (if all you want is to be notified at the end) or use the delegate methods (if you want full information as you go). Not a problem. It's not like the delegate methods are hard to use or anything; it's probably just about always better to use them.

    0 讨论(0)
  • 2021-01-05 18:30

    To employ the didWriteData method of the NSURLSessionDownloadDelegate, you sadly have to use the rendition of downloadTaskWithURL without the completionHandler and then implement your own URLSession:downloadTask:didFinishDownloadingToURL: to perform those actions you otherwise would have done in the completion handler.

    This is a little annoying (especially since the NSURLSessionDownloadDelegate is set at the session object), but it's how it works.

    0 讨论(0)
提交回复
热议问题