Ios 有没有什么办法可以;在这里等着……”;在代码中-就像一个空循环?

Ios 有没有什么办法可以;在这里等着……”;在代码中-就像一个空循环?,ios,objective-c,semaphore,promise,nsnotificationcenter,Ios,Objective C,Semaphore,Promise,Nsnotificationcenter,考虑以下代码: [self otherStuff]; // "wait here..." until something finishes while(!self.someFlag){} [self moreStuff]; 请注意,这一切都发生在同一个线程上,我们不想转到另一个线程 其他的东西可以做一些事情,比如连接到云,从用户那里获取输入,等等。所以这需要很多时间,并且可以遵循许多可能的路径 当otherStuff最终完成时,otherStuff会将self.someFlag设置为true

考虑以下代码:

[self otherStuff];
// "wait here..." until something finishes
while(!self.someFlag){}
[self moreStuff];
请注意,这一切都发生在同一个线程上,我们不想转到另一个线程

其他的东西可以做一些事情,比如连接到云,从用户那里获取输入,等等。所以这需要很多时间,并且可以遵循许多可能的路径

当otherStuff最终完成时,otherStuff会将self.someFlag设置为true

这工作得很好,一点问题都没有——除了像这样用空循环烧掉处理器是站不住脚的

很简单,有没有一种方式可以说

halt here, until (some message, interrupt, flag, boolean, whatever?)
而不仅仅是在(!self.someFlag){}

(请注意,另一种选择是“链接”过程……因此在“otherStuff”的末尾,您和所有其他程序员必须“知道”您必须下一次调用“moreStuff”,而不管otherStuff的结果如何,等等。当然,当您必须添加新过程或更改事情的顺序时,这是非常混乱的。)干杯


顺便说一句,对于需要不同线程的情况,下面已经有两个很好的答案。我建议使用NSOperationQueue并等待所有任务在特定点完成。诸如此类:

self.queue = [[NSOperationQueue alloc] init];

// Ensure a single thread
self.queue.maxConcurrentOperationCount = 1;

// Add the first bunch of methods
[self.queue addOperation:[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(method1) object:nil]];
[self.queue addOperation:[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(method2) object:nil]];
[self.queue addOperation:[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(method3) object:nil]];

// Wait here
[self.queue waitUntilAllOperationsAreFinished];

// Add next methods
[self.queue addOperation:[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(method4) object:nil]];
[self.queue addOperation:[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(method5) object:nil]];

// Wait here
[self.queue waitUntilAllOperationsAreFinished];

HTH

这是一个使用信号量的解决方案,小心不要引入死锁-您需要某种方式来告诉您的应用程序某些事情已经完成,您可以像您建议的那样使用
nsnotificationcenter
这样做,但使用块要容易得多


我认为你应该考虑一下NSLock和NSCondition。这比无限循环更合适,我只是好奇。为什么需要这样的内联解决方案?malex——嗯,我真的不知道NSLocking在这里有什么帮助。也许我错过了什么。Andrey—看第一个示例假想代码块。移动物品、添加物品等都非常简单/优雅。继续写一个非内联解决方案!你在等什么?睡眠(1000000)能满足你的问题吗?可以在这里继续吗?如果可以的话,什么时候继续?很好的想法--但是这些人都会在不同的线程上,对吗?如果你将maxConcurrentOperationCount设置为1,就不会了。Marcio完全正确。我更新了上面的代码示例以反映这种行为。嗯,伙计们,我想知道,如果您将.maxConcurrentOperationCount设置为1,是否会将它们全部放在(1)个新的(不同的)线程上?还是让他们都在“我们的”同一条线上?干杯…在一个新的不同的线程。但我想你想把它放在同一条线上。或者在主线上?奥利弗,非常感谢你。。。啊。那么现在。。。这完全发生在“同一”线程上,对吗?(也就是说,你不必将其他完成的东西发送到另一个线程,或者类似的东西)谢谢!TBC,当你说避免死锁时,你的意思是:“别忘了”最终调用dispatch\u semaphore\u signal?或者更可怕的事情,我不明白谢谢<代码>其他完成的东西需要发生在另一个线程上你是对的啊,另一个线程,我明白了——废话!我正在寻找一个同样的解决方案。该死,我得把问题说清楚,对不起!但仍然非常有用。。。。。。。。再次感谢如果您在同一个线程上执行工作,那么您不需要等待,下一个操作只会在前一个操作完成后执行-这就是计算机的工作方式。
[self someOtherStuffWithCompletion:nil];

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

[self someOtherStuffWithCompletion:^{
  dispatch_semaphore_signal(semaphore);
}];

NSLog(@"waiting");
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"finished");

[self someOtherStuffWithCompletion:nil];