gpt4 book ai didi

objective-c - 使用 GCD 的慢循环

转载 作者:行者123 更新时间:2023-12-03 17:50:49 25 4
gpt4 key购买 nike

我必须运行许多包,但也运行一些方法(70 个方法 + 40 个包),并且我正在根据用户在应用程序界面中选择的内容构建 _myArray。这是因为我无法知道用户在按下“开始”按钮之前会选择什么,并且应用程序要求所有内容都遵循预定的顺序(_myArray 已排序)。该应用程序从 10.7 开始支持 OSX

-(void)package01 {
}

-(void)package02 {
}

.....

-(void)package40 {

// also tried with NSTask, but is slow exactly as NSAppleScript (so guess that this is not the problem)

NSString* package = [_pathToPkg stringByAppendingPathComponent:@"40.pkg"];
NSAppleScript* runpkg;
NSString *command = [NSString stringWithFormat:@"/usr/sbin/installer -pkg %@ -target %@", package, _targetOBJ];

runpkg = [[NSAppleScript alloc] initWithSource:[NSString stringWithFormat:
@"do shell script \"%@\"", command]];

[runpkg executeAndReturnError: nil];
}

-(void)oneOfMyMethod {
dispatch_async(_myQueue,
^{
// some operation
dispatch_async(_mymainQueue, ^{/*updating interface here*/});
});

dispatch_async(_myQueue,
^{
// more operation
dispatch_async(_mymainQueue, ^{/*updating interface here*/});
});

dispatch_async(_myQueue,
^{
// ++ operation
dispatch_async(_mymainQueue, ^{/*updating interface here*/});
});


// Here I'm calling the problematic Method:
[self runPackageOrMethod:^(BOOL finished) {
if(finished){
[self doOther];
}
}];

}

// this is pratically the core of the app...
- (void) runPackageOrMethod:(completion) compblock{

_pathToPkg = @"path/to/the/folder/that/contain/packages";


dispatch_group_t group = dispatch_group_create();

@try {
for (NSString *method in _myArray) {
NSArray *descriptions;
descriptions = [NSArray arrayWithArray:[METHOD_DESCRIPTION allKeys]]; // Another array that I use to display some info about running pkg or method

dispatch_group_async(group,_myQueue, ^ {
dispatch_async(_mymainQueue, ^{
if ([descriptions containsObject:method] && ![[METHOD_DESCRIPTION objectForKey:method] isEqualToString:@"MUTE"]) {
[_displayStore appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"\n- %@\n", [METHOD_DESCRIPTION objectForKey:method]] attributes:_dispAttr]];
[_display scrollToEndOfDocument:self];
}
});
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
dispatch_async(_mymainQueue, ^{[self performSelector:NSSelectorFromString(method) withObject:nil];});
#pragma clang diagnostic pop
});
}
}
@catch (NSException *exception) {
NSLog(@"ERROR: %@", [exception description]);
}
@finally {
compblock(YES);
}
}

运行包(总是超过 30 个)时代码特别慢..

关于如何解决的任何想法?

最佳答案

我不确定这是否有帮助,但位于/usr/sbin/installer 的 Apple 安装程序二进制文件一次不会安装多个软件包。因此,即使您将所有内容都分派(dispatch)到队列上,被调用的底层工具也会将其序列化并一次安装一个包。

因此,如果您有 30 个软件包(或 .pkg),安装程序工具将花费大量时间对其进行解档和安装。

我认为如果不编写自己版本的“安装程序”工具来执行您想要的操作,您就无法解决这个问题。

关于objective-c - 使用 GCD 的慢循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27534566/

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