Problem while running in the background and Nstimer?

Hi I have an atask for developing an application with Nstimer and a background process.

I've already been running a background process using a timer. And that excludes good.but, I have a problem when I minimize the app for the first time, at that time it doesn't start the background process. after minimizing the application 3 - 4 times. After that, it runs smoothly. I am also displaying the background task and timer code as shown below.

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

      

And my updateCounter method is below:

    - (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);

}

      

Do they have any problematic code. Please help me to solve the problem.

+2


source to share


3 answers


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

From what I am gathering from your code, it seems you like to have the location update when the app is running in the background. To do this, you should read the guidelines from here: https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW2424



There are several ways to track users' location in the background, some of which don't actually include a regular background:

  • Apps can register for significant location changes. (Recommended) Significantly modified location service offers a low-power way to obtain location data and is highly recommended for applications that do not need highly accurate location data. With this service, location updates are generated only when users' location changes significantly; thus, it is ideal for social applications or applications that provide the user with non-critical, location-relevant information. If an app pauses while an update occurs, the system wakes it up in the background to process the update. If an application starts this service and then exits, the system automatically starts the application,when a new location becomes available. This service is available in iOS 4 and later, only on devices that contain a cellular radio.
  • Apps can continue to use standard location services. While it is not intended to run indefinitely in the background, standard location services are available on all iOS versions and provide regular updates while the app is running, including while running in the background. However, updates stop as soon as the app is paused, or new location updates do not cause the app to wake up or resume. This type of service is suitable if location data is used primarily when the application is in the foreground.
  • The app may assert itself as needing to continuously update the background location. An application that needs regular location updates, both foreground and background, should add the UIBackgroundModes key to the Info.plist file and set the value of this key to an array containing the location string. This setting is intended for applications that provide specific services, such as navigation services, which include informing the user about his or her location at any time. The presence of a key in the application. The Info.plist file tells the system to allow the application to run as needed in the background.

You are encouraged to use significant location change service or use standard services sparingly. Location services require the active use of iOS devices on radio equipment. Continuous use of this equipment can consume significant amounts of energy. If your application is not required to provide accurate and continuous information about the user's location, 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 has periodic location updates and may even wake up in the background of an app or restart a completed app to deliver them.

For apps that require more accurate location data at regular intervals, such as navigation apps, you need to declare as a continuous background app. This option is available for applications that really need it, but it is the least desirable option as it significantly increases power consumption.

+3


source


NSTimer

are suspended when the application is in the background.

You will need to run some background task to do what you want. Even so, you will be limited to a certain amount of time after the application is placed in the background.



Real background behavior is only provided for location tracking, VoIP or audio applications. Other applications have to face limitations: once in the background, you are given a set amount of time to complete tasks starting with beginBackgroundTaskWithExpirationHandler:

( backgroundTimeRemaining

).

This is all covered in the iOS Application Programming Guide, Code Execution in the Background Especially.

+4


source


Check when your app goes into background and when it enters foregroud, calculate this difference and add the elapsed time to your timer to get an approximate total time. I also implemented the same and it works very well at the end.

+3


source







All Articles