2

I am having a little trouble using addoperationwithblock in Cocoa. Let's say I have a master function

-(IBAction) callthisone {

  // Call another function "slave" here and store returned value in result

    result = return value from slave
    NSLog(@" result is %@",result);
 }];

}

-(NSArray *) slave {

 [operationQueue addOperationWithBlock: ^{   

  NSString * result = @"5" ;
  }];

 return result;
}

I can never get the result value returned in the master. How do I do this ? Is my approach correct ? Thanks

cocoacoder
  • 381
  • 8
  • 19

2 Answers2

1

You may try something like this:

-(IBAction) callthisone {
    [self slave: ^(NSString* result) {
            NSLog(@" result is %@",result);
        }
    ];
}


-(void)slave: (void(^)(NSString*)) callback {
    [operationQueue addOperationWithBlock: ^{
            NSString* str = [NSString stringWithFormat: @"5]";
            callback(str);
        }
    ];
}
Julien May
  • 2,011
  • 15
  • 18
0

Apple's documentation for addOperationWithBlock says:

Parameters

The block to execute from the operation object. The block should take no parameters and have no return value.

These are meant for self contained block operations.

Could you try something different that does have more flexibility in terms of getting stuff in and out of the queue / thread? Maybe Grand Central Dispatch (I was just looking at this thread).

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215