Pause,Resume,Cancel Upload Task Using NSURLSession UploadTask

对着背影说爱祢 提交于 2019-12-06 23:37:33

问题


I am developing an app to upload multiple files using NSURLSession, right now my files are successfully uploaded. But now what i want to achieve is to pause,resume and cancel the uploads just like we do in download tasks. Is it possible.? Any help would be appreciated. Thnx


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/27813799/pause-resume-cancel-upload-task-using-nsurlsession-uploadtask

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