Newbie threading design question
Newbie threading design question
- Subject: Newbie threading design question
- From: Kurt Marek <email@hidden>
- Date: Tue, 4 Nov 2003 12:41:17 -0800
I am new to threading and have tried to read all of the available Apple
docs as well as some articles over at cocoa-dev, stepwise, and
searching the archives here. I think I get the basics of detaching a
thread and setting up the communication between threads, but I have a
design question that is really flummoxing me and preventing me from
implementing threads.
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.
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. 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? 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? 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.
I'm sorry it is so difficult for me to express what is confusing me,
but that's because I'm confused. :)
Kurt
_______________________________________________
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.