Ios 并行NSTimers多线程

Ios 并行NSTimers多线程,ios,objective-c,multithreading,Ios,Objective C,Multithreading,我有两个NSTimer,应该彼此平行运行。每个计时器控制UI的特定部分(Timer1=UI a,Timer2=UI B)用户界面A和用户界面B在其各自的计时器到达X间隔时发生变化。但是,为了进行更改,UI A需要检查UI B的状态 当我运行两个NSTimer时,它们之间有1秒的延迟: firstSemaphoreTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(firstT

我有两个NSTimer,应该彼此平行运行。每个计时器控制UI的特定部分(
Timer1
=UI a,
Timer2
=UI B)<代码>用户界面A和
用户界面B
在其各自的计时器到达X间隔时发生变化。但是,为了进行更改,
UI A
需要检查
UI B
的状态

当我运行两个NSTimer时,它们之间有1秒的延迟:

    firstSemaphoreTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(firstTimerTick) userInfo:nil repeats:YES];
    secondSemaphoreTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(secondTimerTick) userInfo:nil repeats:YES];

如何使用线程同步这两个计时器?或者你推荐什么方法?

你不需要两个计时器。您可以使用一个计时器处理这两个任务

NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
// ...
-(void)tick:(sender)id{

void(^processBlock)(void)= ^{
    // process UI 1 and 2     
};
if([NSThread isMainThread]){
    processBlock();
}else{
    dispatch_async(dispatch_get_main_queue(), processBlock);
}
}

即使有两个计时器,也不能同时更新UI,因为它是在主队列上完成的。如果在更新依赖于它的UI后还需要执行其他操作,则始终可以通过调用dispatch_sync同步调度块。

如果需要使用多线程,正确的方法是什么?把行为想象成一个信号量。正如我所说的,你们只能更新主队列上的UI。你不能使用多线程。我的意思是,在不同的线程上运行
进程A
进程B
,并在主线程上更新UI…@OscarSwanros为什么需要在不同的线程上运行
A
B
?在任何情况下,都应该使用队列,或者干脆
将\u async()
分派到其中一个全局并发队列。或者问一个新问题,更好地描述你实际上在做什么。