0

I have a UIProgressBar which is updated in a HTML Parsing for loop. But it is not updating smoothly. I want it to show a smooth increase. I have tried to put in async_dispatch but then it didnt update at all. Here is my code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self performSelectorOnMainThread:@selector(getWeeklyProgram) withObject:nil waitUntilDone:NO];
}

-(void)getWeeklyProgram 
{
   NSURL *programURL = [NSURL URLWithString:@"http://www.mostlifeclub.com/studyo-programlari/"];
   NSData *programHtmlData = [NSData dataWithContentsOfURL:programURL];

   TFHpple *programHTMLParser = [TFHpple hppleWithHTMLData:programHtmlData];
   NSString *activitiewXpathQueryString = @"//div[@class='wpb_row vc_row-fluid prk_full_width prk_section parallaxed']/div[@class='prk_inner_block columns centered']/div[@class='vc_span12 wpb_column column_container']/div[@class='wpb_wrapper']/div[@class='wpb_tabs wpb_content_element']//div[@class='wpb_wrapper wpb_tour_tabs_wrapper ui-tabs vc_clearfix']/div[@class='wpb_tab ui-tabs-panel wpb_ui-tabs-hide vc_clearfix']";
   NSArray *activityNodes = [programHTMLParser searchWithXPathQuery:activitiewXpathQueryString];
   NSMutableArray *activities = [[NSMutableArray alloc] init];

   //for each node
   for (TFHppleElement *element in activityNodes)
   {
        currentProgressCount += 1;

        @try
        {
            //Here I want to increase ProgressBar
            [self increaseProgress:currentProgressCount :activityNodes.count];

            }
         @catch(NSException* ex)
         {

         }
    }
}


-(void)increaseProgress:(int)currentLine:(int)totalLines {

    progressView.progress = (CGFloat)currentLine / (CGFloat)totalLines;
    if(progressView.progress >= 1.0f)
    {
        NSLog(@"bitti");
    }
}
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
birdcage
  • 2,638
  • 4
  • 35
  • 58

1 Answers1

0

Run the For loop and the time intensive stuff in an different Thread, and Update the UI in the main Thread:

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
   // Time Intensive Code
   dispatch_sync(dispatch_get_main_queue(), ^{
       // Update UI
   });
});

You may need to put dispatch_sync... inside the for loop to update the UI

firebug
  • 160
  • 2
  • 5