From my main thread I call a selector using
[self performSelectorInBackground:@selector(startTask) withObject:nil];
This is the method startTask:
-(void)startTask{
NSTask *task = [[NSTask alloc] init];
NSPipe *pipe = [[NSPipe alloc] init];
NSFileHandle *fh = [pipe fileHandleForReading];
NSArray *args = [NSArray arrayWithObjects:@"-z",iPAddress, [NSString stringWithFormat:@"%@",portNumber], nil];
[task setLaunchPath:@"/usr/bin/nc"];
[task setArguments:args];
[task setStandardOutput:pipe];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self];
[nc addObserver:self
selector:@selector(dataReady:)
name:NSFileHandleReadCompletionNotification
object:fh];
[task launch];
[fh readInBackgroundAndNotify];
}
This should prevent NSTask from blocking the main thread (and the UI). But it doesn't. If I remove
[task launch];
The main thread doesn't get blocked. What am I doing wrong? o_O
(BTW dataReady just handles the data. It's not this method, that blocks...)
EDIT: I just found out, that I am not calling the selector from the main thread. I call it from a separate thread! Unfortunately I have to call it from that thread.