在objective-c中setCompletionBlock的奇怪行为

时间:2016-06-14 21:19:26

标签: ios objective-c objective-c-blocks

GPUImageOutput<GPUImageInput> *filter;
GPUImageMovieWriter *movieWriter;
.
.
.
__block BOOL finished = NO;
__weak id weakMovieWriter = movieWriter;
[movieWriter setCompletionBlock:^{
    NSLog(@"Completed Successfully");
    __strong id strongMovieWritier = weakMovieWriter;
    [strongMovieWritier finishRecording];
    [filter removeTarget:strongMovieWritier];
    finished = YES;
    NSLog(finished ? @"Yes" : @"No");

}];
while (!finished);
[self completeTransaction]; // this method is not executed!!!

关于上面的代码,方法[self completeTransaction]不会被执行,即使我知道变量&#34;已完成&#34;更改为YES。

但如果我改变这段代码

while (!finished);

while (!finished){
    NSLog(@"Whatever");
}

调用方法[self completeTransaction]。

有人知道为什么会这样吗?

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

再次审核您的代码我认为

[self completeTransaction];

执行很少见,因为完成块

[movieWriter setCompletionBlock:^{
    NSLog(@"Completed Successfully");
    __strong id strongMovieWritier = weakMovieWriter;
    [strongMovieWritier finishRecording];
    [filter removeTarget:strongMovieWritier];
    finished = YES;
    NSLog(finished ? @"Yes" : @"No");

}];

同步执行你的[self completeTransaction];我假设在主线程上执行,我建议你这样做

GPUImageOutput<GPUImageInput> *filter;
GPUImageMovieWriter *movieWriter;
.
.
.
__weak id weakMovieWriter = movieWriter;
__weak weakSelf = self;
[movieWriter setCompletionBlock:^{
    NSLog(@"Completed Successfully");
    __strong id strongMovieWritier = weakMovieWriter;
    [strongMovieWritier finishRecording];
    [filter removeTarget:strongMovieWritier];
    [weakSelf completeTransaction];
}];

正如我在之前的评论中所说,希望这可以帮助你