Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

objective c - IOS didUpdateLocations called multi time

I'm using didUpdateLocation. Inside, I have called a webservices.

And, I want to every 2 minutes, I will call webservices.

But, didUpdateLocation update location multi time. So, I have to call webservices. This is not good.

How to I can call webservices every 2 minutes.

Please help me!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to trigger the call with something other than didUpdateLocation, because the frequency of that call is up to the user of the device, and not your code. If you only need to call the web service if there has been a significant change in location, you can do as follows.

  1. Save the location passed to didUpdateLocation.
  2. Use a repeating NSTimer on a two minute interval.
  3. Every time the timer fires, check the current location versus the location when the last request was made. If it exceeds the threshold, make the request.
  4. If the request is made, save the location for the next iteration.

An alternative algorithm:

  1. Make the web request. Remember the time.
  2. When didUpdateLocation fires, check the last time you made the web request. If it was more than 2 minutes ago, make the request, else ignore the update.

You might need a bit of each, depending on your exact needs. If the cadence for the request is most important, start with the first. If the only important bit is that you don't call more frequently than every 2 minutes, but longer intervals are perfectly OK, go with the second.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...