NSThread sleepfortimeinterval bloqueia thread principal
Eu quero simular uma comunicação com um servidor. Como o servidor remoto terá alguns atrasos eu quero usar um thread de fundo que tem sobre ele
[NSThread sleepForTimeInterval:timeoutTillAnswer];
O thread é criado com NSThread subclassificação e iniciado ... No entanto, notei que sleepForTimeInterval está bloqueando o thread principal ... Por que ??? Não é um NSThread um backgroundThread por padrão?
É assim que o segmento é criado:
self.botThread = [[PSBotThread alloc] init];
[self.botThread start];
Mais informações: Esta é a subclasse do segmento bot
- (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];
}
Eu quero chamar "messageForBot" a partir do segmento principal ... também o segmento de fundo deve chamar um método no thread principal para se comunicar .. O sono por hora intervail dentro do objeto gManager ....