Sandeep
Sandeep

Reputation: 21154

NSOperation and NSOperationQueue does not work

I have the following code and it does not work. Is there something working behind it.

[operationQueue addOperationWithBlock:^{
        imageData =  [NSData dataWithContentsOfURL:imageURL];
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            UIImage *image = nil;

            if(imageData){
                UIImage *image = [UIImage imageWithData:imageData];
                cell.imageView.image = image;
            }
        }];
    }];

Even I create a subclass of NSOperation and then alloc init it, it does not work the way I think it to. I always have to invoke start to the NSOperation subclass to run but I suppose sending start message to NSOperation runs it in the main thread rather than running in the background thread.

Upvotes: 0

Views: 548

Answers (1)

subhash kumar singh
subhash kumar singh

Reputation: 2756

I want to add an alternative solution using GCD :

backgroundQueue = dispatch_queue_create("com.razeware.imagegrabber.bgqueue", NULL);
dispatch_async(backgroundQueue, ^{
                          /* put the codes which makes UI unresponsive like reading from network*/ 
                         imageData =  [NSData dataWithContentsOfURL:imageURL];
                         .....   ;
                         dispatch_async(dispatch_get_main_queue(),^{
                                         /* do the UI related work on main thread */
                                         UIImage *image = [UIImage imageWithData:imageData];
                                         cell.imageView.image = image;
                                         ......;   });
                                  });
dispatch_release(backgroundQueue);

Let me know whether this one helped you ;)

Reference

Upvotes: 1

Related Questions