1

I have a Cocoa interface. When I press a button I want to process some data, but I want to keep using the interface while it's working. I guess the only solution is NSThread. Now will there be a locking mechanism preventing me from returning from an IBAction method if it spawns a thread?

1
  • I followed the example from: softpixel.com/~cwright/programming/threads/threads.cocoa.php but this uses a + function which prevents me from communicating with the object. I declared some global variables and pointers used as proxies and it works but calling the coreplot functions make things slow and nothing refreshes in my animation until the thread is over. I tried using a - function but then nothing happens, it doesnt even respond to breakpoints. i think i will have a look at the suggerstions below Aug 4, 2009 at 7:21

3 Answers 3

3

No, there is no locking mechanism. The new thread will start and the current thread will continue. You may want to look at performSelectorInBackground:withObject: and possibly NSOperation in addition to NSThread.

0
3

Take a look at NSOperation. NSOperation is one of the few cocoa classes which must be subclassed for it to be useful. By adding a delegate property to your NSOperation subclass you can get notified when the operation completes. Also, you can add a userInfo property to allow the operation to pass back arbitary data to the delegate

@implementation MyNSOperationSubclass

-(void)main
{
    //do operation here



    //operationResult is used to report back to the delegate. operationResult could include a userInfo key so that the delegate can have some data passed back, or an error key to indicate success of the operation.
    NSDictionary *operationResult; 


    //Some checks to ensure that the delegate implements operationHasFinished: should be added.
    //waitUntilDone: YES locks the main thread
    [[self delegate] performSelectorOnMainThread:@selector(operationHasFinished:)     withObject:operationResult waitUntilDone: YES];

}

@end
2
  • will the delegate be able to send messages while it is still processing so I can animate my interface as the data is being processed by the thread? Aug 4, 2009 at 19:24
  • Yes. NSOperation encapsulates threading. Therefore the processing your NSOperation subclass performs will not interfere with your main thread (or any other thread for that matter). The only time the NSOperation impacts on the main thread is when it calls performSelectorOnMainThread:, but that is by design and is desired behaviour. Aug 5, 2009 at 8:27
0

Don't know much about cocoa, but starting a new thread for processing something in background while keeping the UI thread free for taking user inputs(and thus preventing UI from freezing) is the most widely used technique for the problem you have mentioned.Both threads will work together (concurrently) and the programmer has to take care of the synchronization issues if any.You should go ahead with the technique without doubt.

Thanks, Sourabh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.