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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:43:42+00:00 2026-06-15T02:43:42+00:00

I have an NSObject that I create inside a block. As per the code

  • 0

I have an NSObject that I create inside a block. As per the code below:

__block NSObject *myObject;

[self myMethod:^{
    myObject = [[NSObject alloc] init];
    ....
}];

if(myObject == nil){
    NSLog(@"Why is my object nil?!");
}

In the definition of myMethod I have the following:

  backgroundQueue = dispatch_queue_create("backgroundqueue", NULL);

            dispatch_async(backgroundQueue, 
                           ^{

                               dispatch_async(dispatch_get_main_queue(), 
                                              ^{
                                                  if(block){
                                                      block();//Never called.
                                                  }
                                              });

However the block is never called.

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

    The problem here is that you never seem to execute the block in which you instantiate myObject. For illustration, run this little program:

    #import <Foundation/Foundation.h>
    
    typedef void(^MyTestBlock)(void);
    
    @interface Foo:NSObject
    - (id)initWithBlock:(MyTestBlock)aBlock;
    - (void)someMethod;
    @end
    
    @implementation Foo {
        MyTestBlock _block;
    }
    
    - (id)initWithBlock:(MyTestBlock)aBlock {
        self = [super init];
        if( !self ) { return nil; }
        _block = aBlock;
        return self;
    }
    
    - (void)someMethod {
        _block();
    }
    
    @end
    
    int main(int argc, char *argv[]) {
        @autoreleasepool {
            __block NSObject *myObject;
    
            Foo *myFoo = [[Foo alloc] initWithBlock:^{
                myObject = [[NSObject alloc] init];
            }];
            [myFoo someMethod];
    
            NSLog((myObject)?@"Your object was created":@"Why is my object nil?");
        }
    }
    

    This prints 2012-11-26 05:00:58.519 Untitled 2[23467:707] Your object was created to the console. The point is that blocks don’t execute themselves. In the code above, although we set the block as an ivar of the class, we don’t execute it until we call someMethod on our Foo.

    EDIT:

    An edit to your question states that the block is not executed in the context of an asynchronous dispatch block sent to the main queue. If this is a command line application, then you must call dispatch_main() at the end of main. See the man page for dispatch_get_main_queue(). Here is a full working command line application to illustrate this, as well as issues related to race conditions:

    #import <Foundation/Foundation.h>
    
    typedef void(^MyTestBlock)(void);
    
    @interface Foo:NSObject
    - (id)initWithBlock:(MyTestBlock)aBlock;
    - (void)someMethod;
    @end
    
    @implementation Foo {
        MyTestBlock _block;
    }
    
    - (id)initWithBlock:(MyTestBlock)aBlock {
        self = [super init];
        if( !self ) { return nil; }
        _block = aBlock;
        return self;
    }
    
    - (void)someMethod {
        dispatch_queue_t backgroundQueue = dispatch_queue_create("backgroundqueue", NULL);
        dispatch_async(backgroundQueue, ^{
            dispatch_queue_t innerQueue = dispatch_get_main_queue();
            dispatch_async(innerQueue, ^{
                if( _block){
                    NSLog(@"Will call block.");
                    _block();
                }
            });
        });
    
    }
    
    @end
    
    int main(int argc, char *argv[]) {
        @autoreleasepool {
            __block NSObject *myObject;
    
            Foo *myFoo = [[Foo alloc] initWithBlock:^{
                myObject = [[NSObject alloc] init];
            }];
            [myFoo someMethod];
    
            //  this log statement should show that myObject is nil because it will (probably)
            //  be executed before your block.
            NSLog((myObject)?@"Your object was created":@"Why is my object nil?");
    
            //  wait a little bit to resolve race condition (just for illustrative purposes)
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.4f * NSEC_PER_SEC);
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                NSLog((myObject)?@"Your object was created":@"Why is my object nil?");
            });
        }
        //  this isn't a Cocoa app, so must call dispatch_main() at end of main
        dispatch_main();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume that I have this piece of code: @interface Foo : NSObject { Bar
I would like to create an nsobject that will have all of my methods
I have an object from a NSObject class that I call brand and has
I have a very basic data class that is subclassed from NSObject. I declare
I have a NSObject @interface MyObject : NSObject @property bool pName1; @property NSString *pName2;
I have a couple UIViewControllers that I am trying to access an array inside
I have a class (from NSObject ) that contains: NSString name int position float
I have a custom class of type NSObject that contains a single NSMutableArray. This
I have an application that intends to create a popup window when a button
I have a problem with the init() method of a standard NSObject. I wrote

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.