重复调用函数
我正在创建一个函数,该函数将每 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.当你想在一定时间后调用某个函数时,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.
nstimer 是最好的选择,因为它在单独的线程中运行...这是我在代码中使用的,不会造成任何问题。
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.