0

am new to ios programming. my app is linking with a library that opens NSStream and uses them as below:


[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                       forMode:NSRunLoopCommonModes];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                        forMode:NSRunLoopCommonModes];

: and events for above streams are handled by

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode

in same file (or default thread?)

I need to talk to server in another context and if I use NSStreams, do I have to setup a different thread/run loop? i don't want to touch above library code. how can i have another stream handling callback?

I saw this post. Is that applicable?

NSStream and Sockets, NSStreamDelegate methods not being called

thanks

Community
  • 1
  • 1
user3282227
  • 131
  • 2
  • 10

2 Answers2

0

The link you posted to is about "socket streams". "Socket streams" are a clever Posix thing where you have an output stream and an input stream connected together by the operating system, and everything you write to the output stream automatically appears as input in the input stream. Most likely not what you wanted.

If you have multiple input and output streams, you can schedule them all in the same run loop. Normally you'd just schedule them in the run loop of the main thread. If you use lots of streams so that processing the stream data in your main thread affects the responsiveness of your application, then you could create an extra thread that is just used for processing streams, and schedule everything in that thread's run loop.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • WHat you mentioned sounds like pipes in unix. @gnasher729: Yes, I'm also talking about sockets. THe library I linked with is public domain sksmtp which sends email. I need to write tcp client code to talk to my server. How do I handle tcp socket in my code? Only one of the sockets is active at anytime. thanks. – user3282227 Apr 05 '14 at 00:20
0

As you see in the code you cite, you schedule you're streams in the "currentRunLoop" defined as the runloop of the current thread. This means that if you call these lines in the context of some NSThread you created first - you'll receive the stream events on that thread's context. So you don't have to change these lines - only make sure you execute them in the same thread you want to use for networking.

Motti Shneor
  • 2,095
  • 1
  • 18
  • 24