@interface
ToolDemoViewController
()
{
BOOL isSuccess;
{
BOOL isSuccess;
void (^testBlock2)(NSString *str);
}
@property
(
strong
,
nonatomic
)
NSString
*testName;
@property
(
nonatomic
,
copy
)
void
(^testBlock)(
NSString
*str);
#pragma mark -
test for circular references
- (
void
)testWeakAction {
// 这里不需要 __weak
[ UIView animateWithDuration : 0.3 animations :^{
self . view . backgroundColor = [ UIColor redColor ];
self . testName = @"sadgf" ;
}];
// 这里不需要 __weak dispatch_async(dispatch_get_main_queue(), ^{self.view.backgroundColor= [UIColorblueColor];
self . testName = @"dddd" ;
});
// Need __weak __weak typeof ( self ) wSelf = self ; testBlock2 = ^( NSString *str) { NSLog ( @"===%@" ,wSelf. testName ) ; }; // Test call if ( self . testBlock ) { self . testBlock ( @" test " ); } if ( testBlock2 ) {
testBlock2 ( @"ddd" );
// 这里不需要 __weak
[ UIView animateWithDuration : 0.3 animations :^{
self . view . backgroundColor = [ UIColor redColor ];
self . testName = @"sadgf" ;
}];
// 这里不需要 __weak dispatch_async(dispatch_get_main_queue(), ^{self.view.backgroundColor= [UIColorblueColor];
self . testName = @"dddd" ;
});
// Need __weak __weak typeof ( self ) wSelf = self ; testBlock2 = ^( NSString *str) { NSLog ( @"===%@" ,wSelf. testName ) ; }; // Test call if ( self . testBlock ) { self . testBlock ( @" test " ); } if ( testBlock2 ) {
testBlock2 ( @"ddd" );
}
void
(^testTempBlock) (
NSString
* str);
// No need for __weak here
testTempBlock = ^( NSString *str) {
NSLog ( @"%@" , self . testName );
};
// The call of the block in the test method if (testTempBlock) { testTempBlock( @"xiao" );
// No need for __weak here
testTempBlock = ^( NSString *str) {
NSLog ( @"%@" , self . testName );
};
// The call of the block in the test method if (testTempBlock) { testTempBlock( @"xiao" );
}
// Special handling is required when there is a delay in the block
self.testBlock = ^(NSString *str) {
ToolDemoViewController *strongSelf = wSelf;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// After the page returns to the previous page, it will be executed and it will be empty. . .
NSLog(@"name111===%@,,,%@",strongSelf.testName,str);
});
};
if (self.testBlock) {
self.testBlock(@"xiaoming");
}
}