Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem while Working in background and Nstimer?

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

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

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.

like image 985
Hrushikesh Betai Avatar asked Jul 26 '26 16:07

Hrushikesh Betai


1 Answers

NSTimer are paused when the app is in background state.

You'll have to start some background task to do what you want. But even with that, you will be limited to a certain amount of time after the app was put in background.

Real backgrounding behavior is only granted for location tracking, VoIP or Audio apps. Other apps must face limitations: once in background, you are given an amount of time to complete tasks you start with beginBackgroundTaskWithExpirationHandler: (backgroundTimeRemaining).

The whole thing is described in iOS Application Programming Guide, Executing Code in the Background, especially here.

like image 180
Jilouc Avatar answered Jul 28 '26 10:07

Jilouc