仪器显示我的代码泄漏:
泄漏:CFHTTPCookieStorage
- > NSData *resultData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; //100.0%
我使用块执行异步下载:
NSURL *url = [NSURL URLWithString:user.user_photo];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection asyncRequestSimple:request success:^(NSData *data) {
user.user_photo_data=data;
NSLog(@"Image Downloaded");
dispatch_async(dispatch_get_main_queue(), ^{
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
});
} failure:^(NSError *error) {
NSLog(@"Failed downloading");
}];
我使用类别的异步实现:
NSURLConnection的+ MyExtensions.h
+ (void)asyncRequestSimple:(NSURLRequest *)request success:(void(^)(NSData* data))successBlock_ failure:(void(^)(NSError *error))failureBlock_;
NSURLConnection的+ MyExtensions.m
+ (void)asyncRequestSimple:(NSURLRequest *)request success:(void(^)(NSData* data))successBlock_ failure:(void(^)(NSError *error))failureBlock_
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURLResponse *response = nil;
NSError *error = nil;
NSData *resultData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];//100% leak
dispatch_sync(dispatch_get_main_queue(), ^{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
});
if (error) {
failureBlock_(error);
} else {
successBlock_(resultData);
}
});
}
为什么会泄漏?