iphone - NSTimers在后台 [英] iphone - NSTimers in background

查看:113
本文介绍了iphone - NSTimers在后台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个必须在后台运行的应用程序。它是一个基于位置的应用程序,所以它一直运行,操作系统不会杀死它。



它应该每10秒发送一些信息(仅用于调试),我在后台设置了一个定时器。我在这个函数中设置一个断点,每10秒钟执行一次,这是永远不会被调用的,但如果我暂停应用程序,然后继续调用定时器,然后定时器每10秒钟执行一次,没有问题, / p>

我以为当我没有调试时,计时器将会执行,但是并不像我没有暂停调试一样。 p>

我的问题是为什么?定时器设置正确(我假设),因为它在暂停后工作,但不是。



任何想法?



我设置定时器的方式是:

  self.timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector (doStuff)userInfo:nil重复:YES]; 

在我连接到webservice的函数中。



谢谢。

解决方案

我有一个类似的应用程序设计,被困在同一件事上。我在互联网上发现的地方是添加这种类型的语句 applicationDidEnterBackground:

  UIBackgroundTaskIdentifier locationUpdater = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^ {
[[UIApplication sharedApplication] endBackgroundTask:locationUpdater];
locationUpdater = UIBackgroundTaskInvalid;
}];

这告诉我们你仍然有事情,而不是停止它。



我的定时器附加到此功能

  //这是一个包装方法适合所需的选择器签名
- (void)timeIntervalEnded :( NSTimer *)timer {
[self writeToLog:[NSString stringWithFormat:@Timer Ended On%@,[NSDate date]]]
[self startReadingLocation];
[timer invalidate];
timer = nil;
}

我在我的位置管理器委托方法中设置了计时器。



我感到你的痛苦。我发现这些东西是超级迷人的。这对我来说有用。我希望它有帮助。我发现在后台你不能做任何真正的限制。


Im developing an app that has to run in the background. It's a location based app, so it runs all the time, the OS doesn't kill it.

It should send some info every 10 secs(just for debugging), I set a timer once its in the background. I set a breakpoint in the function that should be executed every 10 secs, which is never called, but if I pause the app and then continue the timer is called, and then the timer is executed every 10 secs without problems, weird right?

I thought that the timer would be executing anyway when I wasn't debugging, but it isn't, same thing as if I didn't pause the debugging.

My question is WHY?? The timer is set correctly(I assume) since it works after pausing, but it's not.

Any ideas?

The way I set the timer is:

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

And in the function I connect to a webservice.

Thanks.

解决方案

I have a similar app design and was stuck on the same thing. What I found somewhere on the internet is adding this type of statement applicationDidEnterBackground:

    UIBackgroundTaskIdentifier locationUpdater =[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
         [[UIApplication sharedApplication] endBackgroundTask:locationUpdater];
        locationUpdater=UIBackgroundTaskInvalid;
     } ]; 

This tells the os that you still have things going and not to stop it.

I have my timer attached to this function

 //this is a wrapper method to fit the required selector signature
- (void)timeIntervalEnded:(NSTimer*)timer {
     [self writeToLog:[NSString stringWithFormat:@"Timer Ended On %@",[NSDate date]]];
     [self startReadingLocation];
     [timer invalidate];
     timer=nil;
}

I set the timer in my my location manager delegate methods.

I feel your pain. I found that these things were super finicky. This is what worked for me. I hope it helps. I have found that there isn't any really restrictions in what you can do in the background.

这篇关于iphone - NSTimers在后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