Pause,Resume,Cancel Upload Task Using NSURLSession UploadTask

情到浓时终转凉″ 提交于 2019-12-05 04:06:59

I have studied alot but could find nothing .After i tried this on my code assuming this as a download task ,I came to know that we can "pause , resume" upload tasks as well using NSURLSession just we do in downloading tasks.

To pause a task simply call [yourUploadTask suspend]; to resume [yourUploadTask resume]; To cancel [yourUploadTask cancel];

It might help someone working on uploadTask using NSURLSession.

You can try to use the suspend(), resume() methods inherited from URLSessionTask, but resume() will cause the upload to fail. You can work around this in your upload class by automatically retrying upload errors (i.e. any non-permanent error other than 400s), but that means restarting every pending upload again from the beginning and losing the data that's already been uploaded.

There isn't a cancel(byProducingResumeData completionHandler: @escaping (Data?) -> Void) for URLSessionUploadTask like there is for URLSessionDownloadTask, so there is no state data saved to restore and continue the upload. To implement one would require rolling our own, which I haven't done (yet), but I believe requires specific server support to implement, and the user of lower level networking APIs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!