NSURLSession 委托与 completionHandler [英] NSURLSession delegate vs. completionHandler

查看:39
本文介绍了NSURLSession 委托与 completionHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用完成处理程序.使用 NSURLConnection,现在使用 NSURLSession.这导致我的代码非常不整洁,尤其是我在请求中的请求中请求.

I've always used completion handlers. With NSURLConnection and now with NSURLSession. It's led to my code being really untidy, especially I have request within request within request.

我想尝试在 NSURLSession 中使用委托来实现我用 NSURLConnection 做的一些杂乱无章的事情.

I wanted to try using delegates in NSURLSession to implement something I've done untidily with NSURLConnection.

于是我创建了一个NSURLSession,并创建了一个dataTask:

So I created a NSURLSession, and created a dataTask:

NSURLSessionDataTask *dataTask = [overallSession dataTaskWithURL:url
                                                  completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                      if(error == nil)
                                                      {
                                                          NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
                                                          NSLog(@"Data = %@",text);
                                                      }

                                                  }];

    [dataTask resume];

现在我有一个 completionHandler 用于响应,我将如何切换到委托来管理响应和数据?我可以从这个委托中添加另一个 dataTask 吗?使用此 dataTask 创建并放入会话中的 cookie?

Right now I have a completionHandler for the response, how would I switch to delegates to manage the response and data? And can I add another dataTask from the delegate of this one? Using the cookies that this dataTask created and placed into the session?

推荐答案

如果要添加自定义委托类,需要实现NSURLSessionDataDelegateNSURLSessionTaskDelegate协议至少.

If you want to add a custom delegate class, you need to implement the NSURLSessionDataDelegate and NSURLSessionTaskDelegate protocols at the minimum.

使用方法:

 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response
 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {

    receivedData=nil; receivedData=[[NSMutableData alloc] init];
    [receivedData setLength:0];

    completionHandler(NSURLSessionResponseAllow);
}

NSURLSessionDataDelegate

-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
   didReceiveData:(NSData *)data {

    [receivedData appendData:data];
}

NSURLSessionTaskDelegate

 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error {
 if (error) {
  // Handle error
 }
else {
   NSDictionary* response=(NSDictionary*)[NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&tempError];
    // perform operations for the  NSDictionary response
}

如果您想将委托代码(中间层)与您的调用类分开(通常最好的做法是为网络调用提供单独的类/层),NSURLSession 的委托必须是:-

If you want to separate the delegate code (middle layer) from your calling class (generally its good practice to have separate class/layer for network calls), the delegate of NSURLSession has to be :-

NSURLSession *session=[NSURLSession sessionWithConfiguration:sessionConfig delegate:myCustomDelegateClass delegateQueue:nil];

参考链接:

  1. NSURLSession 类参考
  2. iOS NSURLSession 示例(HTTP GET、POST、后台下载)
  3. 从 NSURLConnection 到 NSURLSession

这篇关于NSURLSession 委托与 completionHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