Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9019391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:45:58+00:00 2026-06-16T04:45:58+00:00

I am trying to implement an operation queue and I have the following scenario:

  • 0

I am trying to implement an operation queue and I have the following scenario:

NSOperation A
NSOperation B
NSOperation C
NSOperation D
NSOperationQueue queue

I start adding A to queue.

During the execution of A I need to get some data from B and I can’t continue with A until B returns what I need.

The same situation will occur for B depending on C and for C depending on D.

To manage this, at each NSOperation I have this code:

NSOperation *operation; //This can be A, B, C, D or any other NSOperation

[self setQueuePriority:NSOperationQueuePriorityVeryLow]; //Set the current NSOperation with low priority

[queue addOperation: operation]; //Add the operation that I want to the queue

while(!operation.isFinished && !self.isCancelled){} //I need to wait the operation that I depend before moving on with the current operation

[self setQueuePriority:NSOperationQueuePriorityNormal]; //After the while, the other operation finished so I return my priority to normal and continue

if(self.isCancelled){ //If I get out of the while because the current operation was cancelled I also cancel the other operation.
[operation cancel];          
}

My problem is that when I have something like 3 or 4 NSOperations waiting and executing the while(!operacao.isFinished && !self.isCancelled){} my code just freeze because the NSOperation that is important to me don’t get executed, even if it have higher priority.

What I tried

  • Adding dependency during execution time but since my NSOperation is already running I doesn’t seems to have any effect.

  • Instead of adding the operation to queue, I can do something [operation start]. It works, but canceling the current operation will also cancel the other operations that I started?

  • I can do something like while(!operacao.isFinished && !self.isCancelled){[NSThread sleepForTimeInterval:0.001];}. It works, but is this the correct way? Maybe there is a better solution.

In this situation how I can guarantee that the operation that I want will run and the others will wait in background? What is the correct way to solve this?

If anyone question me why I don’t add the dependency before starting my queue its because an operation will need the other only if some conditions are true. I will know if I need other operation only during execution time.

Thanks for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:45:59+00:00Added an answer on June 16, 2026 at 4:45 am

    Here’s two ideas for you with contrived examples. I only used two operations but you could expand the concept to any number and/or nest them as needed.

    Example 1: Using Grand Central Dispatch

    GCD provides lightweight “dispatch groups”, which allow you to explicitly order tasks and then wait on their completion. In this case AlphaOperation creates a group and enters it, then starts BetaOperation, whose completionBlock causes the group to be left. When you call dispatch_group_wait, the current thread blocks until the number of times entering the group is equal to the number of times leaving it (a lot like retain count). Don’t forget to check the isCancelled state of the operation after any potentially long-running task.

    @interface BetaOperation : NSOperation
    @end
    @implementation BetaOperation
    - (void)main
    {
        NSLog(@"beta operation finishing");
    }
    @end
    
    @interface AlphaOperation : NSOperation
    @end
    @implementation AlphaOperation
    - (void)main
    {
        dispatch_group_t group = dispatch_group_create();
        dispatch_group_enter(group);
    
        BetaOperation *betaOperation = [[BetaOperation alloc] init];
        betaOperation.completionBlock = ^{
            dispatch_group_leave(group);
        };
    
        [betaOperation start];
    
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    
        if ([self isCancelled])
            return;
    
        NSLog(@"alpha operation finishing");
    }
    @end
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            AlphaOperation *operation = [[AlphaOperation alloc] init];
            [operation start];
        });
    
        return YES;
    }
    
    @end
    

    Example 2: Using a local NSOperationQueue

    Since you’re already with working operations, another option is creating a queue as a property of AlphaOperation, then adding BetaOperation and calling waitUntilAllOperationsAreFinished on the queue. This has an added benefit in that you can easily cancel the queue’s operations when AlphaOperation is cancelled, simply by overriding the cancel method.

    @interface BetaOperation : NSOperation
    @end
    @implementation BetaOperation
    - (void)main
    {
        NSLog(@"beta operation finishing");
    }
    @end
    
    @interface AlphaOperation : NSOperation
    @property (strong) NSOperationQueue *queue;
    @end
    @implementation AlphaOperation
    - (void)main
    {
        self.queue = [[NSOperationQueue alloc] init];
    
        BetaOperation *betaOperation = [[BetaOperation alloc] init];
        [self.queue addOperation:betaOperation];
        [self.queue waitUntilAllOperationsAreFinished];
    
        if ([self isCancelled])
            return;
    
        NSLog(@"alpha operation finishing");
    }
    
    - (void)cancel
    {
        [super cancel];
    
        [self.queue cancelAllOperations];
    }
    @end
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            AlphaOperation *operation = [[AlphaOperation alloc] init];
            [operation start];
        });
    
        return YES;
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a queue which blocks on the Pop operation if it's
I'm trying to implement the following operation in Java and am not sure how:
I'm trying to implement some bitwise operation on keys in redis. I have to
I'm trying implement A* Start path finding in my games(which are written with JavaScript,
Im trying to implement my own Huffman Coding algorithm and the priority queue for
I am trying to implement a custom endpoint/operation extension in my WCF service. I
I am trying to implement drag and drop between two listboxes. I have a
I'm trying to implement an All-To-All (i.e. MPI_Allgather) operation on a hyper-cube network using
I am trying to implement a unranked boolean retrieval. For this, I need to
In my program, I am trying to implement support for dual-stack operation (both IPv4

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.