嗨,大家好,我很沮丧,因为我想改进代码,但结果却不好,这是我的一段代码

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;请帮助或提供更好的解决方案

最佳答案

添加另一个依赖于您其他操作的操作(使用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];

关于ios - 从NSBlockOperation获得值(value)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20712829/

10-14 20:51