Multiple HTTP Requests with CoreData processing from NSOperation
===

Here is what I want to accomplish - run independent JSON http requests and process them in the background. Every response is parsed and saved to database using CoreData. 
I tried deifferent approaches to this problem ended up with custom NSOperation.
The idea is - let's implement whole process in custom NSOperation that runs all independent requests, process them and notifies caller after is finished.
So, I would like to do something like this:


    NSOperationQueue *downloadQueue = [[NSOperationQueue alloc] init];
  
    TestOperation *operation = [[TestOperation alloc]init];
    operation.completionBlock = ^{
        
        NSLog(@"NSOperation Completion Block");

    };
    
    [downloadQueue addOperation:operation];

I tried with non-concurrent NSOperation and concurrent NSOperation and my test (not working solution) right now is :

    @implementation TestOperation
    
    -(id)init
    {
        self = [super init];
        
        if (self == nil)
            return nil;
        
        self.done = NO;
        
        return self;
    }
    
    - (void)start
    {    
        NSArray *urls = @[@"http://www.thetimes.co.uk", @"http://www.thetimes.co.uk", @"http://www.bbc.co.uk"];
        
        self.context = [[NSManagedObjectContext alloc] init];
        // Here : set shared persistentStoreCoordinator and other CoreData stuff
        
        __block NSInteger counter = [urls count];

        for (NSString *url in urls) {
            
            [self httpRequestWithURL:url callback:^(NSData *data, NSError *error) {
    
                if (error) {
                    NSLog(@"Error for url: %@, %@", url, error.localizedDescription);
                    return;
                }
                
                // Do something
                // 1. JSON parsing
                // 2. CoreData stuff using self.context 
                
                NSLog(@"Received data for: %@", url);
                
                if (--counter <= 0) {
                    [self finish];
                }
            }];
        }
    }
    
    - (void)httpRequestWithURL: (NSString *) url callback: (void (^)(NSData *data, NSError *error))callback
    {
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
        NSOperationQueue *currentQueue = [NSOperationQueue currentQueue];
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];    
  
        [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
            
                [currentQueue addOperationWithBlock: ^{
                    callback(data, error);
                }];
        }];
    }
    
    -(BOOL)isExecuting
    {
        return !self.done;
    }
    
    -(BOOL)isFinished
    {
        return self.done;
    }
    
    -(BOOL)isConcurrent
    {
        return YES;
    }
    
    - (void)finish
    {
        
        [self willChangeValueForKey:@"isExecuting"];
        [self willChangeValueForKey:@"isFinished"];
        
        self.done = YES;
        
        [self didChangeValueForKey:@"isExecuting"];
        [self didChangeValueForKey:@"isFinished"];
        
        NSLog(@"Finished all requests");
    }
    
    @end

Before that I tried non-concurrent NSOperation - I implemented main instead of start and didn't touch isExecuting, isFinished, isConcurrent. The problem was that NSOperation finished and called completionBlock immediately, not after whole procccessing.

So, here are biggest problems with above implementation:

1. I do not now why but callback "callback(data, error);" inside sendAsynchronousRequest: completionHandler is never executed. I tried run sendAsynchronousRequest: in the same queue as NSOperation with no luck. Why? The more general question of this point is - How can I make NSOperation finish after ALL http requests?

2. completionHandler of sendAsynchronousRequest runs on different Thread than TestOperation, even if I run it on the same NSOperationQueue. So - is it going to be a huge problem with CoreData? When I return with callback to TestOperation i will use self.context which was created on different thread - disaster?

Assumptions:

1. I need an explanation not solution using any existing libs.
2. I know I can use Notification after NSManagedObjectContext saved but I am interested in solution using simple NSOperation completionHandler.
3. I know I could use NSURLConnection synchronousRequest but I don't want to - because I want to be able to cancel TestOperation and because synchronous requests are bad. 