Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 每10秒在后台检查一次情况并发送通知_Objective C_Cocoa_Macos_Background - Fatal编程技术网

Objective c 每10秒在后台检查一次情况并发送通知

Objective c 每10秒在后台检查一次情况并发送通知,objective-c,cocoa,macos,background,Objective C,Cocoa,Macos,Background,我正在为Mac OS X开发一个应用程序。我的应用程序每十秒钟检查一次,如果情况属实,应用程序会发送咆哮通知 我已经对咆哮通知和检查进行了编码。我只需要知道如何让这个检查每十秒钟重复一次,如果是真的,每次都会在后台发送一个通知 请写出准确的代码,因为我对Objective-C非常陌生。谢谢你:D -------------------------------编辑------------------------------------ 目前我使用的是: // MyApp_AppDelegate.

我正在为Mac OS X开发一个应用程序。我的应用程序每十秒钟检查一次,如果情况属实,应用程序会发送咆哮通知

我已经对咆哮通知和检查进行了编码。我只需要知道如何让这个检查每十秒钟重复一次,如果是真的,每次都会在后台发送一个通知

请写出准确的代码,因为我对Objective-C非常陌生。谢谢你:D

-------------------------------编辑------------------------------------

目前我使用的是:

//  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]; 
}

@end
//MyApp\u AppDelegate.m
#导入“MyApp_AppDelegate.h”
@实现MyApp_AppDelegate
-(无效)从NIB中唤醒{
返回;
}
-(无效)ApplicationIDFinishLaunching:(NSNotification*)通知{
//grwol:
NSBundle*myBundle=[NSBundle bundleForClass:[MyApp_AppDelegate class]];
NSString*growlPath=[[myBundle PrivateFrameworkPath]stringByAppendingPathComponent:@“GrowlWithInstaller.framework”];
NSBundle*growlBundle=[NSBundle bundleWithPath:growlPath];
#包括
int x=0;
int l=10;//时间/重复
int t=10;//秒

而(x您要查找的确切代码可以在此处找到:

您要查找的确切代码可在此处找到:

完成计时器调用后,
[timer invalidate]
。粘贴到XCode并按住alt键并单击它以读取文档


完成计时器调用后,
[timer invalidate]
。粘贴到XCode并按住alt键并单击它以读取文档。

要计划计时器每10秒运行一次,您需要:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 10.0
                                                  target: someObject 
                                                selector: @selector(fire:)
                                                userInfo: someParameter
                                                 repeats: YES];
这将创建一个计时器,并将其置于运行循环中,以便每10秒触发一次。当它触发时,相当于以下方法调用:

[someObject fire: someParameter];
您可以将nil作为
某个参数传递
,在这种情况下,您的选择器不需要获取参数,即它可以是
-fire
,而不是
-fire:

要停止计时器,只需向其发送
invalidate
消息

[timer invalidate];
计时器需要一个运行循环才能工作。如果在应用程序的主线程上运行它,这很好,因为主线程已经有了一个运行循环(它是处理UI事件并将它们传递给操作的工具)。如果您希望计时器在不同的线程上启动,则必须在该不同的线程上创建并运行运行循环。这有点高级,因此如果您不熟悉Objective-C,请暂时避免使用它


编辑

看到您正在尝试执行的操作后,计划计时器的第一段代码需要替换整个while循环。
-fire
方法如下所示:

-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...
}

要计划计时器每10秒运行一次,您需要:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 10.0
                                                  target: someObject 
                                                selector: @selector(fire:)
                                                userInfo: someParameter
                                                 repeats: YES];
这将创建一个计时器,并将其置于运行循环中,以便每10秒触发一次。当它触发时,相当于以下方法调用:

[someObject fire: someParameter];
您可以将nil作为
某个参数传递
,在这种情况下,您的选择器不需要获取参数,即它可以是
-fire
,而不是
-fire:

要停止计时器,只需向其发送
invalidate
消息

[timer invalidate];
计时器需要一个运行循环才能工作。如果在应用程序的主线程上运行它,这很好,因为主线程已经有了一个运行循环(它是处理UI事件并将它们传递给操作的工具)。如果您希望计时器在不同的线程上启动,则必须在该不同的线程上创建并运行运行循环。这有点高级,因此如果您不熟悉Objective-C,请暂时避免使用它


编辑

看到您正在尝试执行的操作后,计划计时器的第一段代码需要替换整个while循环。
-fire
方法如下所示:

-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...
}

谢谢,但正如我所说,我是obj-c的新手,那里有一半以上的代码我都不懂,你能不能只给我我需要的…标题为“定时”的部分这就是你需要阅读才能做的事情。我知道阅读文档可能会让人困惑,但一旦你学会浏览文档,就更容易找到问题的答案。然后你需要向我们展示你的尝试。否则我们可能帮不上忙。@sosborn:用我正在使用的内容编辑了问题……我想要代码在每5秒重复一次的时间内(t)…Jano和JeremyP发布了您需要的内容,请接受其中一个作为答案。如果您返回并查看我建议阅读的部分,您将看到除了方法名称之外,其他代码看起来完全相同。使用他们发布的代码,然后返回并再次阅读文档的该部分。现在您已经e有机会在你的应用程序中使用它。谢谢,但正如我说的,我是obj-c的新手,那里有一半以上的代码我都不懂,你能不能只给我我需要的…标题为“定时”的部分这就是你需要阅读才能做的事情。我知道阅读文档可能会让人困惑,但一旦你学会浏览文档,就更容易找到问题的答案。然后你需要向我们展示你的尝试。否则我们可能帮不上忙。@sosborn:用我正在使用的内容编辑了问题……我想要代码在每5秒重复一次的时间内(t)…Jano和JeremyP发布了您需要的内容,请接受其中一个作为答案。如果您返回并查看我建议阅读的部分,您将看到除了方法名称之外,其他代码看起来完全相同。使用他们发布的代码,然后返回并再次阅读文档的该部分。现在您已经e有机会在你的应用程序中使用它。不要在应用程序主线程上运行的方法中使用
sleep()
。整个UI将在该时间段内冻结。概括:永远不要使用
sleep()
。另外