user user - 29 days ago 13
Objective-C Question

file downloading with UIActivityIndicatorView

I use this code for download file in my app directory. But I have problem.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"file.mp3"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:false];
if (!fileExists) {

UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"1"
message:@"1"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* actionAdd = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{

[alert dismissViewControllerAnimated:YES completion:nil];


UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.frame = CGRectMake(0, 0, 24, 24);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = spinner;
[spinner startAnimating];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{



NSString *stringURL = @"link";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];


if (!fileExists) {
[self performSegueWithIdentifier: @"Segue" sender: self];
}
});
}];
UIAlertAction* actionCancel = [UIAlertAction
actionWithTitle:@"cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];


}];
[alert addAction:actionAdd];
[alert addAction :actionCancel];
alert.popoverPresentationController.sourceView = self.view;
alert.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 3.4, self.view.bounds.size.height / 4.0, 1.0, 1.0);
[self presentViewController:alert animated:YES completion:nil];
}
if (fileExists) {
[self performSegueWithIdentifier: @"Segue" sender: self];
}


————————————————————————————————————————————————————————————————————————————————————————

When file was downloaded in directory UIActivityIndicatorView work about several minutes. How to fix it?

Answer

it was hard to diagnoze the issue , try this code if this helps you solve your problem

i edited your dispatch_async.. code only

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *stringURL = @"link";
        NSURL  *url = [NSURL URLWithString:stringURL];
        NSData *urlData = [NSData dataWithContentsOfURL:url];
        [urlData writeToFile:filePath atomically:YES];

        dispatch_async(dispatch_get_main_queue(), ^{
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.accessoryView = nil;
            fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:false];

            if (fileExists) {
                [self performSegueWithIdentifier: @"Segue" sender: self];
            }
        });

    });
Comments