在我的iPhone应用程序中,我有一个更新按钮,当按下时添加一个新的子视图。这个新视图覆盖整个屏幕,并且只有在整个更新操作完成时才能访问表视图。这是实际上这个想法,但我的实现有以下问题 - 视图显得太慢,需要2或3秒才能弹出。加载视图显得太慢
我认为可能的解决方案是启动一个新线程并同步它,但我不确定这是否会有所帮助。
- (IBAction) updateButtonPressed: (id)sender {
self.updateButton.enabled = NO;
//HERE STARTS THE LOADING VIEW
LoadingView * loadingView =[LoadingView initializeLoadingView:[self.view.window.subviews objectAtIndex:0]];
//HERE STARTS THE LOADING VIEW
[self deleteData];//delete old data
[self insertNewData];
[self loadNewData];
//THE LOADING VIEW IS DISMISSED
[loadingView dismissView];
//THE LOADING VIEW IS DISMISSED
self.updateButton.enabled = YES;
}// updateButtonPressed
视图必须在动作开始后立即显示,因为否则整个程序逻辑将变为地狱。
编辑: 这里是这两种方法insertNewData和loadNewData
- (void) insertNewData
{
NSString *urlStr;
NSError *error;
NSURLResponse *response;
if(self.title == @"New York")
{
urlStr =[[NSString alloc] initWithFormat: @"http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=New_York,IL"];
}
else
urlStr = [NSString stringWithFormat:@"http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=%@,IL",self.title];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:urlStr]];
NSData *dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
xmlControl = [[XMLController alloc] loadXMLbyData:dataReply];
Forecast *forecast;
for(ForecastPeriod *newForecast in [xmlControl weatherForecasts])
{
forecast = [[Forecast alloc] initWithEntity:[NSEntityDescription entityForName:@"Forecast" inManagedObjectContext:self.managedObjectContext] insertIntoManagedObjectContext:self.managedObjectContext];
[forecast setValue:newForecast.conditions forKey:@"conditions"];
[forecast setValue:newForecast.day forKey:@"day"];
[forecast setValue:newForecast.icon forKey:@"icon"];
[self addImagesAtSpecificDirectory:newForecast.icon];
[forecast setValue:newForecast.max forKey:@"max"];
[forecast setValue:newForecast.min forKey:@"min"];
[forecast setValue:newForecast.month forKey:@"month"];
[forecast setValue:newForecast.period forKey:@"period"];
[forecast setValue:newForecast.weekday forKey:@"weekday"];
[forecast setValue:newForecast.year forKey:@"year"];
[forecast setValue:self.title forKey:@"location"];
}
if (![self.managedObjectContext save:&error]) {
NSLog(@"Couldn't save: %@", [error localizedDescription]);
}
}//insertNewData
- (void) loadNewData
{
AppDelegate *appDelegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = appDelegate. managedObjectContext;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Forecast" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"period" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"location = %@",self.title];
[request setPredicate:predicate];
NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
self.items = mutableFetchResults;
[self.tableView reloadData];
[mutableFetchResults release];
[request release];
//iPad
if ([[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPad && popOver_ != nil)
{
[popOver_ dismissPopoverAnimated:YES];
}
}//loadNewData
loadNewData是做什么的?给我们那个代码。 – vikingosegundo 2011-12-19 11:17:36
我想我只是找到了问题,它真的看起来像下面的方法的东西 - 要么insertNewData或loadNewData,但仍然不知道那里发生了什么。 你可能是对的! – e2l3n 2011-12-19 11:24:24