在我的viewController
中,我有一个每 20 秒触发一次并调用位置更新的计时器。然后,当位置更新时,我通过performSelectorOnMainThread
. 出于某种原因,即使我已经包含waitUntilDone:NO
,我的用户界面在执行检查时也会一直锁定。有谁知道我可以如何改进这一点,以使屏幕不会每 20 秒冻结一次?谢谢!
-(void)viewDidLoad {
NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 20.0 target: self
selector: @selector(callAfterSixtySecond:) userInfo: nil repeats: YES];
//other code
}
-(void) callAfterSixtySecond:(NSTimer*) t {
locationManagerProfile.delegate = self;
locationManagerProfile.desiredAccuracy = kCLLocationAccuracyBest;
[locationManagerProfile startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
*)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
[self performSelectorOnMainThread:@selector(buttonUpdate) withObject:nil
waitUntilDone:NO];
}
}
最后,我的界面是用这个方法更新的:
-(void) buttonUpdate {
[locationManagerProfile stopUpdatingLocation];
NSString *userLatitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate
getUserLatitude];
NSString *userLongitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate
getUserLongitude];
NSString *placeLatitude = [[NSUserDefaults standardUserDefaults]
stringForKey:@"savedLatitude"];
NSString *placeLongitude = [[NSUserDefaults standardUserDefaults]
stringForKey:@"savedLongitude"];
NSString *distanceURL = [NSString stringWithFormat:@"http://www.website.com/page.php?
lat1=%@&lon1=%@&lat2=%@&lon2=%@",userLatitude, userLongitude, placeLatitude,
placeLongitude];
NSData *distanceURLResult = [NSData dataWithContentsOfURL:[NSURL
URLWithString:distanceURL]];
NSString *distanceInFeet = [[NSString alloc] initWithData:distanceURLResult
encoding:NSUTF8StringEncoding];
if ([distanceInFeet isEqualToString:@"1"]) {
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:@"Button A"
style:UIBarButtonItemStyleBordered target:self action:@selector(actionA)];
self.navigationItem.rightBarButtonItem = btnGo;
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor
colorWithRed:44.0/255.0 green:160.0/255.0 blue:65.0/255.0 alpha:1.0]];
UIBarButtonItem *btnGoTwo = [[UIBarButtonItem alloc] initWithTitle:@"Button B"
style:UIBarButtonItemStyleBordered target:self action:@selector(actionB)];
self.navigationItem.rightBarButtonItem = btnGoTwo;
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:btnGo,
btnGoTwo, nil];
}
if ([distanceInFeet isEqualToString:@"0"]) {
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:@"Button C"
style:UIBarButtonItemStyleBordered target:self action:@selector(actionC)];
self.navigationItem.rightBarButtonItem = btnGo;
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor
colorWithRed:44.0/255.0 green:160.0/255.0 blue:65.0/255.0 alpha:1.0]];
UIBarButtonItem *btnGoTwo = [[UIBarButtonItem alloc] initWithTitle:@"Button B"
style:UIBarButtonItemStyleBordered target:self action:@selector(actionB)];
self.navigationItem.rightBarButtonItem = btnGoTwo;
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:btnGo,
btnGoTwo, nil];
}
}