Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8675901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:03:51+00:00 2026-06-12T20:03:51+00:00

I know how to make this with NSTimer but I wan’t to get current

  • 0

I know how to make this with NSTimer but I wan’t to get current iPhone coordinates without timer on every few seconds. I can’t use timer because I am getting coordinates while application is in background.
I have tried something but this calls every second not every 10 seconds as I wan’t.
What am I doing wrong here?

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *loc = [locations objectAtIndex:0];

    NSDate* eventDate = loc.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (howRecent < 10)
    {
        CLLocation* location = [locations lastObject];

        double lat = location.coordinate.latitude;
        double lng = location.coordinate.longitude;
        NSLog(@"lat:%f lng:%f", lat, lng);

First try to do background task every 10 seconds, but don’t know where to set time if I do anything right here:

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

    backgroundTask = [application beginBackgroundTaskWithExpirationHandler: ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (backgroundTask != UIBackgroundTaskInvalid)
            {
                [application endBackgroundTask:backgroundTask];
                backgroundTask = UIBackgroundTaskInvalid;
            }
        });
    }];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        _pendingLocationsTimer = [NSTimer timerWithTimeInterval:_pendingLocationsTimerDuration
                                                         target:self
                                                       selector:@selector(_acceptBestAvailableLocation:)
                                                       userInfo:nil repeats:NO];


        NSRunLoop *loop = [NSRunLoop currentRunLoop];
        [loop addTimer:_pendingLocationsTimer forMode:NSRunLoopCommonModes];
        [loop run];

        dispatch_async(dispatch_get_main_queue(), ^{
            if (backgroundTask != UIBackgroundTaskInvalid)
            {
                // if you don't call endBackgroundTask, the OS will exit your app.
                [application endBackgroundTask:backgroundTask];
                backgroundTask = UIBackgroundTaskInvalid;
            }
        });
    });

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:03:51+00:00Added an answer on June 12, 2026 at 8:03 pm

    I have had issues with timers in the background state. There is no guarantee that you will have an active run loop and so the timer will never fire until you come back to foreground. What I did is:

     NSRunLoop *loop = [NSRunLoop currentRunLoop];
     [loop addTimer:myTimer forMode:NSRunLoopCommonModes];
     [loop run];
    

    I do this on a background thread inside BackgroundIdentifierTask begin and end calls. I am not fully confident that I have it all correct, in fact I looked at your question to see if an answer may help me confirm or correct my understanding of the requirements.

    You also must have the background mode for location services enabled in your default info.pList if you want to continually monitor location. You do not need that if you are just using significantLocationUpdates or geofence.

    My timer does work and does fire as intended set up this way. It did not work reliably prior to that.

    Aside from that, you may be best off with no timer. Using your code above, just change the properties of your location manager. Desired accuracy and distance filter are properties that will reduce how often the manager sends out a new location notification.

    See an example of my own location handler I just posted on github for anyone that is interested:

    http://github.com/dsdavids/TTLocationHandler

    I don’t think the timer is the key. If you drop the TTLocationHandler class in your project, then take a look at how the LMViewController class responds to the handler.

    NSNotificationCenter *defaultNotificatoinCenter = [NSNotificationCenter defaultCenter];
    [defaultNotificatoinCenter addObserver:self selector:@selector(handleLocationUpdate) name:LocationHandlerDidUpdateLocation object:nil];
    

    sets up the controller as an observer. Whenever the handler determines a new or more accurate location has been received, handleLocationUpdate is called.

    The locationManager, when startUpdating is invoked will send out new locations, many per second at first and when moving until the device is stationary and accuracy achieved. The TTLocationHandler is filtering these events and only sending notification as needed based on configuration.

    Note that in -(id)init, _recencyThreshold is set to 60. So we are saving or displaying a pin every 60 seconds. If you want smaller interval, change this.

    As it is, TTLocationHandler will always switch to significant location changes when in background unless the device is plugged in to power. You will not get that small an interval between updates if it is on battery power, but you will get updates.

    I added a property to configure for continuous background updates without charging.

    Example Use of TTLocationHandler

    I have added another class to the repository to show how I would go about achieving your goals with the my handler. I have added LMPinTracker class where you can add your code for storing and uploading the locations.

    Look at LMAppDelegate.m

        self.pinTracker.uploadInterval = 30.00;
    

    change that to whatever interval you want between uploads to the server.

        self.sharedLocationHandler.recencyThreshold = 5.0;
    

    Change that 5.0 to the minimum time you want between stored locations. It won’t be exact, will vary on the rate of travel and the signal strength but basically, after x seconds interval it will consider the stored location stale and attempt to get another accurate location.

    Now look at the LMPinTracker.m file
    Put your data storing code right after these two lines:

        // Store the location into your sqlite database here
        NSLog(@"Received location info: %@ \n Ready for store to database",lastKnowLocationInfo);
    

    Put your web upload code right here after this comment:

        // Do your upload to web operations here
    

    Comments

    This should do what you are looking to do, while still respecting user battery and background operation.

    Basically, when a user is traveling, updates will be continuous at about the interval you have set.
    When a user is stationary, there will be no updates and no significant activity.
    When there is no new activity, you won’t upload to web either.
    Logging and updating resumes when user starts moving again.

    If I were you, looking to refine it further, I would consider setting a region and time to check. If the user remains stationary for a length of time, switch to significantLocationUpdates only. Then you will power down the location services but be brought back up when the user gets underway once again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Guys I know this question is silly but just to make sure: Having in
I already know how to make this code work, but my question is more
May I know what is the workaround to make this code passed, in Visual
As you know the dash introduces a comment how can I make this valid?
i don't really know what ruby,gems, or ror is, my objective is make this
I know how to make the intellisense work in a .js file using this
HI May i know how to make the scroll view as mentioned in this
I know that this line of code will make the cell text-wrap: $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setWrapText(true); 'D1'
In Oracle, this returns 03/01/2010. That does not make sense to me. Anybody know
I dont know how to make this with simple and easy code. I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.