重复调用函数

发布于 2024-12-04 14:15:47 字数 1009 浏览 2 评论 0原文

我正在创建一个函数,该函数将每 5 分钟调用一次,直到该项目从后台删除。

我已经实现了 NSTimer ,但它会产生像崩溃这样的问题,并且有些难以管理,

有没有办法在每 5 分钟后重复调用一个函数而不使用计时器?

我发现的一种方法是使用警报服务,但我不知道如何实现。

编辑:示例之一

if([[NSUserDefaults standardUserDefaults] boolForKey:@"IsUpdateConfigON"] == TRUE){

        //NSLog(@"*****************   One Timer to call UpdateConfig");

    NSString *updatedDate = [[NSUserDefaults standardUserDefaults] valueForKey:@"IsUpdateConfigLastUpdateTime"];    
    int time = [Global getTimeDiff:updatedDate];

        //NSLog(@"********************************** Timer value UPDATE CONGIF  %d",time);


    if(time >= 10){     

            //NSLog(@"****************** When time : %d >=  %d  :Config_Interval seconds ",time,Config_Interval);
        cls_ConfigurationJSON *objConfig = [[cls_ConfigurationJSON alloc]init];
        [objConfig loadView];
        [objConfig release];
    }
}

其中 time 是 time 、 1 、 2 、 3 等的倒计时。每 5 分钟后调用此函数。

因此,如果有人有任何建议或代码,请帮助我。

I am creating a function which is going to be called after every 5 mins , untill the project is deleted from the background.

I have implemented the NSTimer , but it creates issue Like crash and being some what difficult to manage,

Is there any way to call a function repeatedly without using timer after every 5 mins ?

One approach I found is to use Alarm services but I don't know the way to implement.

Edit: Sample one

if([[NSUserDefaults standardUserDefaults] boolForKey:@"IsUpdateConfigON"] == TRUE){

        //NSLog(@"*****************   One Timer to call UpdateConfig");

    NSString *updatedDate = [[NSUserDefaults standardUserDefaults] valueForKey:@"IsUpdateConfigLastUpdateTime"];    
    int time = [Global getTimeDiff:updatedDate];

        //NSLog(@"********************************** Timer value UPDATE CONGIF  %d",time);


    if(time >= 10){     

            //NSLog(@"****************** When time : %d >=  %d  :Config_Interval seconds ",time,Config_Interval);
        cls_ConfigurationJSON *objConfig = [[cls_ConfigurationJSON alloc]init];
        [objConfig loadView];
        [objConfig release];
    }
}

Where time is the count down of time , 1 , 2, 3 etc. This function is called after every 5 minites.

So if any one has any suggestion or code please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱的那么颓废 2024-12-11 14:15:47

NSTimer 是频繁调用方法的首选方式。发生的崩溃可能是由于与对象释放或计时器失效等相关的问题造成的。

NSTimer is the preferred way to call methods frequently. The crash that occurs might be due to issues related to release of objects or invalidation of timer etc.

烟火散人牵绊 2024-12-11 14:15:47

当你想在一定时间后调用某个函数时,NSTimer 确实是一个不错的选择。如果您可以发布代码,我们将尽力解决您的崩溃问题。

线程中的其他可能的选择是进行无限循环并放置等待语句。

您可以从此无限循环调用该函数。

[NSThread detachNewThreadSelector:<#(SEL)selector#>;目标:<#(id)目标#> withObject:<#(id)argument#>]

执行操作后,您可以使用它使其休眠 5 分钟。

[NSThread sleepForTimeInterval:<#(NSTimeInterval)ti#>]

最后确保应用程序进入后台后,任何进程的运行时间都不能超过 10 分钟。因此,在某些进程从后台删除之前,我将无法运行它。

NSTimer is really a good option when you want to call some function after certain duration. If you can post you code we will try to resolve your crash problems.

Other possible option in threading is to make an infinite loop and put wait statement.

You can call the function with infinite loop from this.

[NSThread detachNewThreadSelector:<#(SEL)selector#> toTarget:<#(id)target#> withObject:<#(id)argument#>]

and after performing operation you can make it sleeping for 5 minute using this.

[NSThread sleepForTimeInterval:<#(NSTimeInterval)ti#>]

Finally make sure that once your application goes into background no process can run longer than 10 min. So I will not be possible to run some process till it is deleted from back ground.

终止放荡 2024-12-11 14:15:47

nstimer 是最好的选择,因为它在单独的线程中运行...这是我在代码中使用的,不会造成任何问题。

{
[NSTimer scheduledTimerWithTimeInterval:5.0
                                 target:self
                               selector:@selector(checkNetworkStatus:)
                               userInfo:nil
                                repeats:YES];

return YES;
}


- (void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes

NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)

{
    case NotReachable:
    {
        NSLog(@"The internet is down.");
        internetConnectivity=@"NO";
        break;

    }
    case ReachableViaWiFi:
    {
        NSLog(@"The internet is working via WIFI.");
        internetConnectivity=@"YES";
        SyncRecords *obj=[[SyncRecords alloc]init];
        [obj syncData];
        break;

    }
    case ReachableViaWWAN:
    {
        NSLog(@"The internet is working via WWAN.");
        internetConnectivity=@"YES";
        SyncRecords *obj=[[SyncRecords alloc]init];
        [obj syncData];
        break;

    }
}

}

nstimer is the best option as it runs in a separate thread... this is what i have used in my code and causes no problem whatsoever.

{
[NSTimer scheduledTimerWithTimeInterval:5.0
                                 target:self
                               selector:@selector(checkNetworkStatus:)
                               userInfo:nil
                                repeats:YES];

return YES;
}


- (void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes

NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)

{
    case NotReachable:
    {
        NSLog(@"The internet is down.");
        internetConnectivity=@"NO";
        break;

    }
    case ReachableViaWiFi:
    {
        NSLog(@"The internet is working via WIFI.");
        internetConnectivity=@"YES";
        SyncRecords *obj=[[SyncRecords alloc]init];
        [obj syncData];
        break;

    }
    case ReachableViaWWAN:
    {
        NSLog(@"The internet is working via WWAN.");
        internetConnectivity=@"YES";
        SyncRecords *obj=[[SyncRecords alloc]init];
        [obj syncData];
        break;

    }
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文