2017-07-24 26 views
0
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     guard let mostRecentLocation = locations.last else { 
      return 
     } 

     print(mostRecentLocation.coordinate.latitude) 
     print(mostRecentLocation.coordinate.longitude)   
     Timer.scheduledTimer(timeInterval: 60.0, target: self, selector: #selector(StartTestVC.sendDataToServer), userInfo: nil, repeats: true) 
    } 

    func sendDataToServer (latitude: Double, longitude: Double) { 
     SFUserManager.shared.uploadPULocation(latitude, longitude:longitude) 
    } 

我希望每1分鐘向服務器發送一次數據。我正在使用Timer.scheduledTimer並設置選擇器。但我怎麼能發送經緯度/經度函數?如何在定時器選擇器上傳遞參數

+0

你可以把你的位置作爲類變量,併發送 –

+0

是的,我知道要通過選擇送? – pmb

+0

@pmb你只需要擺脫輸入參數並直接在函數內部使用類屬性。請參閱下面的答案。 –

回答

2

對於Timer發送數據可以使用userInfo參數傳遞數據。

下面是您可以通過其調用選擇器方法的示例,並且可以將您的位置座標傳遞給它。

Timer.scheduledTimer(timeInterval: 0.5, target: self, selector:#selector(iGotCall(sender:)), userInfo: ["Name": "i am iOS guy"], repeats:true) 

對於處理userInfo你需要按照下面去。

func iGotCall(sender: Timer) { 
     print((sender.userInfo)!) 
    } 

你的情況確保你的didUpdateLocations被頻繁調用。

+0

即使我設置了1秒,iGotCall也會每秒調用一次。爲什麼? – pmb

+0

一分鐘timeInterval應該是60所以它的timeInterval:60 –

+0

是啊我知道我設置它60,但它延遲60秒,然後每1秒調用IGotCall – pmb

1

確保您的sendDataToServer始終上傳最新座標而不將輸入座標作爲輸入參數的函數的一種方法是將值存儲在一個作用域中,該作用域可以被該函數訪問,並將這些值用於功能。

假設你可以mostRecentLocation類屬性,可以使用下面的代碼

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    guard let mostRecentLocation = locations.last else { 
     return 
    } 

    self.mostRecentLocation = mostRecentLocation 
    Timer.scheduledTimer(timeInterval: 60.0, target: self, selector: #selector(StartTestVC.sendDataToServer), userInfo: nil, repeats: true) 
} 

func sendDataToServer() { 
    SFUserManager.shared.uploadPULocation(self.mostRecentLocation.coordinate.latitude, longitude:self.mostRecentLocation.coordinate.longitude) 
} 
+0

@Dnvid,但sendDataToServer根本不會被調用 – pmb

+0

在更改之前它是否工作?你沒有提到它沒有在你的問題中被調用。如果它在任何一個之前都不起作用,didUpdateLocations被調用了嗎? –

0

這也正是userInfo參數是指用於:

struct SendDataToServerData { //TODO: give me a better name 
    let lastLocation: CLLocation 
    // Add other stuff if necessary 
} 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    guard let mostRecentLocation = locations.last else { return } 

    print(mostRecentLocation.coordinate.latitude) 
    print(mostRecentLocation.coordinate.longitude) 

    Timer.scheduledTimer(
     timeInterval: 60.0, 
     target: self, 
     selector: #selector(StartTestVC.sendDataToServer(timer:)), 
     userInfo: SendDataToServerData(mostRecentLocation: mostRecentLocation), 
     repeats: true 
    ) 
} 

// Only to be called by the timer 
func sendDataToServerTimerFunc(timer: Timer) { 
    let mostRecentLocation = timer.userInfo as! SendDataToServerData 
    self.sendDataToServer(
     latitude: mostRecentLocation.latitude 
     longitude: mostRecentLocation.longitude 
    ) 
} 

// Call this function for all other uses 
func sendDataToServer(latitude: Double, longitude: Double) { 
    SFUserManager.shared.uploadPULocation(latitude, longitude:longitude) 
} 
+0

sendDataToServer每秒調用一次,即使我設置了1秒。爲什麼? – pmb

+0

@pmb huh?你有錯別字嗎? – Alexander

+0

我複製你的代碼做了一些改變,但sendDataToServer呼叫1分鐘後,但之後,它開始每1秒呼叫 – pmb

相關問題