“This is the 22nd day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

About GCD timer

The GCD timer actually uses a Dispatch source, which listens for and processes system kernel objects. Dispatch is similar to the producer-consumer pattern by listening to the system kernel object and automatically notifies the corresponding Dispatch queue after the producer produces data, which acts as the consumer. With system level calls, it’s more precise


GCD timer common methods

  • You need to set dispatch_source_t timer to a member variable. Otherwise, the dispatch_source_t timer will be released immediately

    @property (nonatomic, strong) dispatch_source_t timer;
    Copy the code
  • Creating a Timer object

    // Create a thread queue
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // Use the queue you created earlier to create the timer
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0.0, queue);
    Copy the code

    Parameter 1: DISPATCH_SOURCE_TYPE_TIMER is a timer type parameter 2, 3: the middle two parameters are useless for timers parameter 4: What queue is used

  • Set timer

    dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC, 0.0 * NSEC_PER_SEC);
    Copy the code

    Parameter 1: task timer parameter 2: task start time parameter 3: task interval (unit: nanosecond) Parameter 4: Acceptable error time. If 0 is set, no error is allowed

  • Setting timer Tasks

    dispatch_source_set_event_handler(self.timer, ^{
    
    });
    Copy the code
  • Start the timer. After the GCD timer is created, you need to start it manually

    dispatch_resume(self.timer);
    Copy the code
  • Suspend the timer. When suspending the timer, note that multiple suspended operations will cause a thread lock, that is, the number of suspended operations corresponds to the number of continued operations, that is, dispatch_suspend and dispatch_resume are paired, the timer will continue to work

    dispatch_suspend(self.timer);
    Copy the code
  • Stop timer

    dispatch_source_cancel(self.timer);
    Copy the code

Advantages:

  • GCD timer does not rely on Runloop, it is a standalone system
  • High accuracy, as low as 1 nanosecond
  • There is no invalidate method
  • No need to manage memory manually (the target method encapsulated here must be careful about circular references)

Use of GCD timer

Example:

@interface ViewController ()
@property (nonatomic, strong) dispatch_source_t timer;
@end

- (void)viewDidLoad {
    [super viewDidLoad];
    
    dispatch_queue_t queue = dispatch_get_main_queue();
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0.0, queue);
    
    dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC, 0.0 * NSEC_PER_SEC);
    
    dispatch_source_set_event_handler(self.timer, ^{
        NSLog(@"Scheduled task");
    });
    dispatch_resume(self.timer);
}
Copy the code

The log: