NSURLSessionDelegate未被调用

时间:2014-05-23 05:06:16

标签: ios iphone nsurlsession

尝试实现NSURLSession委托,但它们没有被调用。 正在返回数据,但代表们没有被调用。

部首:

@interface APIClient : NSObject <NSURLSessionDelegate>

方法:

-(void)getAllPeopleWithCompletionHandler:(void (^)(NSArray *))completionHandler 
{
   self.apiURL =@"https://gist.githubusercontent.com/anonymous/fb5ae838b8d752c393db/raw/gistfile1.json";
   NSURLSession *session = [NSURLSession sharedSession];
   NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
   session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

   NSURLSessionDataTask *dataTask =
   [session dataTaskWithURL:[NSURL URLWithString:self.apiURL]
       completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
           NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
           NSMutableArray *people = [[NSMutableArray alloc] init];
           for (NSDictionary *personAttributes in json) {
               [people addObject:[Person personWithAttributes:personAttributes]];
           }

       completionHandler(people);

   }];
   [dataTask resume];

}

-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask     didReceiveData:(NSData *)data 
{
   NSLog(@"Hello");
}

1 个答案:

答案 0 :(得分:0)

在调度异步会话任务后,检查接收方线程是否存在。