Skip to content

Instantly share code, notes, and snippets.

@ninjinkun
Last active December 16, 2015 19:49
Show Gist options
  • Save ninjinkun/5487390 to your computer and use it in GitHub Desktop.
Save ninjinkun/5487390 to your computer and use it in GitHub Desktop.
does NSURLConnection block method has circular reference problem? Result is no.
#import <Foundation/Foundation.h>
@interface BlockTest : NSObject
-(void)request;
@end
@implementation BlockTest {
NSOperationQueue *_queue;
NSString *_str;
}
- (id)init
{
self = [super init];
if (self) {
_queue = [[NSOperationQueue alloc] init];
_str = @"hoge";
}
return self;
}
-(void)request {
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.hatena.ne.jp"]];
[NSURLConnection sendAsynchronousRequest:req queue:_queue completionHandler:^(NSURLResponse *res, NSData *data, NSError *error) {
NSLog(@"%@", _str);
}];
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
BlockTest *test = [[BlockTest alloc] init];
[test request];
NSLog(@"retain %ld", CFGetRetainCount((__bridge CFTypeRef) test)); // => retain 2
[NSThread sleepForTimeInterval:1];
NSLog(@"retain %ld", CFGetRetainCount((__bridge CFTypeRef) test)); // => retain 1
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment