Alamofire(2)— 后台下载

😊😊😊Alamofire专题目录,欢迎及时反馈交流 😊😊😊swift


Alamofire 目录直通车 --- 和谐学习,不急不躁!api


这一篇主要讲解后台下载,后台下载对于应用程序来讲,是一个很是重要也比较好用的功能。虽然用好后台下载的确可以大大提高用户体验,可是又不少时候咱们也会遇到不少坑点以及疑惑点。其中会经过 URLSessionAlamofire 两种形式分别展开讨论,对比学习才能更能体会 Alamofire 的设计思惟。Alamofire持续更新中,但愿你们但愿!安全

1、URLSession处理后台下载

URLSession在后台处理方面仍是比较简单的。网络

// 1:初始化一个background的模式的configuration
let configuration = URLSessionConfiguration.background(withIdentifier: self.createID())
// 2:经过configuration初始化网络下载会话
let session = URLSession.init(configuration: configuration, delegate: self, delegateQueue: OperationQueue.main)
// 3:session建立downloadTask任务-resume启动
session.downloadTask(with: url).resume()
复制代码
  • 初始化一个 background 的模式的 configurationconfiguration 三种模式 ,只有background 的模式才能进行后台下载。
  • 经过configuration初始化网络下载会话 session,设置相关代理,回调数据信号响应。
  • session建立downloadTask任务-resume启动 (默认状态:suspend)
  • 接下来依赖苹果封装的网络处理,发起链接 - 发送相关请求 - 回调代理响应
//MARK: - 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")
    }
}
复制代码
  • 实现了 URLSessionDownloadDelegatedidFinishDownloadingTo代理,实现下载完成转移临时文件里的数据到相应沙盒保存
  • 经过urlSession(_ session: downloadTask:didWriteData bytesWritten: totalBytesWritten: totalBytesExpectedToWrite: ) 的代理监听下载进度
  • 这里也是由于 http的分片传输 才致使的进度有段的感受,其实证实内部也是对这个代理方法不断调用,才能进度回调!

这里实现了下载功能,可是对于咱们须要的后台下载还差一段session

Applications using an NSURLSession with a background configuration may be launched or resumed in the background in order to handle the completion of tasks in that session, or to handle authentication. This method will be called with the identifier of the session needing attention. Once a session has been created from a configuration object with that identifier, the session's delegate will begin receiving callbacks. If such a session has already been created (if the app is being resumed, for instance), then the delegate will start receiving callbacks without any action by the application. You should call the completionHandler as soon as you're finished handling the callbacks.闭包

苹果爸爸老是能在合适时间给你优秀的建议,阅读文档的能力决定你是否可以在这个时代站稳本身的脚尖app

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    //用于保存后台下载的completionHandler
    var backgroundSessionCompletionHandler: (() -> Void)?
    func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
        self.backgroundSessionCompletionHandler = completionHandler
    }
}
复制代码
  • 实现handleEventsForBackgroundURLSession就能够完美后台下载
  • 告诉代理与 URLSession 相关的事件正在等待处理。
  • 应用程序在全部与 URLSession对象 关联的后台传输完成后调用此方法,不管传输成功完成仍是致使错误。若是一个或多个传输须要认证,应用程序也会调用这个方法。
  • 使用此方法能够从新链接任何 URLSession 并更新应用程序的用户界面。例如,您可使用此方法更新进度指示器或将新内容合并到视图中。在处理事件以后,在 completionHandler 参数中执行 block,这样应用程序就能够获取用户界面的刷新。
  • 咱们经过handleEventsForBackgroundURLSession保存相应的回调,这也是很是必要的!告诉系统后台下载回来及时刷新屏幕

urlSessionDidFinishEvents的代理实现调用框架

func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
    print("后台任务下载回来")
    DispatchQueue.main.async {
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate, let backgroundHandle = appDelegate.backgroundSessionCompletionHandler else { return }
        backgroundHandle()
    }
}
复制代码
  • 拿到UIApplication.shared.delegate的回调函数执行
  • 注意线程切换主线程,毕竟刷新界面

那么若是不实现这个代理里面的回调函数的执行,那么会发生什么呢async

  • 后台下载的能力是不会影响的
  • 可是会爆出很是验证界面刷新卡顿,影响用户体验
  • 同时打印台会爆出警告
Warning: Application delegate received call to -
application:handleEventsForBackgroundURLSession:completionHandler: 
but the completion handler was never called.
复制代码

2、Alamofire后台下载

