title: how to stop performing selector in background?
body: <p>I have some class A. In this class i have a method,
  which calls [<code>self performSelectorInBackground:...]</code>. And it starts downloading
  some info from internet.</p>
  
  <p>After i tap Home button, then enter the app again, this background method keeps working.
  So, if i call this method again, i have bad_access, because background method is already working and i call it twice.</p>
  
  <p>Can i stop performing selector in background of the class A? For example in my applicationDidEnterBackground?
  Or can i check, if selector is performing or something?</p>
  
  <p>I found couple things like</p>
  
  <pre><code>[[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget:a];
  [NSObject cancelPreviousPerformRequestsWithTarget:a selector:@selector(startDownload) object:nil];
  </code></pre>
  
  <p>But they didn't work for me.
  So</p>
  
  <p>my objAppDelegate:</p>
  
  <pre><code>@inteface ObjAppDelegate
  {
       A *a;
  }
  @implementation ObjAppDelegate
  {
      -(void)applicationDidEnterBackground:(UIApplication *)application
      {
          //or it can be didBecomeActive..
          //here. check if background task of class A is running, or just stop it ??
      }
  }
  
  @implementation A
  {
       //some timer, or event, etc.
       -(void)startDownload
       {
            [self performSelectorInBackground:@selector(runBackgroundTask)           withObject:nil];
        }
      -(void)runBackgroundTask
      {
           //some network stuff..
       }
  }
  </code></pre>
  
  <p>i did it like this:</p>
  
  <p><code><pre>
  threadForDownload = [[NSThread alloc] initWithTarget:self selector:@selector(threadMain:) object:nil];
              [threadForDownload start];
              [self performSelector:@selector(startDownload) onThread:threadForDownload withObject:nil waitUntilDone:NO];</p>
  
  <ul>
  <li><p>(void)threadMain:(id)data {
  NSAutoreleasePool *pool = [NSAutoreleasePool new];</p>
  
  <p>NSRunLoop *runloop = [NSRunLoop currentRunLoop];
  [runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];</p>
  
  <p>while (YES) { 
      [runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  }</p>
  
  <p>[pool release];
  }</p></li>
  </ul>
  
  <p></code></pre></p>
  
  <p>In my startDownload method i look at activity indicator to check, whether
  startDownload is already running..</p>
  
  <p><code><pre>
  -(void)startDownload
  {
         if (![[UIApplication sharedApplication] isNetworkActivityIndicatorVisible])  // flag..
      {
              //....
           }
  }
  </code></pre></p>
  
  <p>// I make visible networkActivityIndicator every time i start downloading</p>