如何让我的应用程序在后台运行NSTimer?

时间:2022-04-26 01:26:03

I'm making a benchmark App for test purposes ONLY. I am not intending this to go to the App Store.

我正在为测试目的制作一个基准测试应用程序。我不打算去App Store。

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使用UIBackgroundTaskIdentifier继续在后台运行,将数据保存到Core Data数据库,最后在一定的时间间隔之后将数据推送到服务器(我正在使用Parse)。

So basically, I haven´t found any questions which apply to my specific case. I set my NSTimer like so:

所以基本上,我还没有找到任何适用于我的具体案例的问题。我这样设置我的NSTimer:

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

the method updateCoreData simply calls the Core Data class and makes the necessary insertions.

方法updateCoreData只调用Core Data类并进行必要的插入。

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.

我已经阅读了有关VoIP和音乐播放部分的内容,但不知道哪一个最适合我的情况,也不知道如何实现它们。

2 个解决方案

#1


36  

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"

我自己想出来,对于有类似问题的其他人,我做的是首先打开Info.plist文件上的位置更新标志。为此,您必须在Xcode 4上添加名为“Required Background Modes”的密钥,然后作为值,选择“App registers for location updates”

I have a timer declared like so in my .h file:

我的.h文件中有一个如此声明的计时器:

NSTimer *silenceTimer;
@property (nonatomic, retain) NSTimer *silenceTimer;

Then on the .m file, I declared it like so:

然后在.m文件中,我声明它是这样的:

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

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.

这将创建一个计时器,启动并在5分钟后立即停止位置服务。这将足以让应用程序无限期地保持活力,除非你杀死进程。

#2


2  

XCode 6.3.1, for iOS 8.3

适用于iOS 8.3的XCode 6.3.1

There are big diffrences that I've encountered between the apps. behavior while plugged in to your Mac running XCode using debugger and the same apps. behavior while unplugged.

我在应用程序之间遇到了很大的差异。使用调试器和相同的应用程序插入运行XCode的Mac时的行为。不插电的行为。

At current iOS version, 8.3, at most your app is allotted is 180 seconds to run in the background for finite long running task with an expiration handler.

在当前的iOS版本8.3中,最多分配的应用程序在后台运行180秒,以便使用到期处理程序执行有限长时间运行任务。

No matter what you try, with your phone unplugged your app will be killed by the system in 180 seconds or earlier. I've done a significant amounts of tests and tricks to confirm this. See my post...

无论您尝试什么,在手机拔掉电源后,您的应用程序将在180秒或更早的时间内被系统杀死。我做了大量的测试和技巧来证实这一点。看我的帖子......

My NSTimer Post

我的NSTimer帖子

Here is a neat way to tell how much time is left for your app to run before termination, keep in mind that you are not guaranteed this time.

这是一个简洁的方法来告诉您的应用程序在终止之前还剩多少时间,请记住,这次您无法保证。

NSTimeInterval backgroundTimeRemaining = [[UIApplication sharedApplication] backgroundTimeRemaining];

if (backgroundTimeRemaining == DBL_MAX)
{
    NSLog(@"background time remaining = undetermined");
}
else
{
    NSLog(@"background time remaining = %0.2f sec.", backgroundTimeRemaining);
}

Hope this helps your investigation. Cheers!

希望这有助于您的调查。干杯!

#1


36  

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"

我自己想出来,对于有类似问题的其他人,我做的是首先打开Info.plist文件上的位置更新标志。为此,您必须在Xcode 4上添加名为“Required Background Modes”的密钥,然后作为值,选择“App registers for location updates”

I have a timer declared like so in my .h file:

我的.h文件中有一个如此声明的计时器:

NSTimer *silenceTimer;
@property (nonatomic, retain) NSTimer *silenceTimer;

Then on the .m file, I declared it like so:

然后在.m文件中,我声明它是这样的:

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

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.

这将创建一个计时器,启动并在5分钟后立即停止位置服务。这将足以让应用程序无限期地保持活力,除非你杀死进程。

#2


2  

XCode 6.3.1, for iOS 8.3

适用于iOS 8.3的XCode 6.3.1

There are big diffrences that I've encountered between the apps. behavior while plugged in to your Mac running XCode using debugger and the same apps. behavior while unplugged.

我在应用程序之间遇到了很大的差异。使用调试器和相同的应用程序插入运行XCode的Mac时的行为。不插电的行为。

At current iOS version, 8.3, at most your app is allotted is 180 seconds to run in the background for finite long running task with an expiration handler.

在当前的iOS版本8.3中,最多分配的应用程序在后台运行180秒,以便使用到期处理程序执行有限长时间运行任务。

No matter what you try, with your phone unplugged your app will be killed by the system in 180 seconds or earlier. I've done a significant amounts of tests and tricks to confirm this. See my post...

无论您尝试什么,在手机拔掉电源后,您的应用程序将在180秒或更早的时间内被系统杀死。我做了大量的测试和技巧来证实这一点。看我的帖子......

My NSTimer Post

我的NSTimer帖子

Here is a neat way to tell how much time is left for your app to run before termination, keep in mind that you are not guaranteed this time.

这是一个简洁的方法来告诉您的应用程序在终止之前还剩多少时间,请记住,这次您无法保证。

NSTimeInterval backgroundTimeRemaining = [[UIApplication sharedApplication] backgroundTimeRemaining];

if (backgroundTimeRemaining == DBL_MAX)
{
    NSLog(@"background time remaining = undetermined");
}
else
{
    NSLog(@"background time remaining = %0.2f sec.", backgroundTimeRemaining);
}

Hope this helps your investigation. Cheers!

希望这有助于您的调查。干杯!