Re: Newbie threading design question
Re: Newbie threading design question
- Subject: Re: Newbie threading design question
- From: Dustin Voss <email@hidden>
- Date: Tue, 4 Nov 2003 17:07:21 -0800
On 4 Nov, 2003, at 12:41 PM, Kurt Marek wrote:
I have a mainController that handles most of the UI. I have a custom
class called query that performs web searches, parses the results and
stores instance variables relating to the nature of the search (name,
last time performed, etc). Each instance of query is then stored in an
array called queryArray in mainController. Each instance of query
generates a bunch of instances of the class result based on the web
search. The method for executing the web search is in the query class
and it can sometimes be a little slow and can block the UI for 5-10
seconds when the app is run without multiple threads. Seems like a
perfect case for spawning a new thread to run the search.
There's a class someone wrote call ThreadWorker
(
http://www.cocoadev.com/?ThreadWorker) which will probably handle all
your threading needs. But if you want to do it yourself, read on.
I have a searchField in my interface and when editing is finished it
fires a performSearch method in mainController. mainController then
creates an instance of query for the search and executes the method
executeSearch:searchItem from the newly created query. searchItem is
just the contents of the searchField. I want a progressIndicator to be
animated when the search is begun and stopped when it is finished, so I
think I need to set up an NSConnection and ports between the two
threads.
You could set up an NSConnection, but I think it would be easier to use
performSelectorOnMainThread: to signal the start and stop of the search
to the main thread, which can then deal with the progress indicator.
This provides one-way communication from your worker thread to the main
thread. If you need two-way communication, you might find the
InterThreadMessaging library to be easier
(
http://www.cocoadev.com/?InterThreadMessaging).
My question is when do I detach the new thread? Is it okay to
create an instance of an object in the main thread and then detach the
new thread using a method from the newly created object, like this:
query *search=[[query alloc] init];
[NSThread detachNewThreadSelector:@selector(executeSearch:)
toTarget:search withObject:searchItem];
If I do it this way, is it bad to set up the NSConnection serverObject
in the secondary thread as an object that was instantiated in the main
thread?
Yeah, you could do it this way. But what I usually do is make a
separate class for each worker thread. I instantiate it, save it in a
variable, call methods to set up the task, and call a method in that
class like "startThread". That method sets up the communication and
launches a new thread with a method like "doWork" or whatever.
Basically, I keep everything related to that thread and operation in
one class.
Would it be better to not instantiate query in the main thread,
but to do something like:
[NSThread detachNewThreadSelector:@selector(connectWithPorts:)
toTarget:[query class] withObject:portArray];
and then in connectWithPorts instantiate the query?
I wouldn't do it this way. You need to access the UI to set up the task
in the separate thread, and that is best done in the main thread.
If I do it this
way, will the instance of query (and all of the instances of result
that it creates) be destroyed when the thread ends? This would be a
problem since mainController needs them in its queryArray in the main
thread.
There is no automatic relationship between thread lifetime and object
lifetime (except for thread-local variables). You can, of course, code
such a relationship if you like.
I'm sorry it is so difficult for me to express what is confusing me,
but that's because I'm confused. :)
Threading can be confusing. :)
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.