在启用ARC的代码中,如何在使用基于块的API时修复有关潜在保留周期的警告?
警告:
Capturing 'request' strongly in this block is likely to lead to a retain cycle
由这段代码生成:
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:...
[request setCompletionBlock:^{
NSDictionary *jsonDictionary = [[CJSONDeserializer deserializer] deserialize:request.rawResponseData error:nil];
// ...
}];
Run Code Online (Sandbox Code Playgroud)
警告与request块内对象的使用相关联.
cocoa cocoa-touch retain asihttprequest automatic-ref-counting
我有一个iOS项目,我在自己的类中使用ARC,但在其他库中关闭了ARC ASIHTTPRequest.
我使用下面的代码从Web服务器获取图像时出现大量内存泄漏:
-(void)buildPhotoView {
self.photoLibView.hidden = NO;
NSString *assetPathStr = [self.cellData objectForKey:@"AssetThumbPath"];
// get the thumbnail image of the ocPHOTOALBUM from the server and populate the UIImageViews
NSURL *imageURL = [NSURL URLWithString:assetPathStr];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:imageURL];
__unsafe_unretained ASIHTTPRequest *weakRequest = request;
[weakRequest setCompletionBlock:^{
// put image into imageView when request complete
NSData *responseData = [weakRequest responseData];
UIImage *photoAlbumImage = [[UIImage alloc] initWithData:responseData];
self.photo1ImageView.image = photoAlbumImage;
}];
[weakRequest setFailedBlock:^{
NSError *error = [request error];
NSLog(@"error geting file: …Run Code Online (Sandbox Code Playgroud)