2017-07-24 24 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

相关问题