NSLSession

一、URL Session的基本概念

1.三种工作模式:

默认会话模式(default):工作模式类似于原来的NSURLConnection,使用的是基于磁盘缓存的持久化策略,使用用户keychain中保存的证书进行认证授权。

瞬时会话模式(ephemeral):该模式不使用磁盘保存任何数据。所有和会话相关的caches,证书,cookies等都被保存在RAM中,因此当程序使会话无效,这些缓存的数据就会被自动清空。

后台会话模式(background):该模式在后台完成上传和下载,在创建Configuration对象的时候需要提供一个NSString类型的ID用于标识完成工作的后台会话。

2.NSURLSession支持的三种任务

NSURLSession类支持三种类型的任务:加载数据,下载和上传。

二、相关的类

NSURLConnection这个名字,实际上指的是一组构成Foundation框架中URL加载系统的相互关联的组件:NSURLRequest,NSURLResponse,NSURLProtocol,NSURLCache,NSHTTPCookieStorage,NSURLCredentialStorage,以及和它同名的NSURLConnection。

在WWDC 2013中,Apple的团队对NSURLConnection进行了重构,并推出了NSURLSession作为替代。

NSURLSession也是一组相互依赖的类,它的大部分组件和NSURLConnection中的组件相同如NSURLRequest,NSURLCache等。而NSURLSession的不同之处在于,它将NSURLConnection替换为NSURLSession和NSURLSessionConfiguration,以及3个NSURLSessionTask的子类:NSURLSessionDataTask, NSURLSessionUploadTask, 和NSURLSessionDownloadTask。

下面来说下NSURLSession新推出的类:

1.NSURLSessionConfiguration类

其中NSURLSessionConfiguration用于配置会话的属性,可以通过该类配置会话的工作模式:

+ (NSURLSessionConfiguration *)defaultSessionConfiguration;

+ (NSURLSessionConfiguration *)ephemeralSessionConfiguration;

+ (NSURLSessionConfiguration *)backgroundSessionConfiguration:(NSString *)identifier;

在backgroundSessionConfiguration:方法中的identifier参数指定了会话的ID,用于标记后台的session。

该类的其中两个属性:

/* allow request to route over cellular. */

@propertyBOOL allowsCellularAccess;

/* allows background tasks to be scheduled at the discretion of the system for optimal performance. */

@property (getter=isDiscretionary)BOOL discretionary NS_AVAILABLE(NA,7_0);

allowsCellularAccess 属性指定是否允许使用蜂窝连接, discretionary属性为YES时表示当程序在后台运作时由系统自己选择最佳的网络连接配置,该属性可以节省通过蜂窝连接的带宽。在使用后台传输数据的时候,建议使用discretionary属性,而不是allowsCellularAccess属性,因为它会把WiFi和电源可用性考虑在内。补充:这个标志允许系统为分配任务进行性能优化。这意味着只有当设备有足够电量时,设备才通过Wifi进行数据传输。如果电量低,或者只仅有一个蜂窝连接,传输任务是不会运行的。后台传输总是在discretionary模式下运行。


2.NSURLSession类

获取NSURLSession类对象有几种方式:

/*

* The shared session uses the currently set global NSURLCache,

* NSHTTPCookieStorage and NSURLCredentialStorage objects.

*/

+ (NSURLSession *)sharedSession;

/*

* Customization of NSURLSession occurs during creation of a new session.

* If you only need to use the convenience routines with custom

* configuration options it is not necessary to specify a delegate.

* If you do specify a delegate, the delegate will be retained until after

* the delegate has been sent the URLSession:didBecomeInvalidWithError: message.

*/

+ (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configuration;

+ (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configurationdelegate:(id )delegatedelegateQueue:(NSOperationQueue *)queue;

第一种方式是使用静态的sharedSession方法,该类使用共享的会话,该会话使用全局的Cache,Cookie和证书。

第二种方式是通过sessionWithConfiguration:方法创建对象,也就是创建对应配置的会话,与NSURLSessionConfiguration合作使用。

第三种方式是通过sessionWithConfiguration:delegate:delegateQueue方法创建对象,二三两种方式可以创建一个新会话并定制其会话类型。该方式中指定了session的委托和委托所处的队列。当不再需要连接时,可以调用Session的invalidateAndCancel直接关闭,或者调用finishTasksAndInvalidate等待当前Task结束后关闭。这时Delegate会收到URLSession:didBecomeInvalidWithError:这个事件。Delegate收到这个事件之后会被解引用。

3.NSURLSessionTask类

NSURLSessionTask是一个抽象子类,它有三个子类:NSURLSessionDataTask,NSURLSessionUploadTask和NSURLSessionDownloadTask。这三个类封装了现代应用程序的三个基本网络任务:获取数据,比如JSON或XML,以及上传和下载文件。

下面是其继承关系:


有多种方法创建对应的任务对象:

(1)NSURLSessionDataTask

通过request对象或url创建:

/* Creates a data task with the given request.  The request may have a body stream. */

- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request;

/* Creates a data task to retrieve the contents of the given URL. */

- (NSURLSessionDataTask *)dataTaskWithURL:(NSURL *)url;

通过request对象或url创建,同时指定任务完成后通过completionHandler指定回调的代码块:

/*

* data task convenience methods.  These methods create tasks that

* bypass the normal delegate calls for response and data delivery,

* and provide a simple cancelable asynchronous interface to receiving

* data.  Errors will be returned in the NSURLErrorDomain,

* see .  The delegate, if any, will still be

* called for authentication challenges.

*/

- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)requestcompletionHandler:(void (^)(NSData *data,NSURLResponse *response,NSError *error))completionHandler;

- (NSURLSessionDataTask *)dataTaskWithURL:(NSURL *)urlcompletionHandler:(void (^)(NSData *data,NSURLResponse *response,NSError *error))completionHandler;

(2)NSURLSessionUploadTask

通过request创建,在上传时指定文件源或数据源。

/* Creates an upload task with the given request.  The body of the request will be created from the file referenced by fileURL */

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)requestfromFile:(NSURL *)fileURL;

