Last active
December 16, 2015 19:49
-
-
Save ninjinkun/5487390 to your computer and use it in GitHub Desktop.
does NSURLConnection block method has circular reference problem? Result is no.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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