2011-12-19 44 views
0

在我的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 
+0

loadNewData是做什麼的?給我們那個代碼。 – vikingosegundo 2011-12-19 11:17:36

+0

我想我只是找到了問題,它真的看起來像下面的方法的東西 - 要麼insertNewData或loadNewData,但仍然不知道那裏發生了什麼。 你可能是對的! – e2l3n 2011-12-19 11:24:24

回答

1
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:urlStr]]; 
NSData *dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

的問題是代碼,您正在執行主線程同步網絡請求。

這意味着UI將完全阻塞,直到請求完成。
你將不得不這樣做異步。你會在StackOverflow上找到很多代碼。

+0

非常感謝! – e2l3n 2011-12-19 11:39:37