我有一个异步运行并通过完成处理程序返回结果的方法。方法调用本身“立即”返回,但完成处理程序可能需要一些时间才能被调用。
我想序列化对这个方法的调用,这样如果完成处理程序在上一次调用中没有被处理,它就不会运行两次。
现在我正在使用一个标志
-(void)myAsyncMethod:(void(^)(NSError *error))block
{
if(_isRunning)
{
return; // would like to queue the call
}
_isRunning = YES;
// do some long running async work which themselves can have completion handlers and run asynchronously
// look for ever place with a call to "block"
_isRunning = NO;
block(error);
}
有没有办法在不使用 dispatch_sync、async 等方法调用中的标志的情况下做到这一点?