views:

153

answers:

0

Hi

All examples/tutorials I can find regarding NSThreads and the only way I have ever dealt with them is using [NSThread detachNewThreadSelector:@selector(processDataMethod) toTarget:self withObject:nil]; to move all processing in a method to another thread sporting its own autoreleasePool etc.

This is good for "one shoot" operations that should just carry out and "disappear".

I have to make many small web service calls in bursts and I would like to create an NSThread object that stayed around for the duration of the applications run. Mostly to prevent spawning many new threads when bursts occur.

I looked at initWithTarget and [NSThread start] which seems to deal with what I am trying to do. I am just not sure how put it together, does the thread go into my object, do I pass the object to my thread etc.

In a pseudo code / explanation way I am trying to implement this:

MyController;

@interface
NSThread *threadProperty; //a property, holding a thread I would like to message
MyWebService *webServiceObject; //the Class that holds web service methods e.g. - (NSArray*) searchFor:(NSString*) searchStr; 

on viewDidLoad:

instantiate webServiceObject;
instantiate threadProperty;
hand threadProperty the webServiceObject.
start the thread. //make it go forever with [[NSRunLoop currentRunLoop] run];

on user does something:
tell the thread to tell the webServiceObject to searchFor:@"Foo";

on thread has a response:
thread says webServiceObject says there are 19 @"Foo" up in the cloud.

I have been fiddling around with how to make it work, but realizes that I don't understand the subject well enough to come up with a "pattern" to implement it. I guess I am trying to implement a cluster of method calls on an object living in a separate continuous thread, instead of spawning a thread each time I need a particular method called (potentially 8 times in a row within 500 ms. and then not at all for 2 minutes).

I hope it makes sense and that someone a bit more experienced in threads can point me in the right direction.


This post is pretty close to what I have tried: how to use performSelect...