1

I have some image processing that take much time and resources, so i use NSOperation + NSOperatioQueue + delegate for callBack. and all work.

now i want to use blocks because its much elegant and simple to use in a tableView for example.

what i need to do is just like AFJSONRequestOperation for example:

NSURL *url = [NSURL URLWithString:@"url"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"App.net Global Stream: %@", JSON);
 } failure:nil];
[operation start];

in this example i don't see any operationQueue ! how can i do the same?

    [ImageManagerOperation modifyImage:(UIImage*)image completitionBlock:(void (^)(UIImage *modifiedImage))complete];

where ImageManagerOperation is an NSOperation.

I know that i can set a Completion Block, but i still need to add the operation in a queue.

i want to minimize the line number in my code (if possible :) ).

Red Mak
  • 1,176
  • 2
  • 25
  • 56

1 Answers1

1

Normally the code in an NSOperation is synchronous. The NSOperationQueue provides the threads needed to run the code in the background. So you add your operation to a queue and the queue then calls start on your operation on a background thread.

AFJSONRequestOperation is a special type of NSOperation called concurrent, this means the operation already provides it's own background threads internally. In some circumstances you may call the start method of a concurrent operation outside of a queue. Because the operation already provides it's own background threads it would still run in the background. In this case start might be called directly just to minimise the code shown in the example.

Normally you would still add concurrent operations to an NSOperationQueue because you want to take advantage of the other things the queue provides such as managing dependancies and the maxConcurrentOperationCount.

So just create yourself an NSOperationQueue and add your operation to it. You wont need to call start the queue will do this for you.

Rory O'Bryan
  • 1,894
  • 14
  • 22
  • thank you, so in the end i have to create my own queue, i this for a tableView this will be much effort, so i do that with GCD and that work fine, and i'm really – Red Mak Mar 16 '13 at 23:37
  • thank you, so in the end i have to create my own queue, to adapt this for a tableView will be much effort, so i do that with GCD and that work fine (i think GCD use operation right ?). you answer my question so i accept it thank you :) – Red Mak Mar 16 '13 at 23:52
  • No, NSOperation uses GCD not the other way round. It sounds like you need a queue so you can limit the amount of work you do from your table view. Im assuming you are doing work for each cell while scrolling? If some one scrolls a lot you may start too much work. Using a queue helps limit this and also let's you cancel tasks that may have been started for a cell but are no longer needed because the user has scrolled away. There is a WWDC (I think 2012) session that discusses this in depth. You should look for this video if this is what you are doing too. – Rory O'Bryan Mar 19 '13 at 13:44
  • yes this is exactly what i'm doing, i call that 'GCD' block in the 'willdisplaycell' method,but i think you are right, i have to optimize my cause because the app will be a messaging application, so allot of cells can be displayed thank you, i'll use a client account to access this video session :) thank you. – Red Mak Mar 19 '13 at 13:49