简体   繁体   中英

Function call in background thread in iOs

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

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... :/

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM