NSThread sleepfortimeinterval blocks main thread

user1028028 picture user1028028 · Aug 15, 2013 · Viewed 27k times · Source

I want to simulate a communication with a server. As the remote server will have some delays I want to use a background thread that has on it

 [NSThread sleepForTimeInterval:timeoutTillAnswer];

The thread is created with NSThread sub classing and started ... However I noticed that sleepForTimeInterval is blocking the main thread... Why??? Isn't a NSThread a backgroundThread by default?

This is how the thread is created:

   self.botThread = [[PSBotThread alloc] init];
    [self.botThread start];

Further info: This is the bot thread subclas

- (void)main
{
    @autoreleasepool {
        self.gManager = [[PSGameManager alloc] init];
        self.comManager = [[PSComManager alloc] init];
        self.bot = [[PSBotPlayer alloc] initWithName:@"Botus" andXP:[NSNumber numberWithInteger:1500]];
        self.gManager.localPlayer = self.bot;
        self.gManager.comDelegate = self.comManager;
        self.gManager.tillTheEndGame = NO;
        self.gManager.localDelegate = self.bot;
        self.comManager.gameManDelegate = self.gManager;
        self.comManager.isBackgroundThread = YES;
        self.comManager.logginEnabled = NO;
        self.gManager.logginEnabled = NO;
        self.bot.gameDelegate = self.gManager;
        BOOL isAlive = YES;
        // set up a run loop
        NSRunLoop *runloop = [NSRunLoop currentRunLoop];
        [runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
        [self.gManager beginGameSP];
        while (isAlive) { // 'isAlive' is a variable that is used to control the thread existence...
            [runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
        }



    }
}

- (void)messageForBot:(NSData *)msg
{
    [self.comManager didReceiveMessage:msg];
}

I want to call "messageForBot" from the main thread... also the background thread should call a method on the main thread to communicate.. The sleep for time intervail in inside the gManager object ....

Answer

John picture John · Aug 15, 2013

It blocks whatever thread sleepForTimeInterval is running on. Run it on another thread to simulate your server delay like this:

dispatch_queue_t serverDelaySimulationThread = dispatch_queue_create("com.xxx.serverDelay", nil);
dispatch_async(serverDelaySimulationThread, ^{
     [NSThread sleepForTimeInterval:10.0];
     dispatch_async(dispatch_get_main_queue(), ^{
            //Your server communication code here
    }); 
});