Re: NSTextView won't update via NSThread
Re: NSTextView won't update via NSThread
- Subject: Re: NSTextView won't update via NSThread
- From: Jens Alfke <email@hidden>
- Date: Fri, 6 Nov 2009 14:07:10 -0800
On Nov 6, 2009, at 1:50 PM, Dalton Hamilton wrote:
Well, I would think Cocoa would let me manage whether I'm calling it
multiple times and just do what I'm asking.
Thread-safety of a complex system is hugely more complicated than just
managing "calling it multiple times". It's nightmarishly complex, and
generally just not worth it. Cocoa takes the approach that asynchrony
is a better approach than promiscuous multithreading — most tasks like
I/O can be performed without direct use of background threads, and
when you do need them, you should use a message-passing paradigm
rather than trying to do UI work on the thread.
So, I now I'm trying to set an NSLock, then call the worker Thread
and put the output into a MutableString. The Thread then UnLocks
the NSLock which tells the Main Thread the NSMutableString has
data. The main thread calls [NSTextView setString:
myMutableString]; and this continues looping through the work
items. When all items are done, the NSTextView updates all at once
-- nothing displays in the NSTextView until the thread is finished
running even though my main program/thread is continually blocking
on the LOCK, obtaining the LOCK (when the thread releases it) then
updating NSTextView, UNLOCKING, and looping. In general that's what
I'm doing. It just isn't working.
Well, first off you should never have the main (UI) thread blocking on
a lock. That's just asking for the dreaded spinning beachball.
A much simpler approach is to have your background thread call
performSelector:onMainThread to send a message back to the main
thread, which will be processed as part of the thread's runloop.
My GUI points to a LOGFILE directory and I loop through each file
doing an NSTask to call the Python script and read an NSPipe
Filehandle of the output. I need to display that in the NSTextView.
Generally you would hook the filehandles up to the runloop so you can
be notified asynchronously when data is available, without blocking.
—Jens_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden