zoukankan      html  css  js  c++  java
  • NSURLSession

    NSURLSession这个类和与其有关联的其他类,提供一个通过HTTP下载的API。这个API提供丰富的代理方法可以让你的app在挂起或者没有运行的时候,在后台下载。

    用NSURLSession API,你的app可以创建一系列的session,每一个session协调一组关联的数据转存任务。举个例子:你在协议一个网络浏览器,你的app可以创建为每个tab或每个window创建一个session。在每个session中,你的app添加一系列的任务,每一个任务代表一个指定URL的请求。

    三种类型session:default session behave similary to other Foundation methods for downloading URLs,使用基于磁盘的cache,并且在用户的钥匙串中存储credentials,ephemeral session不在disk上做缓存,所有的缓存、credential等都保持在RAM里,和session绑定;background session 它的行为跟default session相似,但它用一个单独的进程处理数据传输。把结果存在一个文件中,并且在app挂起、退出、崩溃的时候仍然继续转存数据。

    在这些session中,你可以安排三种类型的任务:

      data task用NSData对象收发数据,Data task are intended for short, often interative request from you app to a server。可以一次返回一块数据,或者通过completion handler一次返回所有数据。Because data task do not store the data to file, they are not supported in background sessions。

      download task retrieve data in the form of file, and support background downloads while the app is not running。

      upload task send data(usuall in the form of a file),and support background uploads while the app is not running。

    像大多数的网络API一样,NSURLSession是高度异步的,它根据你调用的方法,用一种或两种方法返回数据:

    • 一个handler block,当转存完成或者出错的时候,给你的app返回数据。
    • 通过调用你自定义的代理,当数据被收到的时候。
    • 通过调用方法在自定义的代理上,当下载数据完成。

    NSURLSession的API除了传送信息给代理,也提供状态和过程属性。支持取消、重新开始、继续、挂起任务,并且有继续  挂起、取消或者失败的下载的能力。

    URL Sessioin Class Hierarchy

    NSURLSession API包括下面这些类:

    • NSURLSession:一个session对象
    • NSURLSessionConfiguration: 初始化session时用得配置对象。
    • NSURLSessionTask:session中任务的基类。
      • NSURLSessionDataTask:任务用于获取一个URL的内容(作为NSData)。
        • NSURLSessionUploadTask:上传文件,接着取回一个URL的内容(作为NSData)。
      • NSURLSessionDownloadTask:获取一个URL的内容作为一个临时文件放在磁盘上。

    除此之外,NSURLSession的API提供了四个协议,你的app可以实现它们的代理方法以对session和task的行为进行更颗粒化(精细)的控制。

    • NSURLSessionDelegate:处理session-level事件。
    • NSURLSessionTaskDelegate:处理task-level事件,对所有任务类型可用。
    • NSURLSessionDataDeleg:处理task-level事件,只对data task和upload task有用。
    • NSURLSessionDownloadDelegate:处理task-level事件,只对download task有用。

    最后,NSURLSession的API用许多类,这些类也经常和其他API一起用(比如NSURLConnection和NSURLDownload)。

    • NSURL:
    • NSURLRequest:封装和URL request相关的元数据(metadata),包括URL,request方法,等等。
    • NSURLResponse:封装和服务器对请求的响应有关的元数据,比如内容的MIME类型和长度。
      • NSHTTPURLResponse:为HTTP request添加额外的,比如响应头。
    • NSCachedURLResponse:封装一个NSURLRespo对象(与服务器响应体的真实数据一起),目的是cache。

    Background Transfer Considerations

    使用background session的时候,因为实际的传输由一个单独的进程来执行,也因为重启app的代价相对比较大,因此,一些特性不可用,造成下面的限制:

    • The session must provide a delegate for event delivery.(For uploads and downloads, the delegate behave the same as for in-process transfer.)
    • 只支持HTTP/HTTPS协议,不支持自定义协议。
    • 只支持upload task、download task,不支持data task。
    • Redirects are always follows.
    • If the background transfer is initiated while the app is in the background, the configuration object's discretionary property is treated as being true.

    在iOS中,当后台传输完成或者需要资格(require credentials),如果app没有在运行,iOS自动重启app并调用application:handleEventsForBackgroundURLSession:completionHandler:方法在UIApplicationDelegate对象上。This call provides the identifier for the session that caused your app to be lunched. 你的app应该存储这个completionHandler,用这个identifier创建background configuration object,然后用这个配置对象创建session。The new session is automatically reassociated with on going background activity. Later, when the session finishes the last background download task, it sends the session delegate a URLSessionDidFinishEventsForBackgroundURLSession: message. Your session delegate should then call the stored completion handler.

    In both iOS and OS X, when the user relaunches your app, your app should immediately create background configuration objects with the same identifiers as any session that had outstanding tasks when your app was last running, then create a session for each of those configuration objects. These new sessions are similary automatically reassociated with ongoing backgorund activity.

    当你的app被挂起的时候,如果任何任务完成,这个任务的代理的URLSession:downloadTask:didFinishDownloadingToURL:  方法被调用。

    Similarly, if any task requires credentials, the NSURLSession object calls the delegate’s URLSession:task:didReceiveChallenge:completionHandler: method or URLSession:didReceiveChallenge:completionHandler: method as appropriate. 

    创建和配置

    NSURLSession API提供的配置选项:

    • Private storage support for caches,cookies,credentials,and protocols in a way that is specific to a single session.
    • Authentication,tied to a specific request(task) or group of requests(session)。
    • File uploads and downloads by URL,which encourage separation of the data (the file's contents)from the metadata(the URL and settings)
    • Configuration of the maximum number of connections per host
    • Per-source timeouts that are triggered if an entire resource cann't be downloaded in a certain mount of time
    • Minimum and maximum TLS version support
    • Custom proxy dictionaries
    • Control over cookie policies
    • Control over pipelining bahavior

    NSURLSession API需要许多不同的类用相当复杂的方式一起工作,在用之前还是应该看看the URL Loading System来理解这些类之间是怎么交互的。

    原文: NSURLSession Class Reference

    文章版权归个人所有,转载时请在文章显眼位置给出本文链接。
  • 相关阅读:
    备忘
    跨域问题思考
    个人理解的Lambda表达式的演化过程
    MySql 事务与锁
    Python连接MySQL
    将python虚拟环境移植到无法联网的电脑
    PyQt5+QtDesigner+fbs+python创建桌面程序
    Pycharm+Anaconda安装及配置
    一个极好的JavaScript学习网址
    Python GUI工具Tkinter以及拖拉工具Page安装
  • 原文地址:https://www.cnblogs.com/xjshi/p/4379221.html
Copyright © 2011-2022 走看看