user1237473
user1237473

Reputation: 69

How to update from UIProgressBar on main thread in iOS

i am using one method to download video files inside document directory and it worked perfectly fine, I am using below code which is downloading video in background, however while downloading i need to update progress bar also as how much is downloaded, however main thread is not updating.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    NSData *data = [NSData dataWithContentsOfURL: videoURL];

    NSString *pathToDocs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filename = [NSString stringWithFormat:@"%@.mp4",self.titleString];
    [data writeToFile:[pathToDocs stringByAppendingPathComponent:filename] atomically:YES];
    NSLog(@"File %@ successfully saved", filename);

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Succesfully Downloaded" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];

});

Upvotes: 0

Views: 439

Answers (3)

Rob
Rob

Reputation: 438297

To be able to monitor the progress of a download, you should consider using NSURLConnection, where you can use the NSURLConnectionDataDelegate method didReceiveData to update your UI regarding the progress.

If you want to know how much data is expected, you can often use didReceiveResponse, which may, for HTTP URLs, contain the expected file size. (Not all web servers provide that, though.)

As Richard suggested, you may want to look into using AFNetworking, as it can simplify your programming effort.

Upvotes: 0

βhargavḯ
βhargavḯ

Reputation: 9836

Try to update Progress on main thread like this,

     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSData *data = [NSData dataWithContentsOfURL: videoURL];

        NSString *pathToDocs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *filename = [NSString stringWithFormat:@"%@.mp4",self.titleString];
        [data writeToFile:[pathToDocs stringByAppendingPathComponent:filename] atomically:YES];
        NSLog(@"File %@ successfully saved", filename);

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Succesfully Downloaded" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];

        dispatch_async(dispatch_get_main_queue(), ^(){
            //Task you want perform on mainQueue
            //Control Progressbar here

        });
    });

Upvotes: 2

Richard Brown
Richard Brown

Reputation: 11444

dataWithContentsOfURL won't return a status; it simply blocks the thread until the complete download is done. You need to use something asynchronous that reports progress.

Take a look at AFNetworking (https://github.com/AFNetworking/AFNetworking#file-upload-with-progress-callback) as it's designed to do what you're looking for.

Upvotes: 1

Related Questions