嗨,大家好我非常沮丧,因为我想改进代码,但我没有得到好结果这是我的代码
NSBlockOperation *blockOperation1 = [NSBlockOperation blockOperationWithBlock: ^{
value1 = [self getDiferences:0.0 finx:width iniy:0.0 finy:cuartoheith image:imagen1 imagetoComapare:imagen2];
}];
[queue addOperation:blockOperation1];
NSBlockOperation *blockOperation2 = [NSBlockOperation blockOperationWithBlock: ^{
value2 = [self getDiferences:0.0 finx:width iniy:0.0 finy:cuartoheith image:imagen1 imagetoComapare:imagen2];
}];
[queue addOperation:blockOperation2];
NSBlockOperation *blockOperation3 = [NSBlockOperation blockOperationWithBlock: ^{
value3 = [self getDiferences:0.0 finx:width iniy:0.0 finy:cuartoheith image:imagen1 imagetoComapare:imagen2];
}];
[queue addOperation:blockOperation3];
NSBlockOperation *blockOperation4 = [NSBlockOperation blockOperationWithBlock: ^{
value4 = [self getDiferences:0.0 finx:width iniy:0.0 finy:cuartoheith image:imagen1 imagetoComapare:imagen2];
}];
[queue addOperation:blockOperation4];
我希望在NSBlockOperation之外使用这个值,例如:valuetotal = value1 + value2 + value3 + value4;请帮助或帮助提供更好的解决方案
答案 0 :(得分:3)
添加另一项操作,该操作取决于您的其他操作(使用addDependency:
),并在那里添加您的代码。排队此操作。它将等待所有其他人完成,然后使用他们的输出。
例如,
NSBlockOperation *blockOperationFinal = [NSBlockOperation blockOperationWithBlock: ^{
valueTotal = value1 + value2 + value3 + value4;
}];
[blockOperationFinal addDependency:blockOperation1];
[blockOperationFinal addDependency:blockOperation2];
[blockOperationFinal addDependency:blockOperation3];
[blockOperationFinal addDependency:blockOperation4];
[queue addOperation:blockOperationFinal];
答案 1 :(得分:0)
一个块操作就足够了,因为它能够添加更多的块,并且可以同时执行,同时提供一个块来在所有块完成时触发,如下所示:
NSMutableArray *array = [[NSMutableArray alloc] init];
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
[array addObject:@"op1"];
}];
[operation addExecutionBlock:^{
[array addObject:@"op2"];
}];
[[[NSOperationQueue alloc] init] addOperation:operation];
[operation setCompletionBlock:^{
NSLog(@"array:%@", array);
}];