On Sat, Jul 26, 2008 at 3:34 PM, Sumner Trammell
<[EMAIL PROTECTED]> wrote:
> Hi. I'm writing a Cocoa document-based app (using the Xcode template) that
> uses a WebView and needs an NSTimer to trigger one of the methods every 10
> seconds in the MyDocument class.

It sounds like you want a single timer shared by all instances of your
class, so you should do your work setting it up (and tearing it down)
in class methods. Something like (warning, composed in mail):

(Note, checkVPN: is now a class method, not an instance method)

@interface MyDocument : NSDocument
{
   IBOutlet WebView *webView;
}

...

@implementation MyDocument

static NSTimer    *VPNTimer;
static NSInteger  VPNTimerCount;

+(void)startVPNTimer {
  @synchronize(self) {

    VPNTimerCount++;

    if(VPNTimer == nil) {
      VPNTimer  = [[NSTimer scheduledTimerWithTimeInterval: 10.0
                                                    target: self
                                                  selector: @selector(checkVPN:)
                                                  userInfo: nil
                                                   repeats: YES] retain];

    }
  }
}

+(void)relinquishTimer {
  @synchronize(self) {
    if(VPNTimerCount <= 1) {
      [VPNTimer invalidate];
      [VPNTimer release];
      VPNTimer      = nil;
      VPNTimerCount = 0;
    } else {
      VPNTimerCount--;
    }
  }
}

+(void)checkVPN:(NSTimer*)timer {
  //Do your work here
}

- (id)init {
  self = [super init];
  if (self) {
    [[self class] startVPNTimer];
  }
  return self;
}

- (void)close {
  [[self class] relinquishTimer];
  //Your normal close machinery goes here
}

-- 
Clark S. Cox III
[EMAIL PROTECTED]
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to