I was going to build an app in Objective-C, but I need to have some specific ASOC components. And some of these actions are going to take a long time, like 3-20 seconds.
Will Objective-C “wait” for replies to come back from a script?
Like if I say
NSString *aResult = [asocScripts doSomeLongEvent:parameter];
will it wait for aResult to be something? Or will it assume nil/null or failed function after X seconds?
I don’t want to start down this road if it would be better off in a straight script because of the application’s delays (Adobe illustrator functions to be specific).
Unlike single threaded AppleScript the Objective-C runtime works quite completely different, because you’re be able to perform tasks asynchronously on multiple threads.
The big advantage is: there is no need for those creepy AppleScript wait loops which are very expensive.
The Objective-C way is to use notifications or protocols / delegates to inform the main task about the several states of the child task, those protocol methods appear often like
- (BOOL)shouldDoSomething:(id)sender // the boolean value indicates to allow/deny the task
- (void)willDoSomething:(id)sender // will be called before starting the task
- (void)didSomething;(id)sender // will be called after finishing the task
notifications work similarly, in an ASOC environment I guess notifications are preferable
Yes… it waits for the method doSomeLongEvents to be finished like any other Objective-C method will wait for a method to be finished before continuing. Doesn’t have anything to do with AppleScript directly it is simply following the rules of Objective-C.
As Stefan says, you can use separate threads, but a lot of AS stuff needs to run on the main thread, as does your UI. There’s no risk of timeout, so much as frozen UI. I usually just sprinkle the AS code with fordEvent() from Myriad Helpers, and it works fine. Some of the Illustrator examples I call take a couple of minutes to run.