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 :) ).
Normally the code in an
NSOperation
is synchronous. TheNSOperationQueue
provides the threads needed to run the code in the background. So you add your operation to a queue and the queue then callsstart
on your operation on a background thread.AFJSONRequestOperation
is a special type ofNSOperation
calledconcurrent
, this means the operation already provides it's own background threads internally. In some circumstances you may call thestart
method of aconcurrent
operation outside of a queue. Because the operation already provides it's own background threads it would still run in the background. In this casestart
might be called directly just to minimise the code shown in the example.Normally you would still add
concurrent
operations to anNSOperationQueue
because you want to take advantage of the other things the queue provides such as managingdependancies
and themaxConcurrentOperationCount
.So just create yourself an
NSOperationQueue
and add your operation to it. You wont need to callstart
the queue will do this for you.