Can not clean up the memory after deleting all the files from specified directory

1k Views Asked by At

Introduction
in my project, i've to delete folder and it's content(s) so i tried with this accepted answer ClickHere

it works and i thought that the task is over but after deleting a whole folder(Directory)i see that the memory is still allocated but file doesn't exist.
here is my code to delete directory(folder).

-(BOOL)removeItem:(NSString*)name{
    //name: is directory(folder)'s name
    NSString*path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    path = [path stringByAppendingPathComponent:@"Normal"];
    path = [path stringByAppendingPathComponent:name];
    NSError* err;
    NSArray* list = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:path error:&err];
    if(err){
        NSLog(@"\n##CONTENTS OF DIRECTORY ERROR:->\n%@",err.localizedDescription);
    }
    //if array's count is not zero(0) it means directory has files available.
    if(list.count >= 1){
        for(NSString* string in list){
            [[NSFileManager defaultManager]removeItemAtPath:[path stringByAppendingPathComponent:string] error:&err];
            if(err){
                NSLog(@"\n##FOLDER IN FOLDER ERROR:->\n%@",err.localizedDescription);
            }
        }
        [[NSFileManager defaultManager]removeItemAtPath:path error:&err];
        return YES;
    }else{
        [[NSFileManager defaultManager]removeItemAtPath:path error:&err];
        if (err) {
            return NO;
        }
        return YES;
    }
}


thanks in advance.

1

There are 1 best solutions below

3
On BEST ANSWER

Alright, i got the issue.in my case the issue was a temp folder(directory). when i add video to my app, iOS created some temporary files in app's temp folder(directory). so, app's size i seen in setting->General->Usage->Manage Storage after removing the files(videos) from my app , was the size of temp folder.
so, when you import photos or videos from Gallery you have to manually clear the temp folder after fetching. i'm clearing the temp folder from UIImagePickerController's delegate as bellow.

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{
[self dismissViewControllerAnimated:YES completion:^{
BOOL success = [videoData writeToFile:path atomically:YES];
            NSLog(@"Successs:::: %@", success ? @"YES" : @"NO");
            [RWTAppDelegate clearTmpDirectory];
}];
}


+ (void)clearTmpDirectory
{
    NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
    for (NSString *file in tmpDirectory) {
        [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
    }
}


Update for swift 3+

class func clearTmpDirectory(){
        let path = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
        let manager = FileManager.default
        let files = try? manager.contentsOfDirectory(atPath: path.path)
        files?.forEach { (file) in
            let temp = path.appendingPathComponent(file)
            try? manager.removeItem(at: temp)
            // --- you can use do{} catch{} for error handling ---//
        }
}


sorry for my bad english.