यदि आप चाहते हैं एक दोहराई जाने वाली घड़ी एक dispatch_queue_t
पर लागू किया जा करने की कोशिश करो, dispatch_source_create
का उपयोगके साथ करें:
dispatch_queue_t queue = dispatch_queue_create("com.firm.app.timer", 0);
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 20ull * NSEC_PER_SEC, 1ull * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
// stuff performed on background queue goes here
NSLog(@"done on custom background queue");
// if you need to also do any UI updates, synchronize model updates,
// or the like, dispatch that back to the main queue:
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"done on main queue");
});
});
dispatch_resume(timer);
कि एक टाइमर है कि एक बार हर 20 सेकंड रन (dispatch_source_set_timer
को 3 पैरामीटर) बनाता है, (dispatch_source_set_timer
को 4 पैरामीटर) एक एक दूसरे के एक छूट के साथ।
इस टाइमर को रद्द करने के dispatch_source_cancel
का उपयोग करें:
dispatch_source_cancel(timer);
बस उपरोक्त को देखने के लिए एक वीसी में लोड किया गया कोई कंसोल आउटपुट उत्पन्न नहीं करता है, क्या गुम है? –
@ डेविडकर्ल्सन 'टाइमर' वैरिएबल वास्तव में एक वर्ग संपत्ति (या ivar) होना चाहिए। 'NSTiner' के विपरीत, यदि यह दायरे से बाहर हो जाता है, तो प्रेषण टाइमर रद्द कर दिया जाता है और हटा दिया जाता है। – Rob