title: Placing an NSTimer in a separate thread
body: <p><strong>Note:</strong> It's probably worth scrolling down to read my edit.</p>
  
  <p>I'm trying to setup an NSTimer in a separate thread so that it continues to fire when users interact with the UI of my application. This seems to work, but Leaks reports a number of issues - and I believe I've narrowed it down to my timer code.</p>
  
  <p>Currently what's happening is that updateTimer tries to access an NSArrayController (timersController) which is bound to an NSTableView in my applications interface. From there, I grab the first selected row and alter its timeSpent column. Note: the contents of timersController is a collection of managed objects generated via Core Data.</p>
  
  <p>From reading around, I believe what I should be trying to do is execute the updateTimer function on the main thread, rather than in my timers secondary thread.</p>
  
  <p>I'm posting here in the hopes that someone with more experience can tell me if that's the only thing I'm doing wrong. Having read Apple's documentation on Threading, I've found it an overwhelmingly large subject area.</p>
  
  <pre><code>NSThread *timerThread = [[[NSThread alloc] initWithTarget:self selector:@selector(startTimerThread) object:nil] autorelease];
  [timerThread start];
  
  -(void)startTimerThread
  {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
      activeTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES] retain];
  
      [runLoop run];
      [pool release];
  }
  -(void)updateTimer:(NSTimer *)timer
  {
      NSArray *selectedTimers = [timersController selectedObjects];
      id selectedTimer = [selectedTimers objectAtIndex:0];
      NSNumber *currentTimeSpent = [selectedTimer timeSpent];
  
      [selectedTimer setValue:[NSNumber numberWithInt:[currentTimeSpent intValue]+1] forKey:@"timeSpent"];
  }
  -(void)stopTimer
  {
      [activeTimer invalidate];
      [activeTimer release];
  }
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I'm still totally lost with regards to this leak. I know I'm obviously doing something wrong, but I've stripped my application down to its bare bones and still can't seem to find it. For simplicities sake, I've uploaded my applications controller code to: <a href="http://pastebin.com/DCY6igJD" rel="noreferrer">a small pastebin</a>. Note that I've now removed the timer thread code and instead opted to run the timer in a separate runloop (as suggested here).</p>
  
  <p>If I set the Leaks Call Tree to hide both Missing Symbols and System Libraries, I'm shown the following output:</p>
  
  <p><strong>EDIT:</strong> Links to screenshots broken and therefor removed.</p>