Runloop介绍
http://www.cnblogs.com/tangbinblog/archive/2012/12/07/2807088.html
巧妙使用block
+ (void)spinUntilCompletion:(void (^)(void(^completion)()))block
{
__block volatile atomic_bool didFinish = false;
block(^{
atomic_fetch_or(&didFinish, true);//先返回didFinish,再修改didFinish
});
while (!atomic_fetch_and(&didFinish, false)) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:FBWaitInterval]];
}
}
- atomic_fetch_or原子操作bool值,或
- atomic_fetch_and原子操作bool值,与,先返回本身值做判断
执行完block操作,在block中调用completion,didFinish值为true,则runloop停止,否则while循环一直监听didFinish值。