1

I have created NSOperation class in that class i am calling NSURLConnection to fetch some data. I am calling NSURLConnection using main thread inside NSOperation class. NSURLConnection's delegate is set to NSOperation class object. Call from NSURLConnection comes on main thread. I need to process this data using the same operation thread. how do i achieve this ??

@implementation  ModelCreationSearchOperation {
    int try;
}

- (BOOL)isConcurrent
{
    return YES;
}

- (void)start
{
    [self willChangeValueForKey:@"isExecuting"];
    _isExecuting = YES;
    [self didChangeValueForKey:@"isExecuting"];

    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.isCancelled) {
            [self finish];
            return;
        }
    });

    [self fetchData];
}

-(void)fetchData {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    });
}

- (void)finish
{
    [self willChangeValueForKey:@"isExecuting"];
    [self willChangeValueForKey:@"isFinished"];

    _isExecuting = NO;
    _isFinished = YES;

    [self didChangeValueForKey:@"isExecuting"];
    [self didChangeValueForKey:@"isFinished"];

    [self cancel];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    //Main thread
    //Want to perform parsing of response data on operation thread ....
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
Satyam Raikar
  • 465
  • 6
  • 20
  • By the way, unrelated to your question at hand, the `isCancelled` logic is not quite right. You should check that before you set `isExecuting` and call `fetchData`. There's no point in dispatching that to the main queue, either (and it's counter-productive, because your `return` should prevent the fetch from occurring, but right now you're just returning from the dispatch, but the `isExecuting` and `fetchData` are occurring whether the op was canceled or not, which is not what you intended, I'm sure). Also, starting iOS 7, you should implement `isAsynchronous`, too. – Rob Jan 09 '15 at 16:20

1 Answers1

1

You say that you "want to perform parsing of response data on operation thread." Do you really need to run it on the operation thread, or do you just need to get it off the main thread? The operation queue doesn't necessarily have a single, dedicated thread, so the question doesn't quite make sense. (It's one of the beauties of dispatch queues and operation queues, that it manages the threads for us and we generally don't have to get involved in those details.)

If you simply want the code in connectionDidFinishLoading to run on a background thread (if, for example, you're doing something exceptionally slow in this delegate method), just dispatch it to a background thread (you could use a global queue for that). If you want a serial queue for these connectionDidFinishLoading calls, create your own serial queue for that and dispatch this code to that queue. But if it's not too computationally intensive (e.g. parsing JSON or something like that), you can often just let it run on the main thread without incident.

As an aside, you can, if you really want, create a dedicated thread for your NSURLConnection delegate calls, and schedule the connection on that thread, but it's generally overkill. But see AFNetworking code for example of this implementation. This is illustrated in How do I start an Asychronous NSURLConnection inside an NSOperation?

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Is it required to call nsurlconnection using nsoperation ?? Nsurl connection itself runs on background thread ..WHat might be the actual use??.. It would be great if you could provide more details on how to create speacial thread for urlconnection delegate ..thanks... – Satyam Raikar Jan 09 '15 at 13:46
  • "Is it required to call nsurlconnection using nsoperation?" No, of course not. You should do that if you want to enjoy some of the benefits of `NSOperation` (e.g. nice contained objects for each request, constraining degree of concurrency on queue, dependencies, etc.), but if you don't need that, don't use `NSOperation` and `NSOperationQueue`. You're the one who said that you wanted to wrap it in an `NSOperation`, so I assume you had a reason. (Personally, I always do, but many people do not.) – Rob Jan 09 '15 at 14:48
  • Regarding creating dedicated thread for `NSURLConnection`, you can do that, but _why?_ As you point out, it already runs asynchronously regardless of which thread you start it from, so if you're good about not blocking the main thread (and you should do that anyway), it's a non-issue. But if you _really_ need to do so, you can refer to http://stackoverflow.com/a/17427268/1271826. But personally, I just use the main queue nowadays. – Rob Jan 09 '15 at 15:06