对于应该在后台线程上定期执行的操作,我通常会使用NSTimer
。我想知道是否使用GCD有任何缺点:
//Set up a dispatch queue owned by an instance of the class. (ie in init).
dispatch_queue_t backgroundQueue = dispatch_queue_create("some.queue",
DISPATCH_QUEUE_SERIAL);
- (void)scheduleRefresh
{
__weak id weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 120 * NSEC_PER_SEC),
backgroundQueue, ^
{
//Do some recurring task.
//Now, schedule again, by calling recursively, unless weakSelf is nil
[weakSelf scheduleRefresh]
});
}