Reputation: 535
In my iPhone application I have an update button which when being pressed adds a new subview.This new view covers the whole screen and the table view is not accessible until the whole update action is completed.This is actually the idea but I have the following problem with its realization - the view appears too slowly, it takes 2 or 3 seconds to pop up.
I thought that a possible solution would be to start a new thread and synchronize it but I'm not sure if this will help at all.
- (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
The view must show instantly after the action begins because otherwise the whole program logic goes to hell.
EDIT: here is the code for the both methods insertNewData and 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
Upvotes: 0
Views: 172
Reputation: 52227
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:urlStr]];
NSData *dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
The problem is, that you are performing a synchronous network request on the main thread.
This means, that the UI will block entirely, until the request is finished.
You will have to do it asynchronously. You will find plenty of codes on StackOverflow.
Upvotes: 1