Iphone 加载视图显示得太慢

Iphone 加载视图显示得太慢,iphone,objective-c,cocoa-touch,uiview,Iphone,Objective C,Cocoa Touch,Uiview,在我的iPhone应用程序中,我有一个更新按钮,按下该按钮时会添加一个新的子视图。这个新视图覆盖整个屏幕,在整个更新操作完成之前无法访问表视图。这实际上是一个想法,但我在实现时遇到以下问题-视图显示太慢,需要2到3秒钟才能弹出 我认为一个可能的解决方案是启动一个新线程并同步它,但我不确定这是否会有帮助 - (IBAction) updateButtonPressed: (id)sender { self.updateButton.enabled = NO; //HERE STA

在我的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
问题是,您正在主线程上执行同步网络请求

这意味着UI将完全阻塞,直到请求完成。

您将必须异步执行此操作。您将在StackOverflow上找到大量代码。

loadNewData的作用是什么?给我们这些代码。我想我刚刚找到了问题所在,它看起来确实与以下方法有关-insertNewData或loadNewData,但仍然无法确定那里发生了什么。你可能是对的!
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:urlStr]];
 NSData *dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];