NSAppleScript和线程安全

时间:2011-02-13 08:46:46

标签: objective-c multithreading cocoa thread-safety applescript

我的应用需要在后台线程上定期调用预编译的AppleScripts 。因为NSAppleScript不是线程安全的,所以我需要在主线程上执行脚本。我需要在执行脚本后获取返回值,因此我使用此解决方案:

- (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是一个实例变量。我的问题是,我在后台线程上分配script并在主线程上执行它。 NSAppleScripts的分配是否是线程安全的?

1 个答案:

答案 0 :(得分:3)

此:

  

因为NSAppleScript不是   线程安全我需要执行   主线程上的脚本。

回答这个问题:

  

是NSAppleScripts的分配   线程安全?

不,这不安全。特别是,实例的初始化可以执行任何非线程安全的事情。