I've not found any decent documentation that explains the threading process for NSStream. To be specific, let's go for NSInputStream. Threading in Objective-C to me is currently a mystery simply because it appears to be so simple.
What my question is refers to this line primarily:
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
You can specify the run loop that the input stream will run in, which I thought was quite cool. The thing is, if I want the input and output streams to run in their own threads, and both are instantiated in a single class, say Connection, then how do you get them to run in their own threads?
The reason I ask is because of delegates. Previously we would've done [inputStream setDelegate:self] which means we have to declare stream:handleEvent to handle incoming/outgoing data.
So ultimately my question is, if you have one class which sets up the input and output stream, how do you both thread each stream and delegate responsibility for handling stream events to the current class?
Here's some code to chomp on:
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
I'm thinking the following:
- You can't delegate responsibility for both threads in the current class, you'd have to delegate to separate objects.
- One thread would do for both streams? (I don't personally think so, because input/output will run concurrently)
- I'm thinking this through wrong, and you can create a separate run loop and call scheduleRunLoop against some separate thread?
Any ideas?