简体   繁体   English

如何在后台下载后将属性传递给Swift 3 urlsession didFinishDownloadingTo委托?

[英]how to pass properties to swift 3 urlsession didFinishDownloadingTo delegate after background download?

I can successfully download a zip file in background with my app code with ios 10 and swift 3. 我可以在iOS 10和Swift 3中使用我的应用程序代码在后台成功下载一个zip文件。

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 eg on a choice a user made earlier. 我需要在此委托中提供有关该下载的一些信息,例如,这取决于用户之前做出的选择。 I am not able to get this data from the server or the url. 我无法从服务器或网址获取此数据。

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 所以我想有一个变量或字典downloadType告诉我如何完成下载,在委托中有这样的内容

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

I am not sure if I can safely use properties of the class implementing the URLSessionDownloadDelegate protocol while the App was suspend? 我不确定在应用暂停期间是否可以安全地使用实现URLSessionDownloadDelegate协议的类的属性? 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? 在应用程序暂停的情况下完成下载后,类实例本身是否将还原,或者ios会创建该类的新实例?

So will this always work: 因此,这将始终有效:

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? 该用例是否有更好的方法?

thanks in advance! 提前致谢!

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? 在应用程序暂停的情况下完成下载后,类实例本身是否将还原,或者ios会创建该类的新实例?

You are responsible for saving and restoring this yourself. 您有责任自己保存和还原此文件。

Your app may be completely terminated (eg 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. 在守护程序完成您的后台请求期间,您的应用程序可能会在此期间完全终止(例如,已暂停,可能由于内存压力而被抛弃),因此您必须能够将任何相关信息保存到持久性存储中并能够在应用重启时从存储中加载。

When testing apps with background sessions, I find it useful to have code that calls exit(0) to terminate the app. 在使用后台会话测试应用程序时,我发现使用调用exit(0)终止应用程序的代码很有用。 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. 您要测试在正常的应用程序生命周期中终止了您的应用程序并在后台请求完成后由网络守护程序重新启动了该应用程序的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM