views:

154

answers:

2

Hi, I know I can create a separate thread in cocoa touch with the following code:

       [NSThread detachNewThreadSelector:@selector(getEarthquakeData) toTarget:self withObject:nil];

How to go back to the process of my primary thread? I know I can do this from the delegate with this code:

    [(id)[[UIApplication sharedApplication] delegate] performSelectorOnMainThread:@selector(addToEarthquakeList:) withObject:self.currentEarthquakeObject waitUntilDone:YES];

How would I do the above if the main thread was running in a controller and not in the delegate like above.

Thanks!

A: 

You have the right calls. When you call performSelectorOnMainThread:... your request gets queued on the main thread's run loop, - so if the main thread is executing somewhere else at the time that processing will finished first. Once your main thread code returns back to the run loop the request from the background thread can be looked at and invoked.

I don't know what determines the priority if there are other events queued for the run loop to process - but in general I don't think you can rely on it even being the next thing executed. This is another reason you should try and keep your main thread sequential processing to a minimum (ie, try not to have long running sequences on the main thread - but that's probably why you're using a background thread in the first place).

Phil Nash
A: 

Can't you just do:

[controller performSelectorOnMainThread:@selector(addToEarthquakeList:) withObject:self.currentEarthquakeObject waitUntilDone:YES];

...or did I misinterpret your question? (I thought you were asking how to run a controller method on the main thread.)

Daniel Dickison
How do I get that controller? If instantiate a new controller I get the warning with your above code:warning receiver is a forward class and corresponding @interface may not existso should i instantiate? or how do i get this controller to performSelectorOnMainThread?
Buffernet
what object do you want to call a method of? You'll need to have a reference to that object available to your background thread - either by passing it in, or by having it as a (lockable, or atomic) property or somesuch of something else you have access to
Phil Nash
@Joe, I thought maybe 'self' was the controller object you were interested in.
Daniel Dickison