请考虑以下代码:
@interface ViewController ()
@property (nonatomic, strong) dispatch_source_t source;
@end
@implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_resume(self.source);
dispatch_source_set_timer(self.source, dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC), 0.2 * NSEC_PER_SEC, 0.1 * NSEC_PER_SEC);
dispatch_source_set_event_handler(self.source, ^{
NSLog(@"%s",__func__);
});
}
@end
如果我使用dispatch_source_create
两次创建相同的DISPATCH_SOURCE_TYPE_TIMER
,则应用会崩溃。为什么呢?
如上例所示,创建一次和两次调度源有什么区别吗?
libdispatch.dylib`_dispatch_xref_dispose:
0x10015e174 <+0>: ldr w8, [x0, #48]
0x10015e178 <+4>: cmp w8, #2 ; =2
0x10015e17c <+8>: b.hs 0x10015e184 ; <+16>
0x10015e180 <+12>: ret
0x10015e184 <+16>: stp x20, x21, [sp, #-16]!
0x10015e188 <+20>: adrp x20, 41
0x10015e18c <+24>: add x20, x20, #3849 ; =3849
0x10015e190 <+28>: adrp x21, 46
0x10015e194 <+32>: add x21, x21, #2440 ; =2440
0x10015e198 <+36>: str x20, [x21]
0x10015e19c <+40>: ldp x20, x21, [sp], #16
-> 0x10015e1a0 <+44>: brk #0x1
答案 0 :(得分:4)
正如他们所说,一张图片胜过千言万语:
基本上你发布了一个暂停的派遣对象,CGD似乎是prohibited。
恢复第一个计时器会使崩溃消失:
self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_resume(self.source);
self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_resume(self.source);