【问题标题】:How to run NSTimer in background for 1 or 2 days with time interval around 300 secs in iOS?如何在 iOS 中以大约 300 秒的时间间隔在后台运行 NSTimer 1 或 2 天?
【发布时间】:2016-09-20 15:18:02
【问题描述】:

我正在做一个项目,我需要在特定时间间隔内获取所有应用程序状态的位置更新。我使用 NSTimer 在后台和前台的特定时间间隔调用 locationUpdate 方法。但是如果间隔时间约为 30 秒,定时器只能在后台工作 1-2 小时,当我将间隔增加到 300 秒时,定时器即使在 10 分钟内也不会在后台工作。请告诉我 - 有没有办法在后台运行计时器至少一天,时间间隔约为 300 秒,或者在所有州以特定时间间隔获取位置更新???

提前致谢!!

【问题讨论】:

    标签: ios timer location


    【解决方案1】:

    您是否有理由更喜欢 NSTimer 而不是后台位置更新?

    // Delegate method from the CLLocationManagerDelegate protocol.
    - (void)locationManager:(CLLocationManager *)manager
      didUpdateLocations:(NSArray *)locations {
     // Add the new locations to the hike
    [self.hike addLocations:locations];
    
    // Defer updates until the user hikes a certain distance
    // or when a certain amount of time has passed.
    if (!self.deferringUpdates) {
      CLLocationDistance distance = self.hike.goal - self.hike.distance;
      NSTimeInterval time = [self.nextAudible timeIntervalSinceNow];
      [locationManager allowDeferredLocationUpdatesUntilTraveled:distance
                                                         timeout:time];
    self.deferringUpdates = YES;
       }
    }
    

    https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html

    【讨论】:

    • 我无法找到解决方案。你能详细说明一下吗?什么是 nextAudible,deferringUpdates? @eltomato
    • 这段代码在我在代码下方提供的链接中有描述!我认为背景位置正是您所需要的!
    • 我用过这个方法但是位置更新没有延迟!!! @eltomato
    猜你喜欢
    • 2014-05-02
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多