问题
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!
回答1:
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.
- Save the location passed to
didUpdateLocation
. - Use a repeating
NSTimer
on a two minute interval. - 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.
- If the request is made, save the location for the next iteration.
An alternative algorithm:
- Make the web request. Remember the time.
- 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.
回答2:
Use NSTimer.
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(handleTimer:)
userInfo:nil repeats:YES];
Put this code in DidLoad Method and Add the below code in somewhere else.
- (void)handleTimer:(NSTimer*)theTimer {
NSLog (@"Working");
}
来源:https://stackoverflow.com/questions/33667077/ios-didupdatelocations-called-multi-time