I am trying to understand NSOperationQueue's and am trying to create the most simple example possible. I have the following:
NSOperationQueue *myOQ=[[NSOperationQueue alloc] init];
[myOQ addOperationWithBlock:^(void){
NSLog(@"here is something for jt 2");
}];
[myOQ addOperationWithBlock:^(void){
NSLog(@"oh is this going to work 2");
}];
But would like to do this:
void (^jt)() = ^void(){
NSLog(@"here is something for jt");
};
void (^cl)() = ^void(){
NSLog(@"oh is this going to work");
};
NSOperationQueue *myOQ=[[NSOperationQueue alloc] init];
[myOQ addOperation:jt];
[myOQ addOperation:cl];
Is this latter form possible? Can I convert a block to an NSOperation?
thx in advance
You could:
NSBlockOperation *jtOperation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"here is something for jt");
}];
NSBlockOperation *clOperation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"oh is this going to work");
}];
[myOQ addOperation:jtOperation];
[myOQ addOperation:clOperation];
Having said that, I'd generally do addOperationWithBlock
unless I really needed the NSOperation
object pointers for some other reason (e.g. to establish dependencies between operations, etc.).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With