objective-c - NSAppleScript 和线程安全

标签 objective-c multithreading cocoa thread-safety applescript

我的应用需要在后台线程定期调用预编译的 AppleScript。因为 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 的分配是线程安全的吗?

最佳答案

这个:

Because NSAppleScript is not thread-safe I need to execute the scripts on the main thread.

回答这个:

Is allocation of NSAppleScripts thread-safe?

不,这不安全。特别是,实例的初始化可以执行许多非线程安全的操作。

关于objective-c - NSAppleScript 和线程安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4983262/

相关文章:

ios - UIPicker subview 无法识别输入

ios - 删除/编辑核心数据对象时遵循逻辑和关系

Java - 多队列生产者消费者

python - 限时终止Python进程

macos - 在 OSX 上的 Swift 主窗口中设置自定义 View

xcode - iPhoto 类似 NSButton

objective-c - 是否可以使用 presentMoviePlayerViewControllerAnimated 播放多个剪辑?

java - 提高欧拉数并行计算的性能

objective-c - NSOutlineView 更改组行颜色

iOS 导航栏 - 我想用动态值设置导航栏中的标题