Alamofire框架仍是比较有感受的,这个节奏也是函数式回调,还支持链式请求和响应!事务逻辑很是清晰,还有代码可读性也是很是简洁ide

LGBackgroundManger.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)")
}
复制代码
  • 这里封装了一个单例LGBackgroundManger的后台下载管理类,调用manger的手法也是很是直接。
  • 封装的思想不再须要去处理恶心的代理事件
struct LGBackgroundManger {    
    static let shared = LGBackgroundManger()

    let manager: SessionManager = {
        let configuration = URLSessionConfiguration.background(withIdentifier: "com.lgcooci.AlamofireTest.demo")
        configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders
        configuration.timeoutIntervalForRequest = 10
        configuration.timeoutIntervalForResource = 10
        configuration.sharedContainerIdentifier = "group.com.lgcooci.AlamofireTest"
        return SessionManager(configuration: configuration)
    }()
}
复制代码

可能不少同窗都在质疑为何要作成单利,URLSession的时候不是挺好的?

  • 若是你是 SessionManager.defalut 显然是不能够的!毕竟要求后台下载,那么咱们的会话 session 的配置 URLSessionConfiguration 是要求 background模式的
  • 若是你配置出来不作成单利,或者不被持有!在进入后台就会释放,网络也就会报错:Error Domain=NSURLErrorDomain Code=-999 "cancelled"
  • 应用层与网络层也能够达到分离。
  • 可以帮助在AppDelegate 的回调方便直接接收
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
    LGBackgroundManger.shared.manager.backgroundCompletionHandler = completionHandler
}
复制代码

3、SessionManger流程分析

一篇优秀的博客,毕竟还要跟你们交代这样清晰的代码的背后流程

一、SessionManger初始化
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)
}
复制代码
  • 初始化了session,其中configurationdefault的模式,设置了一些基本的 SessionManager.defaultHTTPHeaders 请求头信息
  • 代理移交,经过建立 SessionDelegate 这个专门处理代理的类来实现 URLSession的代理
二、代理完成回调

SessionDelegate 是一个很是重要的类,集合全部的代理

  • URLSessionDelegate
  • URLSessionTaskDelegate
  • URLSessionDataDelegate
  • URLSessionDownloadDelegate
  • URLSessionStreamDelegate

这里咱们根据需求来到 urlSessionDidFinishEvents 的代理

open func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
    sessionDidFinishEventsForBackgroundURLSession?(session)
}
复制代码
  • 这里执行了 sessionDidFinishEventsForBackgroundURLSession 闭包的执行,那么这个闭包在何时申明的呢?
  • 若是你足够聪明,这里你应该是可以想到的,SessionDelegate只是处理代理的专门类,但不是逻辑数据的处理类,按照封装设计的常规思路必将交给管理者类来下发

在咱们的 SessionManger 里面的初始化的时候,有一个方法commonInit

delegate.sessionDidFinishEventsForBackgroundURLSession = { [weak self] session in
    guard let strongSelf = self else { return }
    DispatchQueue.main.async { strongSelf.backgroundCompletionHandler?() }
}
复制代码
  • 这里就是代理的delegate.sessionDidFinishEventsForBackgroundURLSession闭包的声明
  • 只要后台下载完成就会来到这个闭包内部
  • 回调了主线程,调用了 backgroundCompletionHandler , 这也是 SessionManger 对外提供的功能!聪明的你应该知道知道了我在application的操做的本质了!
三、流程总结
  • 首先在 AppDelegatehandleEventsForBackgroundURLSession方法里,把回调闭包传给了 SessionManagerbackgroundCompletionHandler
  • 在下载完成回来的时候 SessionDelegateurlSessionDidFinishEvents代理的调用 -> sessionDidFinishEventsForBackgroundURLSession 调用
  • 而后sessionDidFinishEventsForBackgroundURLSession 执行 -> SessionManagerbackgroundCompletionHandler的执行
  • 最后致使 AppDelegatecompletionHandler 的调用

不管你是使用 URLSession 的方式,仍是 Alamofire 进行后台下载,可是原理仍是同样的,只是 Alamofire 使用更加达到依赖下沉,网络层下沉,使用更简洁,这也是不少时候咱们须要第三方框架的缘由。这一篇你估计已经感觉到了 Alamofire 的舒服,那么若是你喜欢的话,麻烦点心,关注一下。我会持续更新一个 Alamofire 的系列专题,谢谢!

就问此时此刻还有谁?45度仰望天空,该死!我这无处安放的魅力!

相关文章
相关标签/搜索