Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why waitUntilAllOperationsAreFinished doesn't wait in this code?

-(NSInteger) buttonIndexWithMessage:(NSString *) title andArrayOfOptions:(NSArray *) options
{
    self.operation=[NSOperationQueue new];

    [self.operation addOperationWithBlock:^{
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
                                                                     delegate:self
                                                            cancelButtonTitle:nil
                                                       destructiveButtonTitle:nil
                                                            otherButtonTitles:nil];

            for (NSString * strOption in options) {
                [actionSheet addButtonWithTitle:strOption];
            }

            [actionSheet showInView:[BGMDApplicationsPointers window]];
        }];
        self.operation.suspended=true; //Okay make t
    }];

    [self.operation waitUntilAllOperationsAreFinished];//Don't get out of the function till user act.

    //Wait till delegate is called.
    return self.buttonIndex;//I want to return buttonIndex here.
}

The execution point keeps moving till return self.buttonIndex even though self.operation has not finished.

like image 809
user4951 Avatar asked Aug 06 '26 04:08

user4951


1 Answers

How do you know that self.operation has not finished? The operation you add to it is very fast to execute: it just adds another operation to the main queue.

You seem to think that the line

self.operation.suspended=true;

should block the ongoing operation. But from the documentation:

This method suspends or resumes the execution of operations. Suspending a queue prevents that queue from starting additional operations. In other words, operations that are in the queue (or added to the queue later) and are not yet executing are prevented from starting until the queue is resumed. Suspending a queue does not stop operations that are already running.

Your operation is already running, so is not affected.

Why don't you tell us what you are actually trying to achieve, and we can suggest good ways how to achieve that.

like image 144
fishinear Avatar answered Aug 07 '26 19:08

fishinear