举例说明NSURLSessionDataDelegate实现方法中,有关block块的说明

pragma mark - NSURLSessionDataDelegate

  • (void)URLSession:(NSURLSession *)session
    dataTask:(NSURLSessionDataTask *)dataTask
    didReceiveResponse:(NSURLResponse *)response
    completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler
    {
    NSURLSessionResponseDisposition disposition = NSURLSessionResponseAllow;

    if (self.dataTaskDidReceiveResponse) {
    disposition = self.dataTaskDidReceiveResponse(session, dataTask, response);
    }

    if (completionHandler) {
    completionHandler(disposition);
    }
    }
    ==============================================

[[AFAppDotNetAPIClient sharedClient] GET:@"v2/book/1220562" parameters:nil progress:nil success:^(NSURLSessionDataTask * __unused task, id JSON) {
NSArray *postsFromResponse = [JSON valueForKeyPath:@"data"];
NSMutableArray *mutablePosts = [NSMutableArray arrayWithCapacity:[postsFromResponse count]];
for (NSDictionary *attributes in postsFromResponse) {
Post *post = [[Post alloc] initWithAttributes:attributes];
[mutablePosts addObject:post];
}

    if (block) {
        block([NSArray arrayWithArray:mutablePosts], nil);
    }
} failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
    if (block) {
        block([NSArray array], error);
    }
}];
第一个completionHandler回调块,一半平常大家使用block回调,block里面的参数基本都是回调给我们需要处理的内容,但是这个completionHandler回调,里面没有参数,说明什么这个completionHandler回调块,需要我们向他传递参数,回调给系统使用

你可能感兴趣的:(举例说明NSURLSessionDataDelegate实现方法中,有关block块的说明)