我有一個使用AlamoFire向服務器發送經度/緯度的iPhone應用程序。當按下按鈕時,數據將從應用程序發送。除了服務器從一個動作接收多個相同數據的條目之外,所有的工作都很好。我可以在模擬器輸出以及服務器中看到這一點。此外,它似乎是隨機的,有時是3項,2只或1 使用SWIFT 2.0,這裏是代碼:發佈位置數據到服務器接收多個條目
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("inside didUpdateLocations", terminator: "")
let latestLocation = locations.last
let coords = latestLocation!.coordinate
print("Latitude: " + coords.latitude.description + " Longitude: " + coords.longitude.description, terminator: "")
locationManager.stopUpdatingLocation()
// **UPDATED HERE**
locationManager.delegate = nil
let recievedData : CLLocation! = nil
if recievedData == nil {
sendDatatoServer(coords.longitude, latitude: coords.latitude, date: dateString)
}} // **End UPDATE**
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Error while updating location: " + error.localizedDescription, terminator: "")
}
@IBAction func findMyLocation(sender:AnyObject) {
print("inside findMyLocation", terminator: "")
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
// send the longitude, latitude, and date to the database server
func sendDatatoServer(longitude: Double!, latitude: Double!, date: String!) {
print("inside sendDataToServer", terminator: "")
Alamofire.request(.POST, "http://example.com", parameters:["latitude": latitude, "longitude": longitude, "date": date])
}
在Xcode輸出:
inside findMyLocation * inside didUpdateLocations * Latitude: -26.204103 Longitude: 28.0473051inside sendDataToServer *
inside didUpdateLocations * Latitude: -26.204103 Longitude: 28.0473051inside sendDataToServer *
inside didUpdateLocations * Latitude: -26.204103 Longitude: 28.0473051inside sendDataToServer *
從數據庫:
ID:622 --->經度:28.0473。緯度:-26.2041。 Date:2015-10-08 10:04 AM
ID:623 ---> Longitude:28.0473。緯度:-26.2041。 Date:2015-10-08 10:04 AM
ID:624 ---> Longitude:28.0473。緯度:-26.2041。日期:2015-10-08 10:04 AM
在這個特定的POST中,它發送了3次。從xcode輸出它看起來是從didUpdateLocations,但我不明白爲什麼。
謝謝@rmp我儘可能的按照你的建議盡我所能。到目前爲止,我所做的更改(在主要問題中更新)正在按照我的希望工作。 – JackZ
非常好!真高興你做到了! – rmp