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 8259755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:56:14+00:00 2026-06-08T02:56:14+00:00

In reviewing my code, I’ve been seeing that in many places I have been

  • 0

In reviewing my code, I’ve been seeing that in many places I have been making the assumption that calling [NSBlockOperationInstance start]; will start this operation on the main thread. I don’t know why I thought this, but I shouldn’t have been so sure any way. I checked the documentation but couldn’t find any explicit mention of the thread the block would run on. However, asserting assert([NSThread isMainThread]); in the main body of the block does pass every time using start, so I’m not sure if this is a coincidence. Any one have more solid understanding of how this would work?

I forgot to mention that [op start] is being called on the main thread.

  • 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-08T02:56:15+00:00Added an answer on June 8, 2026 at 2:56 am

    OK, it all depends on where you call start(). While NSBlockOperation will farm out blocks to other threads, start() is synchronous, and will not return until all the blocks that have been given to NSBlockOperation have completed.

    While NSBlockOperation will concurrently execute the blocks it is given, NSBlockOperation itself is NOT concurrent (i.e., isConcurrent is false). Thus, according to the documentation, start() will execute in its entirety in the thread of the caller to start().

    Since the thread that calls start() will not return until all the blocks have executed, it makes sense to let the calling thread be involved in the thread pool that is executing the concurrent blocks. That is why you will see some blocks executing in the thread that called start().

    If you are seeing a block execute in the main thread, then you must have called it from the main thread.

    On a related note, if your NSBlockOperation contains a single block, than that block will always execute in the calling thread.

    Remember, if you want a NSOperation to be fully concurrent, you must implement the appropriate functionality in a subclass.

    Barring that, you can give any NSOperation to a NSOperationQueue, and it will execute concurrently, because the NSOperation is given to a queue, and the thread running the operation calls start().

    Personally, I do not see any advantage in using NSBlockOperation over dispatch_async() unless I need to use its features. If you are only executing one block, just call

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ });
    

    If you want to utilize the features of NSBlockOperation, but you do not want to wait for them to complete in the current calling thread, it still makes sense to do this…

    // Add lots of concurrent blocks
    [op addExecutionBlock:^{ /*whatever*/ }];
    // Execute the blocks asynchronously
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [op start];
        // Now do what you want after all the concurrent blocks have completed...
        // Maybe even tell the UI
        dispatch_async(dispatch_get_main_queue(), ^{
            // Update the UI now that all my concurrent blocks have finished.
        });
    });
    

    EDIT
    To address your comment to tc’s answer…

    If you call

    op = [NSBlockOperation blockOperationWithBlock:^{assert([NSThread isMainThread])}];
    [op start];
    

    from the main thread, then there are some guarantees, and some high probabilities.

    First, you are guaranteed that [op start] will run to completion in the calling thread. That’s because NSBlockOperation does not override the default behavior of NSOperation that specifies it is NOT a concurrent operation.

    Next, you have a very high probability that if the NSBlockOperation only has one block, that it will run in the calling thread. You have almost the same probability that the first block will run in the calling thread.

    However, the above “probabilities” are not guarantees (only because the documentation does not say it). I guess, some engineer may find some reason to spin that single block to one of the concurrent queues, and just have the calling thread join on the operation that is executing in another thread… but I highly doubt that.

    Anyway, maybe your confusion comes from the fact that the documentation for NSBlockOperation says it executes block concurrently, which it does. However, the operation itself is not concurrent, so the initial operation is synchronous. It will wait for all blocks to execute, and it may (or may not) execute some of them on the calling thread.

    While there is no guarantee, I find it highly unlikely that a NSBlockOperation with only one block will do anything other than execute on the calling thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reviewing some code that looks like this: class A; // defined
I have a code piece that I am reviewing (using FindBugs ). public class
When coding and reviewing code, it's easy to spot places where a design pattern
I'm reviewing some code in VB.net, and in a validation object they have written
I'm current writing a short bit of code that will compare an etag for
When reviewing code I came across something odd, someone had read that you can
Reviewing some code that contains a bunch indirectly nested transactionscopes. I would like to
I'm reviewing some code, and saw something I have never seen before. I did
I was reviewing code and found that $(#item1 #item2) and $(#item1>#item2) are used interchangeably.
Is there a compiler setting that can warn about these? Currently I'm reviewing code

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.