问题描述
我仅出于测试目的制作基准测试应用程序.我无意去App Store.
I'm making a benchmark App for test purposes ONLY. I am not intending this to go to the App Store.
我需要的是我的NSTimer,以便在一定时间间隔后使用UIBackgroundTaskIdentifier继续在后台运行,将数据保存到Core Data db,最后将数据推送到服务器(我正在使用Parse),
What I need is my NSTimer to continue running on the background using a UIBackgroundTaskIdentifier, save data to a Core Data db and finally push the data to a server (I'm using Parse), after a certain time interval, of course.
因此,基本上,我没有发现适用于我的特定情况的任何问题.我将NSTimer设置为:
So basically, I haven´t found any questions which apply to my specific case. I set my NSTimer like so:
UIBackgroundTaskIdentifier bgTask;
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
self.timer = [NSTimer scheduledTimerWithTimeInterval:self.localInterval target:self selector:@selector(updateCoreData:) userInfo:nil repeats:YES];
updateCoreData方法仅调用Core Data类并进行必要的插入.
the method updateCoreData simply calls the Core Data class and makes the necessary insertions.
我已经阅读了有关VoIP和音乐播放器的内容,但不知道确切地哪种最适合我的情况,也不知道如何实现它们.
I've read about VoIP and the Music playing part, but don't know exactly which one would apply best for my case, nor how to implement them.
推荐答案
我自己弄清楚了,对于其他有类似问题的人,我所做的就是首先打开Info.plist文件上的位置更新标志.为此,您必须在Xcode 4上添加称为必需的后台模式"的密钥,然后将其选择为用于位置更新的应用注册"作为值
I figured it out by myself, for anyone else with a similar problem, what I did was to first turn on the location update flag on your Info.plist file. To do this, you must add the Key called "Required Background Modes" on Xcode 4, then as a value, select "App registers for location updates"
我的.h文件中有一个这样声明的计时器:
I have a timer declared like so in my .h file:
NSTimer *silenceTimer;
@property (nonatomic, retain) NSTimer *silenceTimer;
然后在.m文件中,我这样声明它:
Then on the .m file, I declared it like so:
UIBackgroundTaskIdentifier bgTask;
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
self.silenceTimer = [NSTimer scheduledTimerWithTimeInterval:300 target:self
selector:@selector(startLocationServices) userInfo:nil repeats:YES];
最后,在选择器方法上,我做了以下事情:
Finally, on the selector method, I made the following:
-(void)startLocationServices {
[locationManager startUpdatingLocation];
[locationManager stopUpdatingLocation];
}
这将创建一个计时器,该计时器将在5分钟后启动并立即停止定位服务.除非您终止该过程,否则这足以使该应用无限期地存活.
This will create a timer that starts and immediately stops location services after 5 minutes. This will be enough for the app to stay alive indefinately, unless you kill the process.
这篇关于如何使我的应用程序在后台运行NSTimer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!