异步分派递归块
假设我运行此代码:
__block int step = 0;
__block dispatch_block_t myBlock;
myBlock = ^{
if(step == STEPS_COUNT)
{
return;
}
step++;
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 2);
dispatch_after(delay, dispatch_get_current_queue(), myBlock);
};
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 2);
dispatch_after(delay, dispatch_get_current_queue(), myBlock);
该块从外部调用一次。当到达内部调用时,程序崩溃,没有任何细节。如果我在任何地方都使用直接调用而不是 GCD 调度,那么一切都会正常工作。
我还尝试使用该块的副本调用dispatch_after。我不知道这是否是朝着正确方向迈出的一步,但这还不足以使其发挥作用。
有想法吗?
Suppose I run this code:
__block int step = 0;
__block dispatch_block_t myBlock;
myBlock = ^{
if(step == STEPS_COUNT)
{
return;
}
step++;
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 2);
dispatch_after(delay, dispatch_get_current_queue(), myBlock);
};
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 2);
dispatch_after(delay, dispatch_get_current_queue(), myBlock);
The block is invoked once from outside. When the inner invocation is reached, the program crashes without any details. If I use direct invocations everywhere instead of GCD dispatches, everything works fine.
I've also tried calling dispatch_after with a copy of the block. I don't know if this was a step in the right direction or not, but it wasn't enough to make it work.
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当尝试解决这个问题时,我发现一段代码可以解决大部分与递归块相关的问题。我无法再次找到源代码,但仍然有代码:
RecursiveBlock
允许无参数块。它可以针对单个或多个参数块进行重写。使用此构造简化了内存管理,例如没有保留周期的机会。When trying to solve this problem, I found a snippet of code that solves much of the recursive block related issues. I have not been able to find the source again, but still have the code:
RecursiveBlock
allows for non-argument blocks. It can be rewritten for single or multiple argument blocks. The memory management is simplified using this construct, there is no chance of a retain cycle for example.我的解决方案完全源自 Berik 的解决方案,因此他在这里得到了所有的功劳。我只是觉得“递归块”问题空间(我在其他地方没有找到)需要一个更通用的框架,包括这里介绍的异步情况。
使用这三个第一个定义使得第四和第五种方法 - 这只是示例 - 成为可能,这是一种非常简单、万无一失且(我相信)内存安全的方法,可以将任何块递归到任意限制。
您会注意到,在以下两个示例中,与递归机制交互的机制非常轻量级,基本上相当于必须将一个块包装在
recurse
中,并且该块必须采用BOOL *stop
变量,应该在某个点设置该变量以退出递归(某些 Cocoa 块迭代器中的熟悉模式)。recurseAfter
的工作原理大致相同,但我不会在这里提供一个人为的示例。我正在毫无问题地使用所有这三个,替换我旧的-performBlock:afterDelay:
模式。My solution was derived entirely from Berik's, so he gets all the credit here. I just felt that a more general framework was needed for the "recursive blocks" problem space (that I haven't found elsewhere), including for the asynchronous case, which is covered here.
Using these three first definitions makes the fourth and fifth methods - which are simply examples - possible, which is an incredibly easy, foolproof, and (I believe) memory-safe way to recurse any block to arbitrary limits.
You'll note that in the following two examples that the machinery of interacting with the recursion mechanism is extremely lightweight, basically amounting to having to wrap a block in
recurse
and that block must take aBOOL *stop
variable, which should be set at some point to exit recursion (a familiar pattern in some of the Cocoa block iterators).recurseAfter
works much the same, though I won't offer a contrived example here. I am using all three of these without issue, replacing my old-performBlock:afterDelay:
pattern.看起来除了延迟变量之外没有任何问题。该块始终使用在第 1 行生成的相同时间。如果您想延迟分派该块,则必须每次都调用dispatch_time。
编辑:
我明白了。
该块通过块文字存储在堆栈中。 myBlock 变量替换堆栈中块的地址。
首先dispatch_after从myBlock变量中复制了块,该变量是堆栈中的地址。并且这个地址此时是有效的。该块位于当前范围内。
之后,该块的范围就被限定了。此时 myBlock 变量的地址无效。 dispatch_after 在堆中具有复制的块。这是安全的。
然后,块中的第二个dispatch_after尝试从无效地址的myBlock变量进行复制,因为堆栈中的块已经超出了范围。它将执行堆栈中损坏的块。
因此,您必须 Block_copy 该块。
当您不再需要该块时,不要忘记 Block_release 该块。
It looks like there are no problem except delay variable. The block uses always the same time that is generated at line 1. You have to call dispatch_time every time if you want to delay dispatching the block.
EDIT:
I understand.
The block is stored in stack by the block literal. myBlock variable is substituted for the address of the block in stack.
First dispatch_after copied the block from myBlock variable that is the address in stack. And this address is valid at this time. The block is in the current scope.
After that, the block is scoped out. myBlock variable has invalid address at this time. dispatch_after has the copied block in heap. It is safe.
And then, second dispatch_after in the block tries to copy from myBlock variable that is invalid address because the block in stack was already scoped out. It will execute corrupted block in stack.
Thus, you have to Block_copy the block.
And don't forget Block_release the block when you don't need it any more.
选择自定义调度源。
Opt for a custom dispatch source.
我认为如果您希望它保留下来,您必须复制该块(当您不想让它再调用自身时释放它)。
I think you have to copy the block if you want it to stick around (releasing it when you don't want it to call itself anymore).