Alamofire(一)后台下载基本使用及原理解析

前言

这篇文章主要是分析后台下载,通过先写URLSession的后台下载,然后使用Alamofire这两种不同的情况,来详细解析在这个过程中遇到的坑和疑惑点,并且学习Alamofire的核心设计思想!

URLSession 后台下载

let configuration = URLSessionConfiguration.background(withIdentifier: self.createID())
        
let session = URLSession.init(configuration: configuration, delegate: self, delegateQueue: OperationQueue.main)
        
session.downloadTask(with: URL(string: self.urlDownloadStr2)!).resume()

这里用到了URLSessionConfiguration.background模式,是专门用来后台下载的,一共有三种模式,常用的是default

default:默认模式,系统会创建一个持久化的缓存并在用户的钥匙串中存储证书。
ephemeral:和default相反,系统不创建持久性存储,所有内容的生命周期与session相同。当session无效时,所有内容自动释放。
background:创建一个可以在后台甚至APP已经关闭的时候仍在传输数据的session

还设置了代理方法监听下载进度和下载完成:

  extension ViewController:URLSessionDownloadDelegate{
    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        // 下载完成 - 开始沙盒迁移
        print("下载完成 - \(location)")
        let locationPath = location.path
        //拷贝到用户目录(文件名以时间戳命名)
        let documnets = NSHomeDirectory() + "/Documents/" + self.lgCurrentDataTurnString() + ".mp4"
        print("移动地址:\(documnets)")
        //创建文件管理器
        let fileManager = FileManager.default
        try! fileManager.moveItem(atPath: locationPath, toPath: documnets)
    }
    
    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
        print(" bytesWritten \(bytesWritten)\n totalBytesWritten \(totalBytesWritten)\n totalBytesExpectedToWrite \(totalBytesExpectedToWrite)")
        print("下载进度: \(Double(totalBytesWritten)/Double(totalBytesExpectedToWrite))\n")
    }
}

这里是因为http的分段传输显得下载有很多段,内部是对这个代理方法不断调用,才能监听进度的回调。
在传输层中会由TCP对HTTP报文做了分段传输,达到目标地址后再对所有TCP段进行重组。

delegate没有接收到下载回调

测试发现,在切入到桌面后,Delegate没有接收到回调,这是因为还需要在AppDelegate实现handleEventsForBackgroundURLSession方法

    class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    //用于保存后台下载的completionHandler
    var backgroundSessionCompletionHandler: (() -> Void)?
    func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
        self.backgroundSessionCompletionHandler = completionHandler
    }
}
  • 直到所有Task全都完成后,系统会调用ApplicationDelegateapplication:handleEventsForBackgroundURLSession:completionHandler:回调,在处理事件之后,在 completionHandler 参数中执行 block,这样应用程序就可以获取用户界面的刷新。
  • 对于每一个后台下载的Task调用SessionDelegate中的URLSession:downloadTask:didFinishDownloadingToURL:(成功的话)和URLSession:task:didCompleteWithError:(成功或者失败都会调用)
    在上面的viewCotroller扩展里,多实现一个URLSessionDownloadDelegate的代理方法监听下载回来:注意要切换到主线程,因为要刷新界面
    func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
    print("后台任务下载回来")
    DispatchQueue.main.async {
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate, let backgroundHandle = appDelegate.backgroundSessionCompletionHandler else { return }
        backgroundHandle()
    }
}

如果不实现urlSessionDidFinishEvents这个代理方法会发生:

  1. 后台下载能力不影响,会正常下载完
  2. 耗费性能,completionHandler一直没调用,界面刷新会卡顿,影响用户体验

Alamofire后台下载

刚才搞定了URLSession的后台下载,但是使用起来是非常恶心的,非常麻烦,现在用Alamofire体验一下快速实现后台下载功能。

    DLBackgroundManger.shared.manager
    .download(self.urlDownloadStr) { (url, response) -> (destinationURL: URL, options: DownloadRequest.DownloadOptions) in
    let documentUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
    let fileUrl     = documentUrl?.appendingPathComponent(response.suggestedFilename!)
    return (fileUrl!,[.removePreviousFile,.createIntermediateDirectories])
    }
    .response { (downloadResponse) in
        print("下载回调信息: \(downloadResponse)")
    }
    .downloadProgress { (progress) in
        print("下载进度 : \(progress)")
}

使用链式直接完成请求和响应,同时还监听下载的回调,代码非常简洁而且可读性高。

