2017-03-06 46 views
1

在我的Swift应用程序中,我正在检查用户的位置并将其发送到我的后端代码。我的Swift App在使用位置管理器时非常快地消耗电量

我该代码全部放在AppDelegate,它看起来是这样的:用我的应用程序后

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 
    switch status { 
    case .notDetermined: 
     print("NotDetermined") 
    case .restricted: 
     print("Restricted") 
    case .denied: 
     print("Denied") 

    case .authorizedAlways: 
     print("AuthorizedAlways") 
    case .authorizedWhenInUse: 
     print("AuthorizedWhenInUse") 
     locationManager.distanceFilter = 200 
     locationManager.startUpdatingLocation() 
    } 
} 

func initLocationManager() { 
    print("init location manager app delegate") 
    locationManager = CLLocationManager() 
    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters 

    if CLLocationManager.authorizationStatus() == .authorizedWhenInUse { 
     //locationManager.startMonitoringSignificantLocationChanges() 

     locationManager.distanceFilter = 200 

     locationManager.startUpdatingLocation() 
    } else { 

     locationManager.requestWhenInUseAuthorization() 
    } 

} 

现在:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

    NotificationCenter.default.addObserver(self, selector: 
     #selector(AppDelegate.notificationConfirmationSent), 
     name: NSNotification.Name(rawValue: locationUpdKey), object: nil) 

    initLocationManager() 

    return true 
} 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    location = locations.last 
    let coord = location.coordinate 
    longitude = coord.longitude 
    latitude = coord.latitude 
    NotificationCenter.default.post(name: Notification.Name(rawValue: locationUpdKey), object: self) 
    location_fixed = true; 
} 


func notificationConfirmationSent() 
{ 

    if(defaults.object(forKey: "uniqueId") != nil) { 
     let currentTime = Date().timeIntervalSince1970 
     if (currentTime - timeInterval > 30) { 
      print("SENDING DATA TO WEBSERVICE FROM NOTIFICATION") 
      timeInterval = currentTime 
      sendCurrentUserPositionToWebService(self.longitude, latitude: self.latitude, uniqueId: defaults.string(forKey: "uniqueId")!) 
     } 
    } 
} 

除此之外我还使用这两种方法一段时间以来,电池消耗非常快,而且整个手机也变得越来越热。我想改变这种行为并修复它,所以手机只发送一次位置(当它是fixed),然后在位置发生重大变化时这样做。 你能给我一个提示,我怎么能从这里开始,为什么电池的排水速度如此之快?

+0

没有任何东西耗尽电池,就像说'startUpdatingLocation'并将其保持打开状态。这显然是你在做什么(虽然你没有显示足够的代码来确定)。 – matt

+0

@matt目前我在'applicationWillTerminate中也使用'locationManager.stopUpdatingLocation()locationManager = nil',但我想这还不够? – user3766930

+0

我应该在哪一点停止更新位置? – user3766930

回答

0

至于电池消耗,运行仪器,看看什么是消耗CPU。我的猜测是,它不是位置经理。

只要知道重大变化,苹果就有一个API。

startMonitoringSignificantLocationChanges()

注意应用程序能够尽快期待一个通知,该设备从之前通知移动500 米以上。它不应该比每五分钟更频繁地期望 通知。如果设备能够从网络检索数据,则位置管理器 更有可能以及时的方式发送通知。

0

一般的想法是,你可以调用startMonitoringSignificantLocationChanges你现在设置的location_fixed = true。您还需要停止位置更新,因此您的代码可能如下所示:

location_fixed = true 
manager.stopUpdatingLocations() 
manager.startMonitoringSignificantLocationChanges()