1

In my iPhone application, I have a button and on the button click I call a web service and getting some links, and after that I need to download the files in those links. On the button click a pop up view will come up and on "OK" click, the button will be replaced with a progress bar and it will show the download progress. And once the download completes, the progress bar will be again replaced by the button. The issue I am having here is, if the internet connection is slow, the UI will blocked and the "Ok" button will be remain in the pressed mode till it completly get the details from the server, only then progress bar will appear. I want to show the progress bar right after the OK button click, it should not lock the UI. I used the following method:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 
                                             (unsigned long)NULL), ^(void) {
        [self download];
    });

in this case, the UI didn't lock, but the progress bar didn't come. And I used

    [self performSelectorInBackground:@selector(download) withObject:nil];

in this case, UI didn't block, but it is showing the download button till it gets the details from the server. Only then it is showing the progress bar.

And the code flow for downloading:

- (void)download{
   //Creating a custom progressview
    myprogressView = [[CustomProgressView alloc]initWithFrame:CGRectMake(25, 138, 100, 28)];
    [cell.contentView addSubview:myprogressView];
    [cell bringSubviewToFront:myprogressView];

    //Calling the methods for getting the details from the server

    //after some internal process, start download
    [self.myQueue go];
}

Please share your ideas

Mithuzz
  • 1,091
  • 14
  • 43

2 Answers2

2

Try not to change the UI in any background thread.. All UI related task should be performed on the main thread.. You can call a delegate method to update the UI as soon as data comes in...

here I can see that you are performing UI related tasks in the background thread... the reason for that is that UIKit is not thread safe... :/

Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
1

First replace your button as progress view. Then start the download. I mean to say, In the button press event change the UI then call your [self.myQueue go];

Vignesh
  • 10,205
  • 2
  • 35
  • 73
  • That is what I am doing now right? And it is working. But it is taking time in the normal case. But having some issues as I mentioned in the question. What changes should I made? Any idea? – Mithuzz Jul 24 '12 at 06:27
  • I meant do the UI separately. Not with in the download function. – Vignesh Jul 24 '12 at 06:31