/* Creates an upload task with the given request.  The body of the request is provided from the bodyData. */

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)requestfromData:(NSData *)bodyData;

/* Creates an upload task with the given request.  The previously set body stream of the request (if any) is ignored and the URLSession:task:needNewBodyStream: delegate will be called when the body payload is required. */

- (NSURLSessionUploadTask *)uploadTaskWithStreamedRequest:(NSURLRequest *)request;

在创建upload task对象时,通过completionHandler指定任务完成后的回调代码块:

/*

* upload convenience method.

*/

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)requestfromFile:(NSURL *)fileURLcompletionHandler:(void (^)(NSData *data,NSURLResponse *response,NSError *error))completionHandler;

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)requestfromData:(NSData *)bodyDatacompletionHandler:(void (^)(NSData *data,NSURLResponse *response,NSError *error))completionHandler;

(3)NSURLSessionDownloadTask

/* Creates a download task with the given request. */

- (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request;

/* Creates a download task to download the contents of the given URL. */

- (NSURLSessionDownloadTask *)downloadTaskWithURL:(NSURL *)url;

/* Creates a download task with the resume data.  If the download cannot be successfully resumed, URLSession:task:didCompleteWithError: will be called. */

- (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData *)resumeData;

下载任务支持断点续传,第三种方式是通过之前已经下载的数据来创建下载任务。

同样地可以通过completionHandler指定任务完成后的回调代码块:

/*

* download task convenience methods.  When a download successfully

* completes, the NSURL will point to a file that must be read or

* copied during the invocation of the completion routine.  The file

* will be removed automatically.

*/

- (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)requestcompletionHandler:(void (^)(NSURL *location,NSURLResponse *response,NSError *error))completionHandler;

- (NSURLSessionDownloadTask *)downloadTaskWithURL:(NSURL *)urlcompletionHandler:(void (^)(NSURL *location,NSURLResponse *response,NSError *error))completionHandler;

- (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData *)resumeDatacompletionHandler:(void (^)(NSURL *location,NSURLResponse *response,NSError *error))completionHandler;

4.NSURLSessionDelegate和NSURLSessionTaskDelegate协议

在协议的方法中可以完成各种各样的回调动作,如身份验证、完成任务后的动作、错误处理和后台任务完成的动作等。委托方法指定在NSURLSession中一定数量的字节传输使用int64_t类型的参数。

这里只说下后台任务的一个委托方法:

/* If an application has received an

* -application:handleEventsForBackgroundURLSession:completionHandler:

* message, the session delegate will receive this message to indicate

* that all messages previously enqueued for this session have been

* delivered.  At this time it is safe to invoke the previously stored

* completion handler, or to begin any internal updates that will

* result in invoking the completion handler.

*/

- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session NS_AVAILABLE_IOS(7_0);

合作使用的ApplicationDelegate方法:

// 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.

- (void)application:(UIApplication *)applicationhandleEventsForBackgroundURLSession:(NSString *)identifiercompletionHandler:(void (^)())completionHandler NS_AVAILABLE_IOS(7_0);

将任务切换到后台之后,Session的Delegate不会再收到和Task相关的消息。当所有Task全都完成后,程序将被唤醒,并调用ApplicationDelegate的application:handleEventsForBackgroundURLSession:completionHandler:回调,在这里要为后台session(由background session的identifier标识)指定对应的回调代码块。

随后,对于每一个完成的后台Task调用该Session的Delegate中的URLSession:downloadTask:didFinishDownloadingToURL:(成功的话)和URLSession:task:didCompleteWithError:(成功或者失败都会调用)方法做处理,以上的回调代码块可以在这里调用。

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

推荐阅读更多精彩内容