封装了一个单例DLBackgroundManger用来管理后台下载,同时可以在里面配置很多基本参数,便于管理

    struct LGBackgroundManger {    
    static let shared = LGBackgroundManger()

    let manager: SessionManager = {
        let configuration = URLSessionConfiguration.background(withIdentifier: "com.test.alamofire")
        configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders
        configuration.timeoutIntervalForRequest = 10
        configuration.timeoutIntervalForResource = 10
        configuration.sharedContainerIdentifier = "com.test.alamofire"
        return SessionManager(configuration: configuration)
    }()
}

在这里使用单例管理的原因:

  1. 之前没有使用单例管理类,直接用SessionManager去调用,发现在切入后台的时候,控制台会报错如下,是因为被释放了,所以就报错了
    Error Domain=NSURLErrorDomain Code=-999 "cancelled"
  1. AppDelegate的回调里使用也非常方便
    func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
    DLBackgroundManger.shared.manager.backgroundCompletionHandler = completionHandler
}

SessionManager源码解析

习惯性的使用框架,一定要分析它的实现原理。
点进去SessionManager,看到它有一个default,类似URLSessiondefault,发现这里也确实设置成URLSessionConfiguration.default,然后还在SessionManager.defaultHTTPHeaders这里设置了一些初始化的header

public static let `default`: SessionManager = {
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders

        return SessionManager(configuration: configuration)
    }()

找到它的初始化init方法

     public init(
        configuration: URLSessionConfiguration = URLSessionConfiguration.default,
        delegate: SessionDelegate = SessionDelegate(),
        serverTrustPolicyManager: ServerTrustPolicyManager? = nil)
    {
        self.delegate = delegate
        self.session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)

        commonInit(serverTrustPolicyManager: serverTrustPolicyManager)
    }

这里都做了什么呢,其实很简单: 初始化了URLSession,默认用default模式,使用了SessionDelegate来接收URLSessiondelegate,其实就是一个代理的移交。

接着点进去commonInit,发现这里回调了当前的delegate.sessionDidFinishEventsForBackgroundURLSession,还做了[weak self]弱引用操作,同时还做了DispatchQueue.main.async切换主线程操作

    private func commonInit(serverTrustPolicyManager: ServerTrustPolicyManager?) {
        session.serverTrustPolicyManager = serverTrustPolicyManager

        delegate.sessionManager = self

        delegate.sessionDidFinishEventsForBackgroundURLSession = { [weak self] session in
            guard let strongSelf = self else { return }
            DispatchQueue.main.async { strongSelf.backgroundCompletionHandler?() }
        }
    }

SessionDelegate

点进去看看SessionDelegate,发现它实现了所有的代理:URLSessionDelegateURLSessionTaskDelegateURLSessionDataDelegate,URLSessionDownloadDelegate,URLSessionStreamDelegate

我们按照在上面探索到的sessionDidFinishEventsForBackgroundURLSession,发现这里是我们所需要找的方法,根据注释,这里是负责执行这个闭包的方法,然后在上面探索到的是这个闭包的具体实现

#if !os(macOS)
    /// Tells the delegate that all messages enqueued for a session have been delivered.
    ///
    /// - parameter session: The session that no longer has any outstanding requests.
    open func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
        sessionDidFinishEventsForBackgroundURLSession?(session)
    }
#endif
}    

总结

我们联系到刚刚在AppDelegate写的代码DLBackgroundManger.shared.manager.backgroundCompletionHandler = completionHandler,然后梳理一下整个流程:

  1. AppDelegatecompletionHandler传递给SessionManagerbackgroundCompletionHandler
  2. 下载完成的时候,SessionDelegate里的urlSessionDidFinishEvents执行,会调用到SessionManager里的sessionDidFinishEventsForBackgroundURLSession
  3. sessionDidFinishEventsForBackgroundURLSession的闭包里会执行当前的backgroundCompletionHandler
  4. backgroundCompletionHandlerAppDelegate传递过来的,所以就会调用它的completionHandler

这一套流程走出来是非常舒服的,简单易懂,达到了依赖下沉,网络层下沉的效果,我们在使用的时候不用关心它的实现,只要对它进行调用就好。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,475评论 4 372
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,744评论 2 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,101评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,732评论 0 221
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,141评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,049评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,188评论 2 320
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,965评论 0 213
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,716评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,867评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,341评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,663评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,376评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,200评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,990评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,179评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,979评论 2 279

推荐阅读更多精彩内容