0

在我的应用程序中,我想从one directory to another directory. 在源目录中有文件和文件夹。

我在下面的代码中这样做:

- (BOOL)copyDirectory:(NSString*)Directory toDirectory:(NSString*)targetDirectory
{
    NSLog(@"start copy");
    @try
    {
        NSError *error = nil;
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) objectAtIndex:0];
        NSString *folderPath = [path stringByAppendingPathComponent:targetDirectory];
        BOOL succes = [[NSFileManager defaultManager] fileExistsAtPath:folderPath];
        if(succes){
            return NO;
        }
        else
        {
            NSString *wwwPath = [[NSBundle mainBundle] pathForResource:Directory ofType:nil];
            NSArray *target= [[NSFileManager defaultManager] contentsOfDirectoryAtPath:wwwPath error:&error];

            [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:NO attributes:nil error:nil];
            int a = 0 ;
            for(a = 0 ; a < target.count;a++){
                NSString *temp = [target objectAtIndex:a];
                if([temp rangeOfString:@"."].location == NSNotFound)
                {
                    NSString *sourceDirectory = [NSString stringWithFormat:@"%@/%@",Directory,temp ];
                    NSString *DestanistionDirectory = [NSString stringWithFormat:@"%@/%@",targetDirectory,temp];

                    if([self copyDirectory:sourceDirectory toDirectory:DestanistionDirectory] == NO){
                        return NO;
                    }
                }
                else
                {
                    NSString *source = [NSString stringWithFormat:@"%@/%@",wwwPath,temp];
                    NSString *target = [NSString stringWithFormat:@"%@/%@",folderPath,temp];

                    BOOL result = [[NSFileManager defaultManager] copyItemAtPath:source toPath:target error:&error];
                    if(result)
                        currentFileCounter++;
                    [self performSelectorInBackground:@selector(updatePrograsBar) withObject:nil];
                    if(error != nil){
                        if(IsDEBUG) NSLog(@"error: %@",[error description]);
                        return NO;
                    }
                }
            }
        }
    }
    @catch (NSException *exception)
    {
        if(IsDEBUG)NSLog(@"%@",exception.description);
        return NO;
    }
    NSLog(@"end copy");
    return YES;
}

为了节省时间,当我打电话给

- (BOOL)copyDirectory:(NSString*)Directory to Directory:(NSString*)target Directory 

在递归中我调用新线程。

- (BOOL)copyDirectory:(NSString*)Directory toDirectory:(NSString*)targetDirectory
{
    NSLog(@"start copy");
    @try
    {
        NSError *error = nil;
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) objectAtIndex:0];
        NSString *folderPath = [path stringByAppendingPathComponent:targetDirectory];
        BOOL succes = [[NSFileManager defaultManager] fileExistsAtPath:folderPath];
        if(succes){
            return NO;
        }
        else
        {
            NSString *wwwPath = [[NSBundle mainBundle] pathForResource:Directory ofType:nil];
            NSArray *target= [[NSFileManager defaultManager] contentsOfDirectoryAtPath:wwwPath error:&error];

            [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:NO attributes:nil error:nil];
            int a = 0 ;
            for(a = 0 ; a < target.count;a++){
                NSString *temp = [target objectAtIndex:a];
                if([temp rangeOfString:@"."].location == NSNotFound)
                {
                    NSString *sourceDirectory = [NSString stringWithFormat:@"%@/%@",Directory,temp ];
                    NSString *DestanistionDirectory = [NSString stringWithFormat:@"%@/%@",targetDirectory,temp];

                    dispatch_async(dispatch_get_global_queue(0, 0), ^{


                        [self copyDirectory:sourceDirectory toDirectory:DestanistionDirectory];

                    });                   
                }
                else
                {
                    NSString *source = [NSString stringWithFormat:@"%@/%@",wwwPath,temp];
                    NSString *target = [NSString stringWithFormat:@"%@/%@",folderPath,temp];

                    BOOL result = [[NSFileManager defaultManager] copyItemAtPath:source toPath:target error:&error];
                    if(result)
                        currentFileCounter++;
                    [self performSelectorInBackground:@selector(updatePrograsBar) withObject:nil];
                    if(error != nil){
                        if(IsDEBUG) NSLog(@"error: %@",[error description]);
                        return NO;
                    }
                }
            }
        }
    }
    @catch (NSException *exception)
    {
        if(IsDEBUG)NSLog(@"%@",exception.description);
        return NO;
    }
    NSLog(@"end copy");
    return YES;
}

但我需要知道何时完成复制所有文件和文件夹,而不是从主调用返回

-(BOOL)copyDirectory:(NSString*)Directory to Directory:(NSString*)target Directory

直到完成所有。

4

0 回答 0