如何在后台下载后将属性传递给Swift 3 urlsession didFinishDownloadingTo委托? [英] how to pass properties to swift 3 urlsession didFinishDownloadingTo delegate after background download?

查看:120
本文介绍了如何在后台下载后将属性传递给Swift 3 urlsession didFinishDownloadingTo委托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用ios 10和swift 3,通过我的应用代码在后台成功下载一个zip文件.

I can successfully download a zip file in background with my app code with ios 10 and swift 3.

我使用了后台会话数据任务:

I use a backgroundsession data task:

let backgroundSessionConfiguration = URLSessionConfiguration.background(withIdentifier: (prefix + postfix))
        let backgroundSession = Foundation.URLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: OperationQueue.main)

        var request = URLRequest(url: dlUrl )
        request.httpMethod = "GET"
        request.cachePolicy = NSMutableURLRequest.CachePolicy.reloadIgnoringCacheData

        let task = backgroundSession.downloadTask(with: request)

        task.resume()

但是我不知道在下载完成之后将某些特定的元数据传递给委托方法的最佳实践是什么:

But I wonder what is the best practice to pass some specific metadata to the delegate method after the download finished:

    func urlSession(_ session: URLSession,
    downloadTask: URLSessionDownloadTask,
    didFinishDownloadingTo location: URL){

我需要在此委托人内部获取有关该下载内容的一些信息,具体取决于根据用户先前做出的选择.我无法从服务器或网址获取此数据.

I need inside this delegate some information about that download which depends e.g. on a choice a user made earlier. I am not able to get this data from the server or the url.

所以我想拥有一个变量或字典downloadType来告诉我如何完成下载,在委托中具有类似的内容

So I would like to have a variable or dictionary downloadType telling me what to do with the finished download, to have something like this in the delegate

if downloadType == "normal" ... do this
else ... do that

我不确定在App暂停时是否可以安全地使用实现URLSessionDownloadDelegate协议的类的属性? 在应用暂停的情况下下载完成后,类实例本身是否将还原,或者ios会创建该类的新实例?

I am not sure if I can safely use properties of the class implementing the URLSessionDownloadDelegate protocol while the App was suspend? Will the class instance itself be restored when the download finishes while the app is suspended or will ios create a new instance of the class?

所以这将始终有效:

class ServerCom: NSObject, URLSessionDownloadDelegate {
   var updateTypeStr = ""
  [...]
  func somePublic(setUpdateType: String) {
     self.updateTypeStr = setUpdateType

  [...]
    func urlSession(_ session: URLSession,
    downloadTask: URLSessionDownloadTask,
    didFinishDownloadingTo location: URL){
   [...] 

       if( self.updateTypeStr == "normal" ) { // do this
       else { // do that

该用例是否有更好的方法?

Are there some better ways for that use case?

提前谢谢!

推荐答案

在暂停应用程序的下载完成后,是否将还原类实例本身,或者ios会创建该类的新实例?

Will the class instance itself be restored when the download finishes while the app is suspended or will ios create a new instance of the class?

您有责任自己保存和还原此文件.

You are responsible for saving and restoring this yourself.

在守护程序完成后台请求期间,您的应用程序可能会在此期间完全终止(例如,已被暂停,可能由于内存压力而被抛弃),因此您必须能够将任何相关信息保存到持久性中存储,并能够在应用重新启动时从存储中加载.

Your app may be completely terminated (e.g. having been suspended, it can possibly be jettisoned due to memory pressure) in the intervening period while the daemon completes your background request, so you have to be able to save any relevant information to persistent storage and be able to load this from storage when the app restarts.

在使用后台会话测试应用程序时,我发现使用调用exit(0)的代码终止应用程序很有用.您永远不会在生产应用程序中执行此操作,但是在后台请求完成后重新启动应用程序时,测试您的应用程序重新创建必需对象的能力时,这很有用.关键点在于,在进行此测试时,请勿尝试使用双击主页按钮并向上滑动"来杀死该应用程序,因为这样做也将设计为取消后台请求.您想要测试在正常的应用程序生命周期中终止了您的应用程序,并在后台请求完成后由网络守护程序重新启动了该应用程序的情况.

When testing apps with background sessions, I find it useful to have code that calls exit(0) to terminate the app. You never do that in a production app, but it's useful when testing your app's ability to recreate the necessary objects when the app is re-started when the background request finishes. The key point is that one should not attempt to use the "double tap home button and swipe up" to kill the app when doing this testing, because that will, by design, cancel the background requests, too. You want to test the scenario where your app has been terminated through the normal app lifecycle and has been restarted by the network daemon once the background request is done.

这篇关于如何在后台下载后将属性传递给Swift 3 urlsession didFinishDownloadingTo委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