开发者

Check a condition in the background every ten seconds and send notification

开发者 https://www.devze.com 2023-03-06 06:41 出处:网络
I\'m working on an app for Mac OS X. My app checks something every ten seconds, and if the condition is true, the app sends a Growl notification.

I'm working on an app for Mac OS X. My app checks something every ten seconds, and if the condition is true, the app sends a Growl notification.

I've already coded the Growl notifications and the checking. I just need to know how to make this checking repeat itself every ten seconds, and each time send a notification if true, all in the background.

Please write the exact code, as I am very new to Objective-C. Thank you :D

-------------------------------EDIT------------------------------------

Currently i'm using this:

//  MyApp_AppDelegate.m

#import "MyApp_AppDelegate.h"

@implementation MyApp_AppDelegate

- (void)awakeFromNib {
    return;
}


-(void)applicationDidFinishLaunching:(NSNotification*)aNotification {

    // grwol:
    NSBundle *myBundle = [NSBundle bundleForClass:[MyApp_AppDelegate class]];
    NSString *growlPath = [[myBundle privateFrameworksPath] stringByAppendingPathComponent:@"Growl-WithInstaller.framework"];
    NSBundle *growlBundle = [NSBundle bundleWithPath:growlPath];

    #include <unistd.h>

    int x = 0;
    int l = 10; // time/repeats
    int t = 10; //seconds
    while ( x <= l ) {

        // more code here only to determine sendgrowl value...

        if(sendgrowl) {
            if (growlBundle && [growlBundle load]) {
                // more code to sends growl
            } else {
                NSLog(@"ERROR: Could not load Growl.framework");
            }
        }

        // do other stuff that doesn't matter...

        // wait:
        sleep(t);

        x++;
    }
}

/* Dealloc method */
- (void) dealloc { 
    [super dealloc]; 
}

@en开发者_JAVA百科d


The exact code that you are looking for can be found here: Time Programming Topics


-(void) sendGrowl { } // your growl method

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self 
                  selector:@selector(sendGrowl) userInfo:nil repeats:YES]; 

When you are done with the timer call [timer invalidate]. Paste to XCode and alt+click it to read the docs.


To schedule a timer to run every 10 seconds, you need this:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 10.0
                                                  target: someObject 
                                                selector: @selector(fire:)
                                                userInfo: someParameter
                                                 repeats: YES];

That will create a timer and put it on the run loop such that it fires every 10 seconds. When it fires, it is equivalent to the following method invocation:

[someObject fire: someParameter];

You are allowed to pass nil as someParameter in which case your selector need not take a parameter i.e. it could be -fire rather than -fire:.

To stop a timer, simply send it the invalidate message.

[timer invalidate];

Timers require a run loop to work. This is fine if you run it on the main thread of your application since the main thread already has a run loop (it's what handles UI events and passes them off to your actions). If you want the timer to fire on a different thread, you have to create and run a run loop on that different thread. This is a bit more advanced, so given you are new to Objective-C avoid it for now.


Edit

Having seen what you are trying to do, the first bit of code to schedule the timer needs to replace that entire while loop. The -fire method would look something like:

-fire
{
    // code here only to determine sendgrowl value...

    if(sendgrowl) 
    {
        if (growlBundle && [growlBundle load]) 
        {
            // more code to sends growl
        }
        else 
        {
            NSLog(@"ERROR: Could not load Growl.framework");
        }
    }
    // do other stuff that doesn't matter...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消