gpt4 book ai didi

iphone - 即使使用 performSelectorOnMainThread 和 waitUntilDone :NO,UI 也会锁定

转载 作者:行者123 更新时间:2023-11-28 17:59:44 25 4
gpt4 key购买 nike

在我的 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];
}
}

最佳答案

您似乎在主线程上使用 dataWithContentsOfURL: 从 URL 获取一些数据。

看起来它会导致卡住。

尝试将其移出,以便仅在您要在 UI 中显示的数据准备就绪时才调用主线程。

关于iphone - 即使使用 performSelectorOnMainThread 和 waitUntilDone :NO,UI 也会锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17260449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com