我试图取消然后释放一个暂停的计时器,但是当我在它上面调用'dispatch_release'时,我立即得到EXC_BAD_INSTRUCTION。
这不是对计时器采取的一组有效操作吗?
定时器创建和暂停:
@interface SomeClass: NSObject { }
@property (nonatomic, assign) dispatch_source_t timer;
@end
// Class implementation
@implementation SomeClass
@synthesize timer = _timer;
- (void)startTimer
{
dispatch_queue_t globalQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
0, 0, globalQ);
dispatch_time_t startWhen = dispatch_walltime(DISPATCH_TIME_NOW, NSEC_PER_SEC * 1);
dispatch_source_set_timer(_timer, startWhen, 1 * NSEC_PER_SEC, 5000ull);
dispatch_source_set_event_handler(_timer, ^{
// Perform a task
// If a particular amount of time has elapsed, kill this timer
if (timeConstraintReached)
{
// Can I suspend this timer within it's own event handler block?
dispatch_suspend(_timer);
}
});
dispatch_resume(_timer);
}
- (void)resetTimer
{
dispatch_suspend(_timer);
dispatch_source_cancel(_timer);
// dispatch_release causes
// 'EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
dispatch_release(_timer);
self.timer = nil;
}
@end
此外,我可以在计时器源的 event_handler 块中调用 dispatch_suspend 吗?
任何帮助,将不胜感激。