3

I copy files then my application is finish launching from resource to caches directory. but I want to update my progress bar during copying the files.

I copy the file with the code below:

-(BOOL)copyDirectory:(NSString*)source toDirectory:(NSString*)targat
{
    BOOL retVal = YES;
    NSFileManager *fileManager = [[NSFileManager alloc] init];

    NSError *error;
    if ([fileManager fileExistsAtPath:targat] == YES)
    {
        NSError *error = nil;
        [fileManager removeItemAtPath:targat error:&error];
    }


    if(![fileManager copyItemAtPath:source
                             toPath:targat
                              error:&error])
    {
        NSLog(@"Error copy files: %@", [error description]);
        retVal = NO;
    }
    [fileManager release];

    return retVal;
}

I can not think about a good idea, how to update the progress bar According to the progress of copying files.

icodebuster
  • 8,890
  • 7
  • 62
  • 65
Guy Kahlon
  • 4,510
  • 4
  • 30
  • 41

1 Answers1

10

In high level you may approach the following :

  1. Run your copying process in a seperate thread (P1)
  2. Run another thread (P2) which reads periodically (say every 100ms) the destination file current_size.
  3. Calculate current_size / total_size
  4. Update you progress bar UI element

Then you just want to find out the size of a file, you don't actually have to open it. Just use NSFileManager like this:

NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:self.finalPath error:NULL];
unsigned long long fileSize = [attributes fileSize]; // in bytes
icodebuster
  • 8,890
  • 7
  • 62
  • 65
  • 3
    Do you have sample code that demonstrates how to copy in a separate thread that tells you when finished copying – Guy Kahlon Jul 09 '13 at 11:55
  • I know this is almost a decade later, and this is still an issue trying to get a file move or copy progress. I'm trying to push the actual process to a background thread and using a timer to observe the destination file's size. But the file size is not being reported until the copy/move is complete. – SouthernYankee65 Mar 18 '22 at 18:26