Skip to content

Instantly share code, notes, and snippets.

@michaelochs
Created November 4, 2016 18:36
Show Gist options
  • Save michaelochs/a25167d13603a791123c8f9098790c25 to your computer and use it in GitHub Desktop.
Save michaelochs/a25167d13603a791123c8f9098790c25 to your computer and use it in GitHub Desktop.
Test ARC whether it retains self on method execution or not
@interface MyObject : NSObject
@end
@implementation MyObject
- (void)doSomething {
for (int i = 0; i < 1000; i++) {
[self print:i];
usleep(100);
}
}
- (void)print:(int)i {
NSLog(@">>> %d", i);
}
- (void)dealloc {
NSLog(@"DEALLOC");
}
@end
@interface ViewController ()
@property (nonatomic) MyObject *sut;
@end
@implementation ViewController
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
_sut = [MyObject new];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// start executing long running task in the background
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
[self.sut doSomething];
});
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// set property to nil in a bit
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"SET PROPERTY TO NIL");
self.sut = nil;
});
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment