iOS Clear Cache Detailed Parsing, app File Path Operation, My Personal Understanding of Sandbox

Keywords: xcode snapshot Mobile less

To clear the cache, we first need to know where to clear the cache, so we first need to know something about the files in an app.  

How to view the contents of app files: Open Xcode - > Toolbar Window - > Devices - > Select Devices - > Click on the project (double-click can also be seen directly, wait for 10 seconds to appear) - > Click on that little thing to select Download. Container Put it on the desktop and you can right-click on the contents of the package.



In an app, there is an AppData folder, which contains:
- Documents 
Used to save the files generated by the creation program, and some data generated by the application browsing. When itunes backs up synchronously, it backs up the directory together, which can be used to store some data backed up regularly.  
It's also for this reason that when we develop, it's better not to put some pictures, videos, etc. in this catalogue, because this will take up a lot of cloud space (normally our iCloud is 5G cloud capacity, upgraded local tyrants when I say nothing).  
Method of obtaining the file path:

    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    1
  • Library 
    There are two subdirectories in it.
    Caches and References: Caches are used to save cached application files, which will not be lost after closing the application, and will not be synchronized to iTunes. Larger text can be saved here. Preferences: Save the preferences file for the application, and here's what NSUser Defaults saves.
    The following picture is written for me.
    [[NSUser Defaults standardUser Defaults] setObject:@"Wang Kai" for Key:@"laojingxing"]; after this code, the result seen by plist in Preferences

Method of obtaining the file path:

    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
  • 1
  • 1

SDWebImage stores pictures in Caches. It also generates a folder of turning doors. As shown in the figure above, the file before loading the picture without SDWebImage. The following is the file path diagram after loading a picture.


  • tmp 
    It is used to store temporary files. When the program closes, the contents will be deleted.  
    Method of obtaining the file path:
   NSString *tmpPath = NSTemporaryDirectory();
  • 1
  • 1

Well, after understanding the path structure, we go into the topic-specific cache.  
In general, to do this function, you need to show how big the cache is first, and then click Clearly to divide it. Considering the large files, the file operation will take a long time, so I use GCD to open up multi-threading. Can not force us not force, on the code:

Get cache

+ (void)getCachesSize:(void (^)(NSString *))CompleteBlck
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Get the cache file directory
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        // Get all the files under this path, delete the folder directly when deleting, and can not calculate the size of the folder directly when calculating, so use subpaths AtPath method. Snapshots are just not available. When deleting, you can't delete anything in it, so it happens to be on top.
        NSArray *allSubPathArray = [FileManager subpathsAtPath:path];
        long long cachesSize = 0;
        for (NSString *subPath in allSubPathArray) {
            NSString *subPathString = [path stringByAppendingPathComponent:subPath];
            NSDictionary *subCachesDic = [FileManager attributesOfItemAtPath:subPathString error:nil];
            // The result is that B will eventually be converted to K and then to M.
            cachesSize += subCachesDic.fileSize;
        }

        // Turn to M and keep two decimal places. Note that the time scale of the calculation is 1000, not 1024. That's why some phones say 16G of memory, but the actual capacity is only about 144 gigabytes.
        double doubleCaches = (double)cachesSize / pow(10, 6);
        // It's M, which can be converted into K or G according to the actual situation.
        CompleteBlck([NSString stringWithFormat:@"%.2f", doubleCaches]);
    });

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

Clear cache

+ (void)cleanCaches:(void (^)(BOOL))CompleteBlock
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Get the cache file directory
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        // Get all the files in this path. When deleting, you can delete the folder directly. When calculating, you can't directly calculate the size of the folder.
        BOOL isSuccess = NO;
        NSArray *allSubPathArray = [FileManager subpathsAtPath:path];
        for (NSString *subPath in allSubPathArray) {
            NSString *subPathStrigng = [path stringByAppendingPathComponent:subPath];
            if ([FileManager removeItemAtPath:subPathStrigng error:nil]) {
                NSLog(@"Success");
                isSuccess = YES;
            } else {

                // Snapshots already exist folder can not be deleted, you can go back to see the folder before SDWebImage was not used, caches folder already has Snapshots (snapshot)
                NSLog(@"fail");

            }
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            [self getCachesSize:^(NSString *cacheSize) {
                if ([cacheSize floatValue] == 0) {
                    // There's no caching, and that's a success.
                    CompleteBlock(YES);
                } else {
                    CompleteBlock(isSuccess);
                }
            }];
        });

    });
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

Of course, in the actual development, we will save some videos, pictures and sounds into the file folder we created. When we need to delete the files under the specified folder

+ (void)cleanCachesAtPath:(NSString *)path completeBlock:(void(^)(BOOL isSuccess))CompleteBlock
{
    if (![FileManager isExecutableFileAtPath:path]) {
        // Folder does not exist
//        [[[UIAlertView alloc] initWithTitle:@ "File path does not exist" message:nil delegate:nil cancelButtonTitle:@ "determine" otherButtonTitles:nil, nil] show];
        NSLog(@"Folder does not exist");
        CompleteBlock(NO);
        return;
    }

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        BOOL isSuccess = NO;
        // Gets all files under the specified folder
        NSArray *allSubPathArray = [FileManager subpathsAtPath:path];
        for (NSString *subPath in allSubPathArray) {
            NSString *subPathStrigng = [path stringByAppendingPathComponent:subPath];
            if ([FileManager removeItemAtPath:subPathStrigng error:nil]) {
//                NSLog(@ "Success");
                isSuccess = YES;
            } else {
//                NSLog(@ "Failure");

            }
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            [self getCachesSize:^(NSString *cacheSize) {
                if ([cacheSize floatValue] == 0) {
                    // There's no caching, and that's a success.
                    CompleteBlock(YES);
                } else {
                    CompleteBlock(isSuccess);
                }
            }];
        });

    });
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

Sometimes when the content of mobile phone is too small, it can also be like WeChat That prompts you to clear the cache, pass a less than how many times, return whether prompt or not.

+ (BOOL)remindCleanCache:(long long)lessThanSize
{
    NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    return [[fattributes objectForKey:NSFileSystemFreeSize] longLongValue] > lessThanSize ? NO : YES;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6


Posted by Davy on Sat, 30 Mar 2019 21:00:29 -0700