简体   繁体   中英

strange behavior of setCompletionBlock in objective-c

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!!!

Concerning the above code the method [self completeTransaction] is not executed, even when i know the variable "finished" is changed to YES.

But if i change this piece of code

while (!finished);

to

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

The method [self completeTransaction] is called.

Anybody know why this happens?

Thanks for your time.

reviewing again your code I think if

[self completeTransaction];

executes is rare because the completion block

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

}];

executes a-synchronically and your [self completeTransaction]; I assume that execute on main thread, I recommend you to do this

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];
}];

instead as I said on my previous comment, Hope this helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM