4

I'm scheduling events on the main run loop using the following code:

[stream setDelegate:self];
[stream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[stream open];

I assume that this means that NSStreamDelegate events are being scheduled on the main run loop and therefore can potentially impact UI responsiveness if frequent enough. This certainly seems to be the case in my program - even if the event handlers detach into a background thread without doing much work themselves.

How would I go about scheduling on a run loop other than mainRunLoop? Do I need to create a thread? Can I just create an NSRunLoop directly? What setup needs to be performed?

chris838
  • 5,148
  • 6
  • 23
  • 27

1 Answers1

0

My suggestion is to use a concurrent nsoperstion. I have a demo app on github that shows how to message such an object. You will find many such projects too. You can then use the performSelector variant that takes a thread specifier (the thread for such an operation is persistent).

The op has a run loop so can be messaged, a thread, can easily be cancelled, and you can create multiples of them.

David H
  • 40,852
  • 12
  • 92
  • 138
  • Nice, but what about that "demo app" in github - a link would be greatly appreciated. (hmmm, at least the name of it?) – Motti Shneor Nov 11 '15 at 10:57
  • @MottiShneor the code is 3 years old now but might be of help: https://github.com/dhoerl/Concurrent_NSOperations – David H Nov 11 '15 at 21:44