gpt4 book ai didi

objective-c - NSAppleScript 和线程安全

转载 作者:行者123 更新时间:2023-12-03 16:59:40 27 4
gpt4 key购买 nike

我的应用需要在后台线程定期调用预编译的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com