user1028028
user1028028

Reputation: 6433

NSThread sleepfortimeinterval blocks main thread

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 ....

Upvotes: 15

Views: 29014

Answers (3)

Brian
Brian

Reputation: 31272

Swift:

let nonBlockingQueue: dispatch_queue_t = dispatch_queue_create("nonBlockingQueue", DISPATCH_QUEUE_CONCURRENT)
dispatch_async(nonBlockingQueue) {
    NSThread.sleepForTimeInterval(1.0)
    dispatch_async(dispatch_get_main_queue(), {
        // do your stuff here
    })
}

Upvotes: 0

John
John

Reputation: 2694

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
    }); 
});

Upvotes: 23

sbarow
sbarow

Reputation: 2819

Try create a method in your thread class called sleepThread

-(void)sleepThread
{
   [NSThread sleepForTimeInterval:timeoutTillAnswer];
}

Then to make it sleep from your main thread

[self.botThread performSelector:@selector(sleepThread) onThread:self.botThread withObject:nil waitUntilDone:NO];

To send updated to your main thread from your bot thread.

dispatch_async(dispatch_get_main_queue(), ^{
    [MainClass somethinghasUpdated];
});

Side Note

To create the RunLoop I think all you need to do is

// Run the Current RunLoop
[[NSRunLoop currentRunLoop] run];

Upvotes: 1

Related Questions