Re: Let the runloop process its queue during a long operation
Re: Let the runloop process its queue during a long operation
- Subject: Re: Let the runloop process its queue during a long operation
- From: eveningnick eveningnick <email@hidden>
- Date: Thu, 7 Oct 2010 11:48:41 +0400
Actually, by "processing" i mean a lot of AppleScript calls, some of
which return NSAppleEventDescriptors.
I am not sure how it is happening under the hood (if someone could
explain me...), but what i see is: i call
NSAppleEventDescriptor *resultFromScript = [myapplescript
executeAndReturnError];
and then this call returns _only after_ the result is available (i.e.,
the "return" operator in applescript has been called).
Does my application register some "event listeners", that wait for
AppleScripting server application (it is Microsoft Word) to post this
event?
Does that mean i will need the run loop? Maybe it's better and easier
to create the thread manually instead of using NSOperation?
Thank you
> It will be much easier to use a custom subclass of NSOperation for this kind of problem.
> The operation's -main method should perform the transformation in a peace-wise manner and thereby repeatedly checking its cancelation state (-isCancelled method), like:
>
> - (void) main {
> // runs on a secondary thread
> NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
> ...
> while (![self isCancelled] && !done) {
> // transform a peace of data
> ...
> }
>
> [delegate fileTransformOperationDidFinish:self];
>
> [pool release];
> }
>
> You add the operation to a NSOperationQueue instance which schedules its operations onto a secondary thread. From your main thread you may then cancel the operation by sending it the -cancel message.
>
> There are several ways to notify the application (or some object) when the task is finished. Using a delegate is safe and easy. You may consider to define a protocol for the delegate. The delegate method may also schedule its actual work to the main thread (via -performSelectorOnMainThread:withObject:waitUntilDone:) if this is necessary.
>
> Just be careful when your task requires itself a runloop (e.g. using asynchronous NSURLConnection) - since there exists no (implicit) one when invoking an NSOperation's -main method on a secondary thread. Properly implementing this will require more elaborated code, though.
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden