正如标题所述,我有一个while循环,它将被执行直到满足某个条件,或者直到5秒过去。
解决此问题的最佳方法是什么?我看过一些关于NSTimer
的简单教程,但在我看来,在NSTimer
内触发的选择器将在指定的时间间隔后执行,无论如何。如果条件不满足,我只需要执行它......
答案 0 :(得分:1)
只需创建一个NSTimer计划操作存储计时器,如果达到您想要实现的目标,则停用此计时器,以便它不会触发该操作。
基本上:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(yourAction) userInfo:nil repeats:NO];
some code
//for deactivating the timer
[timer invalidate];
timer = nil;
你可以在主线程上启动NSTimer(以确保上面的代码有效):
[self performSelectorOnMainThread:@selector(startTimerMethod) withObject:someOrNoObject waitUntilDone:NO];