dispatch_block_t
定义:typedef void (^dispatch_block_t)(void);
dispatch_block_t b = dispatch_block_create(DISPATCH_BLOCK_BARRIER, ^{
//
});
b();
Block_release(b);
一.块回调
定义属性方法
@interface TKClient : NSObject
@property (nonatomic,strong) dispatch_block_t successBlock;
- (void)doTest;
@end
在.m文件实现
@implementation TKClient
- (void)doTest{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self doLongWork];
NSLog(@"doLongWork");
dispatch_async(dispatch_get_main_queue(), ^{
if (self.successBlock) {
self.successBlock();
}
});
});
}
- (void)doLongWork{
for (int i = 0; i<1000; i++) {
}
}
@end
测试一下
- (void)doSomething{
TKClient *client = [[TKClient alloc] init];
client.successBlock = ^{
NSLog(@"work finish");
};
[client doTest];
}
是不是很简单
其他简介*
- dispatch_block_flags
- dispatch_block_create_with_qos_class
- dispatch_block_perform
- dispatch_block_wait
- dispatch_block_notify
- dispatch_block_cancel
- dispatch_block_testcancel