How do i do a background network call when i get the location?

Viewed 601

When the location is changed in the background I want to update location to the server. I want a safe way to update location in the background every time I receive a significant location change. How do I make a network call?

 func endBackgroundUpdateTask() {
        UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
        self.backgroundUpdateTask = UIBackgroundTaskInvalid
    }


    func scheduledLocationManager(_ manager: APScheduledLocationManager, didUpdateLocations locations: [CLLocation]) {
        print(locations.last?.description ?? "no location")
        self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
            Alamofire.request("https://testomkar.herokuapp.com/log", method: .post, parameters: ["log":locations.last?.description ?? "no location"]).validate().responseJSON(completionHandler: { (responce) in
                self.endBackgroundUpdateTask()
            })
        })


    }
1 Answers
Related