完成块?嵌入在同步工作流程中的异步流程 [英] Completion Blocks ? Asynchronous processes embedded in synchronous workflow
问题描述
长时间潜伏,第一次海报.我对Objective-C相对较新,因此如果我问一些简单的问题,我深表歉意.我的Google&堆栈溢出-fu让我失望了,所以我想也许有人可以帮忙.
Long time lurker, first time poster. I'm relatively new to objective-C so my apologies if I'm asking something fairly simple. My google & stack overflow-fu has let me down here, so I figured somebody could maybe help.
我有一个同步进程,该进程连续执行三个函数-称为A-> B-> C,在其中执行任务A,然后依次执行B和C.
I have a synchronous process executing, say, three functions in a row - call it A -> B-> C , where task A executes, followed by B, followed by C.
现在,B涉及一个异步过程,该过程具有委托回调以完成操作.但是B必须在执行C之前完成,因此我需要某种机制,使得在B完成之前不会触发C.我想必须为此问题设计一个通用的设计模式?
Now, B involves an asynchronous process with a delegate callback for completion. But B must complete before C is executed, so I need some mechanism such that C is not triggered before B has finished. I imagine there must be a common design pattern for this problem?
最初的解决方案是-
执行A
执行B
一会儿(!B完成){}
执行C
execute A
execute B
while (!B finished) {}
execute C
...但这似乎真是la脚.
...but this seems really lame.
我怀疑我可以用某种障碍来做到这一点,但是对于我一生,我只是想不通.有人可以帮忙吗?
I suspect I can do this with some kind of block, but for the life of me I just can't figure it out. Could anyone help?
感谢任何帮助!
纪尧姆
推荐答案
感谢所有退款-很抱歉没有尽快回复.我现在以与建议稍有不同的方式解决了这个问题:
Thanks for all the feeback - apologies for not responding sooner. I've now resolved this in a slightly different way to the suggestions:
首先,我将NSObject扩展为具有以下方法-
Firstly, I extended NSObject to have the following method -
#import "NSObject+LTExtensions.h"
@implementation NSObject (Testing)
- (void) performSelectorWithBlock: (SEL) selector withSemaphore:(dispatch_semaphore_t)semaphore
{
[self performSelector:selector]; // This selector should complete the semaphore
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_release(semaphore);
}
@end
这使我可以通过选择器执行一个块.当该块执行时,在其上执行的线程将等待,直到特定的分派信号量发出继续执行信号的信号为止.
This allows me to execute a block via a selector. When the block executes, the thread on which it is executed will wait until signaled to proceed by a specific dispatch semaphore.
接下来我们可以做的是:
What we can then do is as follows:
- 致电A
- 创建调度信号并定义执行B的选择器
- 调用上面定义的方法以执行B并等待选择器完成
- B完成后(通过委托回调),它向调度信号通知挂起等待状态
- 然后我执行C
所以我们有
A
B -> Asynchronous with delegate callback
C
这是实现上述内容的简单示例
Here's a simple example of how the above is implemented
-(void) methodA {
// ... do something
// Assign your semaphore (this is a dispatch_semaphore_t)
self.semaphore = dispatch_semaphore_create(0);
[self performSelectorWithBlock:@selector(methodB) withSemaphore:semaphore];
[self methodC];
}
-(void) methodB {
// ... do whatever needs to be done asynchronously
CFRunLoopRun();
}
-(void) methodBDelegateCallBack {
// This is called when B completes
// Signal completion
dispatch_semaphore_signal(self.semaphore);
CFRunLoopStop(CFRunLoopGetCurrent());
}
-(void) methodC {
...
}
工作得很好,没有任何问题(但是我是Obj C的新手,所以我的方法可能存在明显的问题).
Works very well without any issues (but I am new to Obj C, so there may be glaring issues with my approach).
这篇关于完成块?嵌入在同步工作流程中的异步流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!