I'm trying to run NSStreams
in a thread in my project. I created a thread below:
self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(createStreams:) object:handler];
[self.thread start];
And the thread selector implementation is blow:
- (void)createStreams:(OpenStreamHandler)handler {
assert(![[NSThread currentThread] isMainThread]); // make sure this thread is not a main thread
// open input
[self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.inputStream open];
// open output
[self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.outputStream open];
[[NSRunLoop currentRunLoop] run]; // start a run loop, look at the next point }
Right now I want to remove that stream runloop
running in that thread using something like:
// Shut down input
[self.inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.inputStream close];
self.inputStream = nil;
// Shut down output
[self.outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.outputStream close];
self.outputStream = nil;
How could I run above code in the correct thread (get right currentRunLoop
) without using method swizzling?