Voglio simulare una comunicazione con un server. Come il server remoto avrà alcuni ritardi voglio usare un thread in background che ha su di essoNSThread sleepfortimeinterval blocks main thread
[NSThread sleepForTimeInterval:timeoutTillAnswer];
Il filo è creato con NSThread sub classificare e ha iniziato ... Tuttavia ho notato che sleepForTimeInterval sta bloccando il thread principale. .. Perché??? Non è un NSThread di sfondoThread di default?
Questo è come è stato creato il thread:
self.botThread = [[PSBotThread alloc] init];
[self.botThread start];
Ulteriori informazioni: Questo è il bot subclas filo
- (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];
}
voglio chiamare "messageForBot" dal thread principale ... anche il thread in background dovrebbe chiamare un metodo sul thread principale per comunicare .. Il sleep per l'intervallo di tempo dentro l'oggetto gManager ....
Mostrare come si sta creando il filo , cosa fa e quando si chiama 'sleepForTimeInterval:' (che ritarda il thread corrente in il tempo è chiamato). – Wain
Ecco come sto creando il thread ... [inizio self.botThread]; ... Che chiama il metodo principale PSBotThread sottoclasse NSThread ... – user1028028
Ma cosa fa? Dov'è il 'sleepForTimeInterval' a? – Wain