在后台和Nstimer工作时遇到问题? [英] Problem while Working in background and Nstimer?

查看:117
本文介绍了在后台和Nstimer工作时遇到问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我已经使用Nstimer和后台进程来开发应用程序。

Hi i have atask to develop the application with Nstimer and background process.

我已经使用计时器实现了后台进程。它是令人兴奋的好。但是当我第一次最小化应用程序时,我有问题,那时它没有运行后台进程。最小化应用3至4次后。之后它运作顺利。我还显示后台任务和计时器的代码如下。

I have already implement background process with timer. And it is excuting good.but i have problem when i minimize application first time at that time it is not running the background process. after minimizing application 3 to 4 times. After that it is working smoothly. i also display the code of background task and timer as follow.

 - (void)applicationDidEnterBackground:(UIApplication *)application {

    UIApplication*    app = [UIApplication sharedApplication];
    NSLog(@"Application enter in background");
    [NSTimer scheduledTimerWithTimeInterval:2.0f
                                     target:self
                                   selector:@selector(updateCounter:)
                                   userInfo:nil
                                    repeats:YES]; 
}

我的updateCounter方法如下:

And My updateCounter method is as given follow:

    - (void)updateCounter:(NSTimer*)timer {

    NSString *id = [[UIDevice currentDevice] uniqueIdentifier];
    NSLog(@"uniqueid:%@",id);

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation];

    CLLocation *location = [locationManager location];

    // Configure the new event with information from the location
    CLLocationCoordinate2D coordinate = [location coordinate];

    NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude]; 
    NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];

    NSLog(@"dLatitude : %@", latitude); 
    NSLog(@"dLongitude : %@",longitude);

}

他们有任何问题相关代码请帮我解决。

Is their any problem related code Please help me to solve it.

推荐答案

首先:当您的应用程序进入后台时,计时器将无法正常工作。
(取决于运行循环的可用性和超时)

First of all: Timers won't work as you expect when your app goes in background. (depends on the availability of the run loop and your timeout)

从我从代码中收集到的内容,您似乎希望在应用程序时更新位置在后台运行。为此,您应该从这里查看指南:
https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/ doc / uid / TP40007072-CH4-SW24

From what i gather from your code, seems you like to have location update when the app is running in background. For this, you should check the guidelines from here: https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW24


有几种方法可以跟踪用户在后台的位置,
其中一些实际上并不涉及在
背景中定期运行:

There are several ways to track the user’s location in the background, some of which do not actually involve running regularly in the background:


  • 应用程序可以注册重要的位置更改。
    (推荐)重要变更位置服务提供
    低功耗方式来接收位置数据,强烈推荐用于不需要高精度位置数据的
    应用程序。使用此
    服务,仅当用户的位置
    发生显着变化时,才会生成位置更新;因此,它是社交应用程序或
    应用程序的理想选择,它们为用户提供非关键的,与位置相关的
    信息。如果在更新发生时暂停应用程序,系统会在后台唤醒它以处理更新。如果
    应用程序启动此服务然后终止,系统
    会在新位置变为
    时自动重新启动应用程序。此服务在iOS 4及更高版本中可用,仅适用于包含蜂窝无线电的
    设备。

  • 应用程序可以继续使用标准位置服务
    。虽然不打算在后台无限期地运行
    ,但标准位置服务在所有版本的iOS中都是
    ,并提供通常的更新,同时应用程序运行
    ,包括在后台运行时。
    但是,一旦应用程序暂停或
    终止,更新就会停止,并且新的位置更新不会导致应用程序被唤醒或重新启动
    。当主要在应用程序位于
    前景中时使用
    位置数据时,此类服务是合适的。

  • 应用程序可以声明自己需要连续的
    后台位置更新。需要在前台和后台进行常规
    位置更新的应用程序应该将
    UIBackgroundModes键添加到其Info.plist文件中,并将
    的值设置为包含该键的数组位置字符串。此选项为
    ,用于提供特定服务的应用程序,例如
    导航服务,其中包括始终向用户通知其位置或
    的位置。
    应用程序的Info.plist文件中存在密钥告诉系统应该允许
    应用程序在后台根据需要运行。

鼓励您使用重要的地点变更服务
或谨慎使用标准的
服务。定位服务需要主动使用iOS
设备的板载无线硬件。连续运行此硬件
可能会消耗大量电量。如果您的应用程序不需要向
用户提供精确且连续的位置信息,则最好使用那些最小化功耗
消费的服务。这些低功耗服务中最主要的是iOS 4中引入的重要
位置更改服务。此服务提供
定期位置更新,甚至可以唤醒后台
应用程序,或重新启动已终止的应用程序为了交付它们。

You are encouraged to use the significant location change service or use the standard services sparingly. Location services require the active use of an iOS device’s onboard radio hardware. Running this hardware continuously can consume a significant amount of power. If your application does not need to provide precise and continuous location information to the user, it is best to use those services that minimize power consumption. Chief among these low-power services is the significant location change service introduced in iOS 4. This service provides periodic location updates and can even wake up a background application, or relaunch a terminated application, to deliver them.

对于需要以常规
为间隔的更精确位置数据的应用程序,例如导航应用程序,您需要声明
申请作为连续背景申请。这个选项是
可用于真正需要它的应用程序,但它是最低
的理想选项,因为它大大增加了用电量。

For applications that require more precise location data at regular intervals, such as navigation applications, you need to declare the application as a continuous background application. This option is available for applications that truly need it, but it is the least desirable option because it increases power usage considerably.

这篇关于在后台和Nstimer工作时遇到问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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