Reputation: 109
hi guys im very frustrated because i want to improve a code but i'm not getting good results this is my piece of code
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];
i want use this values outside the NSBlockOperation like this valuetotal=value1+value2+value3+value4; please help or help with a better solution
Upvotes: 2
Views: 741
Reputation: 4089
one block operation is enough, cause it is able to add more blocks, and execute concurrently, meanwhile provide a block to trigger when all blocks are completed, like this:
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);
}];
Upvotes: 0
Reputation: 57040
Add another operation which is dependent on your other operations (using addDependency:
), and add your code there. Queue this operation. It will wait for all others to finish, and then use their output.
For example,
NSBlockOperation *blockOperationFinal = [NSBlockOperation blockOperationWithBlock: ^{
valueTotal = value1 + value2 + value3 + value4;
}];
[blockOperationFinal addDependency:blockOperation1];
[blockOperationFinal addDependency:blockOperation2];
[blockOperationFinal addDependency:blockOperation3];
[blockOperationFinal addDependency:blockOperation4];
[queue addOperation:blockOperationFinal];
Upvotes: 3