Raju
Raju

Reputation: 451

slow loading when clicked on table view cell

When i click on table view cell, it will be in the same table view for some time(till the next page is completely load) then it will display the next view..

i want on click on the table view cell, it should immediately goto next page and show the loading page popup..

i have tried with impActivityAgent and also tried to show alert view when it will enter the next page(but view is of previous page ie table view).. but.. its loading the page completely, which will take time and then its showing alert..

in next page i am posting and parsing the data which will take time, during that time i want to show the activity indicator..

i have tried many methods, but still its first loading the next page completely and then displaying the contents or alertView or activity indicator and i am not able to show the activity indicator when clicked on table view cell..

MY CODE:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    nextTableViewController *doc = [[nextTableViewController alloc]initWithNibName:@"nextTableViewController" bundle:nil];
    [self.navigationController pushViewController:doc animated:YES];
}

AND NEXT VIEW CONTROLLER IS :

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[ImpActivityAgent defaultAgent] makeBusy:YES];

NSURL *loadUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@/gmail.com",inputURL]];
htmlData = [NSData dataWithContentsOfURL:loadUrl];
self.htmlSTR = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
[self parseHTML];
}

and in Parse function i am Parsing the content which i got as response in HTML formate using "hpple" Parser..

Upvotes: 3

Views: 1173

Answers (4)

Ganapathy
Ganapathy

Reputation: 4614

Just use one separate thread to handle parsing. Hope this code will help you.

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[ImpActivityAgent defaultAgent] makeBusy:YES];

[NSThread detachNewThreadSelector:@selector(newMethodForParsing) toTarget:self withObject:nil];

}


-(void)newMethodForParsing
{
NSURL *loadUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@/gmail.com",inputURL]];
htmlData = [NSData dataWithContentsOfURL:loadUrl];
self.htmlSTR = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
[self parseHTML];

[tableView reload]; // tableView refers to your table view name

}

Upvotes: 2

aryaxt
aryaxt

Reputation: 77646

Ganapathy's answer leaks memory, you need to place an autoreleasepool inside the method. this is a simpler way to do this. this will run on the main thread but after the existing tasks in the queue.

If you want it to run on a different thread replace with dispatch_get_global_queue()

- (void)viewDidLoad
{
   [super viewDidLoad];
   // Do any additional setup after loading the view from its nib.
   [[ImpActivityAgent defaultAgent] makeBusy:YES];

   dispatch_async(dispatch_get_main_queue(), ^{

      NSURL *loadUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@/gmail.com",inputURL]];
      htmlData = [NSData dataWithContentsOfURL:loadUrl];
      self.htmlSTR = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
      [self parseHTML];
   )
}

Upvotes: 0

Chirag Pipaliya
Chirag Pipaliya

Reputation: 1281

Try it....

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(newMethodForParsing) userInfo:nil repeats:NO];
}

-(void)newMethodForParsing
{
NSURL *loadUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@/gmail.com",inputURL]];
htmlData = [NSData dataWithContentsOfURL:loadUrl];
self.htmlSTR = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
[self parseHTML];
}

Upvotes: 0

ChintaN -Maddy- Ramani
ChintaN -Maddy- Ramani

Reputation: 5164

Try this

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[ImpActivityAgent defaultAgent] makeBusy:YES];
    [self performSelectorInBackground:@selector(start) withObject:Nil];
    //or you can use after delay then no need to use perform selector on main thread in start method.
    [self performSelector:@selector(start) withObject:nil afterDelay:0.5];
}

-(void)start
{
    NSURL *loadUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@/gmail.com",inputURL]];
    htmlData = [NSData dataWithContentsOfURL:loadUrl];
    self.htmlSTR = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
    [self performSelectorOnMainThread:@selector(parseHTML) withObject:Nil waitUntilDone:YES];
}

Upvotes: 1

Related Questions