NSAppleScript and thread safety
My app needs to call pre-compiled AppleScripts periodically on a backg开发者_开发技巧round thread. Because NSAppleScript is not thread-safe I need to execute the scripts on the main thread. I need to get the return value after executing the script so I am using this solution:
- (void) executeAppleScript:(NSMutableDictionary*) myDict
{
NSString* returnValue = [[script executeAndReturnError:nil] stringValue];
[myDict setValue:returnValue forKey:@"myKey"];
}
NSMutableDictionary* myDict = [NSMutableDictionary dictionary];
script = [[NSAppleScript alloc] initWithContentsOfURL:scriptURL error:nil];
[self performSelectorOnMainThread:@selector(executeAppleScript:) withObject:myDict waitUntilDone:YES];
script
is an instance variable. My question is, I am allocating script
on the background thread and executing it on the main thread. Is allocation of NSAppleScripts thread-safe?
This:
Because NSAppleScript is not thread-safe I need to execute the scripts on the main thread.
Answers this:
Is allocation of NSAppleScripts thread-safe?
No, it isn't safe. In particular, the initialization of the instance could do any number of things that are not thread safe.
精彩评论