NSAppleScript 和线程安全
我的应用程序需要在后台线程定期调用预编译的 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 的分配是线程安全的吗?
My app needs to call pre-compiled AppleScripts periodically on a background 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这:
回答这个:
不,这不安全。特别是,实例的初始化可以执行许多非线程安全的操作。
This:
Answers this:
No, it isn't safe. In particular, the initialization of the instance could do any number of things that are not thread safe.