NSNotification vs message callback?
NSNotification vs message callback?
- Subject: NSNotification vs message callback?
- From: Lloyd Sargent <email@hidden>
- Date: Mon, 6 Aug 2001 19:35:56 -0500
Okay, I'm working on a little class which either subclasses (not having
much luck there) or wraps NSTask with a threaded read loop which slurps
down the data from StandardOutput. As the data streams in, I extract
complete lines with carriage returns and use an NSConnection to send
each
line to an object with a message in the main thread, so as to avoid GUI
problems.
So. Right now, I supply the threaded task with an object and a selector
for accepting these line handling messages. However, should I be using
an
NSNotification for this? I thought maybe, since then multiple objects
could even respond to processing the line, but then I thought that this
would be far too heavyweight, since the message would be broadcast every
time a line came in.
And also, I was thinking of firing off a message every time a block of
data was read in from the child thread, just to provide even more
immediate data... and that I *definitely* thought would be too
intensive
using NSNotifications
Any notions on this?
Personally, I think what you are doing is the safest. NSConnections
allow you the ability to do some really cool things that you can't with
NSNotification. (Like if you set up something that is a server and later
decide "hey, maybe multiple people want to be able to look at this
junk"). Cool stuff.
However, the BIGGIE is that NSNotification is not really thread safe per
se. Which is, of course, TERRIBLY annoying, but there ya go.
For a really GOOD ARGUMENT (you say discussion, I say argument, <grin>)
on this checkout the following:
http://www.cocoadev.com/index.pl?NotificationSampleCode
It goes into how you can use it but then why it probably isn't a good
thing to use across threads. Personally, coming from a RTOS background,
I avoid things that I know aren't thread-safe. All it takes is ONE
object that you THINK is thread-safe (but ain't) and you are sooooo....
unhappy. (Although, as an aside, my FIRST, and only, deadly-embrace
occurred on Win95).
Personally, I'm of the background that if you can't see a good reason to
put it in the first cut of code, why burden yourself (and whoever
maintains it) with stuff you don't need?
Did any of that rambling answer your question?