我完全被难住了,情况如下:
我的应用程序使用核心位置框架来获取用户的当前位置,然后在TrailBehind上 ping 我的服务器以查找附近有趣的地方并将它们显示为列表。没问题。
为了节省电池,我在从服务器获取数据后关闭了 GPS 服务。如果用户在使用应用程序时四处走动并想要一个新列表,他单击导航控制器上的“刷新”并再次激活 CLLocation 服务,从服务器检索一批新数据并重绘表格。
当应用程序从我的服务器获取数据时,我加载了一个带有旋转地球的加载屏幕,上面写着“正在加载,请稍候”,我隐藏了导航栏,这样他们就不会点击“返回”。
因此,从服务器获取的初始数据完美无缺。
我第一次点击刷新所有代码执行以获取新位置,再次 ping 服务器以获取新的数据列表并更新单元格。但是,它不会按应有的方式加载表格视图,而是恢复表格视图的导航控制器栏,但仍会在主窗口中显示我的加载视图。这仅在设备上是正确的,在模拟器中一切正常。
我第二次点击刷新功能正常工作。
我第三次刷新它失败如上。
我第四次刷新它正常工作。
我第五次刷新它失败如上。
等等,偶数刷新成功,奇数刷新失败。我逐行遍历了所有代码,一切似乎都在正常执行。我实际上继续跳过核心指令,在大量单击“跳过”之后,我发现表格视图确实在 CFRunLoopRunSpecific 中的某个时间点显示在屏幕上,但然后我单击“继续”并且我的加载视图接管了屏幕。
我完全感到困惑。请帮忙!!非常感谢您的洞察力。
相关代码:
RootViewControllerMethods(这是这个 TableView 项目的基础视图)
- (void)viewDidLoad {
//Start the Current Location controller as soon as the program starts. The Controller calls delegate methods
//that will update the list and refresh
[MyCLController sharedInstance].delegate = self;
[[MyCLController sharedInstance].locationManager startUpdatingLocation];
lv = [[LoadingViewController alloc] initWithNibName:@"Loading" bundle:nil];
[self.navigationController pushViewController:lv animated:YES];
[super viewDidLoad];
}
- (void)updateClicked {
//When the location is successfully updated the UpdateCells method will stop the CL manager from updating, so when we want to update the location
//all we have to do is start it up again. I hope.
[[MyCLController sharedInstance].locationManager startUpdatingLocation];
[self.navigationController pushViewController:lv animated:YES];
//LV is a class object which is of type UIViewController and contains my spinning globe/loading view.
}
-(void)updateCells {
//When the Core Location controller has updated its location it calls this metod. The method sends a request for a JSON dictionary
//to trailbehind and stores the response in the class variable jsonArray. reloadData is then called which causes the table to
//re-initialize the table with the new data in jsonArray and display it on the screen.
[[MyCLController sharedInstance].locationManager stopUpdatingLocation];
if(self.navigationController.visibleViewController != self) {
self.urlString = [NSString stringWithFormat:@"http://www.trailbehind.com/iphone/nodes/%@/%@/2/10",self.lat,self.lon];
NSURL *jsonURL = [NSURL URLWithString:self.urlString];
NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];
NSLog(@"JsonData = %@ \n", jsonURL);
self.jsonArray = [jsonData JSONValue];
[self.tableView reloadData];
[self.navigationController popToRootViewControllerAnimated:YES];
[jsonData release];
}
}
CLController 方法:基本上只是将所有数据直接发送回 RootViewController
// Called when the location is updated
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"New Location: %@ \n", newLocation);
NSLog(@"Old Location: %@ \n", oldLocation);
@synchronized(self) {
NSNumber *lat = [[[NSNumber alloc] init] autorelease];
NSNumber *lon = [[[NSNumber alloc] init] autorelease];
lat = [NSNumber numberWithFloat:newLocation.coordinate.latitude];
lon = [NSNumber numberWithFloat:newLocation.coordinate.longitude];
[self.delegate noteLat:lat];
[self.delegate noteLon:lon];
[self.delegate noteNewLocation:newLocation];
[self.delegate updateCells];
}
}