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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:00:38+00:00 2026-06-01T07:00:38+00:00

I found this thread as a reference: How do I wait for an asynchronously

  • 0

I found this thread as a reference: How do I wait for an asynchronously dispatched block to finish? and am using that code (signaling the semaphore inside the block) to wait on my thread to finish before finishing the test.

However my semaphore never signals that its done… so I stepped through the code and found that the code inside this little snippet never runs:

        dispatch_async(dispatch_get_main_queue(), ^{
            [self removeDownloadSpinner];
            block(callback);
        });

Does dispatch_get_main_queue() not fire when I am unit testing? If not, how do you unit test async blocks?

EDIT (Here is a little more code):
Basically I have a “ServerRequest” class that contains all the incoming/outgoing network requests for the app. One method is this (completion block is typedeffed):

+(void)checkPlayerCode:(NSString *)playerCode completionHandler:(PlayerCompletionBlock)block {
  [self addDownloadSpinner];
  NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/players/%@",kBaseURL, playerCode]];
  NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
  [ServerConnectionRequest sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    NSDictionary *p = [data objectFromJSONData];
    Player *player = [[Player alloc] init];
    player.last_name = [p objectForKey:@"last_name"];
    if (!error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self removeDownloadSpinner];
            block(player);
        });

    }
}]; 

}

Which is called from a uiviewcontroller like this:

    [ServerRequest checkPlayerCode:codeCell.textField.text completionHandler:^(Player *p){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"wooo block" message:@"works" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert show];
}];

However the dispatch_get_main_queue() block never gets fired.

  • 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-01T07:00:40+00:00Added an answer on June 1, 2026 at 7:00 am

    The problem is that you are waiting on the semaphore on the main thread and simultaneously are trying to execute some code on the main tread with dispatch_async(dispatch_get_main_queue(), …).
    Try to use NSRunLoop’s runUntilDate, like

    __block volatile bool loadComplete = false;
    dispatch_async(dispatch_get_main_queue(), ^(..)
                                                    {                                                                     
                                                        ..
                                                        loadComplete = true;
                                                    });
    
    NSDate* startTime = [NSDate date];
    while ( !loadComplete ) 
    {
        NSDate* nextTry = [NSDate dateWithTimeIntervalSinceNow:0.1];
        [[NSRunLoop currentRunLoop] runUntilDate:nextTry]; 
    
        if ( [nextTry timeIntervalSinceDate:startTime] > ../* some appropriate time interval here */ )
            STAssertTrue(false, @"TIMEOUT");
    }
    
    // Assertions here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading through this thread: Hidden Features of JavaScript? and found this post:
I'm trying to select an select element by name I found this thread showing
Okay, I have done a bit of searching online and found this thread, but
I have set a property across threads before and I found this post Cross-thread
I am using this code: private void Form1_Load(object sender, EventArgs e) { } private
I found this great library for Silverlight that basically a port of libjpeg, so
I have learnt from this thread garbage collection with node.js that node.js uses a
I found some threads saying this was doable, but did not find specific instructions
Found this: Sub SurroundWithAppendTag() DTE.ActiveDocument.Selection.Text = .Append( + DTE.ActiveDocument.Selection.Text + ) End Sub But
found this regex: insert every 10 characters: $text = preg_replace(|(.{10})|u, \${1}. , $text); can

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.