我正在一些由NSOperation管理的NSOperationQueue对象中执行一些代码。代码还包含一个使用performSelector:withObject:afterDelay:的延迟方法调用。
问题是,应该称为延迟的相应选择器是,而不是在所有中调用。
在阅读了this answer to a StackOverflow question之后,我想这是因为NSOperation已经完成了,并且它的线程已经不存在了,“忘记”了对选择器的预定调用。
如何解决这个问题?NSOperation**?** 如何对中的方法执行延迟调用?
发布于 2011-12-30 15:02:42
一种可能是使用大中央调度,即dispatch_after()
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after(popTime, queue, ^{
...
});当然,您也可以创建自己的调度队列,也可以在dispatch_get_global_queue()中使用主队列,而不是dispatch_get_main_queue()。
https://stackoverflow.com/questions/8680227
复制相似问题