您的位置:首页 > 理论基础 > 计算机网络

ASIHTTPRequest with block

2013-06-03 22:03 148 查看
As of v1.8, we can do the same thing using blocks, on platforms that support them:

- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
// Use when fetching text data
NSString *responseString = [request responseString];

// Use when fetching binary data
NSData *responseData = [request responseData];
}];
[request setFailedBlock:^{
NSError *error = [request error];
}];
[request startAsynchronous];
}


Note the use of the __block qualifier when we declare the request, this is important! It tells the block not to retain the request, which is important in preventing a retain-cycle, since the request
will always retain the block.

参考资料

1 http://allseeing-i.com/ASIHTTPRequest/How-to-use - Using
blocks

2 Blocks
Programming Topics - Object and Block Variables
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: