Re: NSTimer and a problem with document-based apps
Re: NSTimer and a problem with document-based apps
- Subject: Re: NSTimer and a problem with document-based apps
- From: "Clark Cox" <email@hidden>
- Date: Sat, 26 Jul 2008 16:12:02 -0700
On Sat, Jul 26, 2008 at 3:34 PM, Sumner Trammell
<email@hidden> 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@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden