7

I am facing problem related to NSoperationQueue. In my code in :

-(void) viewDidLoad
{
    //Initialisation of queue and operation.
    //adding operation to queue
    [self.operationQueue addOperation:op];

}

 -(void) viewWillDisappear:(BOOL)animated 
{
   [super viewWillDisappear:animated];
   [self.operationQueue cancelAllOperations];
}

So during execution of my NSOperation main function when i am checking for isCancelled property it always returns NO. Infact after calling cancellAllOperation on NSOperationQueue. eg.

-(void)main
{
   if(self.isCancelled)
   {
    // Never executing this block :-(
   }

 }

For more details i am doing some network calls in my NSOperation.And when i switched to other view by then cancelAllOperation is called up. And when network response came back to in my NSOperation i am checking if(Self.isCancelled) and now i am in another view(means isCancelled should set YES). but this check always fails.

kidsid49
  • 1,358
  • 3
  • 18
  • 37
  • Are you sure your operation hasn't already started by the time you call cancelAllOperations? You'll need to check for the isCancelled property being set throughout your operation's execution. – Jesse Rusak May 05 '13 at 16:03
  • I am consistently checking for isCancelled property in my operation main method. I debug through breakpoints and found that after calling cancelAllOperation method when call comes back to this checkpoint if(self.isCancelled) its always returning NO. – kidsid49 May 05 '13 at 16:27
  • Are you overriding the `cancel` method on your NSOperation? If so, can you post it? If not, can you override, (call super) and put a log or breakpoint there to see if it's called? – Jesse Rusak May 05 '13 at 16:41
  • Yeah i tried to override the -(void)cancel method in subclass of NSOperation. Its never get called. But it get called if i call cancelAllOperation just after the addOpperation in viewDidLoad. – kidsid49 May 05 '13 at 19:39
  • Log the identity of the objects in all methods. Make sure you're operating on the objects you think you are. For example, is it (`self`) the same instance? Is `self.operationQueue` the same in both places? – Ken Thomases May 05 '13 at 23:04
  • you have using Any ASIHTTP tool in project. – dhaya May 06 '13 at 04:59
  • ASIHTTP tool in project - NO. – kidsid49 May 06 '13 at 05:54

2 Answers2

1

Your operation is added to the queue just after loading the view in viewDidLoad, and the queue will then assume responsibility to start the operation.

Since you cancel your operation(s) when the view disappears (viewWillDisappear), the operation should most likely be finished at this time. In other words, your operation is cancelled after being terminated. You could check the isExecuting property to know whether the operation is actively working.

Nicolas
  • 151
  • 5
  • I am consistently checking for isCancelled property in my operation main method. I debug through breakpoints and found that after calling cancelAllOperation method when call comes back to this checkpoint if(self.isCancelled) its always returning NO. Call will come at this checkpoint only when my operation is executing. – kidsid49 May 05 '13 at 16:28
1

Your operation is probably no longer running, and so won't be cancelled. (Once your operation has finished, the operation queue will no longer keep track of it, so calling cancelAllOperations won't do anything.)

If the network response you're waiting for is calling a callback rather than blocking the call to main, your operation will already have finished (when main returns). You could fix this by using a "concurrent" operation (see the NSOperation docs; you can indicate when you're done, rather than just automatically being done when main returns) or by using synchronous networking calls (so main doesn't return until you're really done).

Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
  • I get your point. When my response comes i am checking for isCancelled property if it is YES i am returning otherwise i am reloading my tabelView with fresh contents with is from response. Now what i want to do is if i am not in that view then when response came back it should check isCancelled property and should not reload my tabelView if i am not in that view currently. So can you give me a little hint like how this situation can be handled ? – kidsid49 May 06 '13 at 06:34
  • Well, you can use a synchronous networking method, like I suggested, and check for isCancelled after that (since your operation will still be executing). Alternatively, you could use a third-partly library like AFNetworking which has cancelable NSOperations for network activity already built. – Jesse Rusak May 06 '13 at 11:33