IOS development diary 14-NSFileManager (sandbox and clear cache), ios sandbox Cache

Source: Internet
Author: User

IOS development diary 14-NSFileManager (sandbox and clear cache), ios sandbox Cache

Today, the blogger has a file management requirement and encountered some difficulties. I would like to share with you the hope that we can make common progress.

IOS sandbox mechanism. Applications can only access files in their app directories. Unlike Android, iOS does not have an SD card and cannot directly access images, videos, and other content. Content generated by iOS apps, such as files and cached content, must be stored in your sandbox. By default, each sandbox contains three folders: Documents, Library, and tmp. The Library contains the Caches and Preferences directories.

Documents: Apple recommends that you store the files created by the program and the file data generated by application browsing in this directory. This directory will be included during iTunes backup and recovery.

Library: the default setting of the storage program or other status information;

Library/Caches: stores cached files and stores the persistent data of applications. It is used for data storage after an application upgrade or application is disabled and won't be synchronized by itunes. Therefore, to reduce the synchronization time, you can consider putting some large files that do not need to be backed up into this directory.

Tmp: provides a place to create temporary files in real time, but does not need to be persisted. After the application is closed, the data in this directory will be deleted, or the system may clear the data when the program is not running.

 

The comments of the Code are still detailed. I will post the code and hope you will try to figure it out.

1. File Operations

# Pragma mark --------- NSFileMnager file management class

// Concatenate the file path for storing data

NSString * documentPath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES). lastObject;

// 1. Create a folder

// Splicing path (under which folder is a new folder, folder name)

NSString * activitypath = [documentPath stringByAppendingPathComponent: @ "activitypath"];

// Create a folder

[[NSFileManager defaultManager] createDirectoryAtPath: activitypath withIntermediateDirectories: YES attributes: nil error: nil];

# Pragma mark ---- store complex object files ---- (Complex Object> archive> NSData> splice file path> writeToFile)

NSLog (@ "% ld", _ activity. wisher_count );

// Create a variable NSMutableData object to store the data after the person object is archived

NSMutableData * activityData = [NSMutableData data];

// Create an archive Tool Object

NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: activityData] autorelease];

// Start archiving the activity (activity-> NSData)

[Archiver encodeObject: _ activity forKey: @ "activity"];

# Warning ----------

// When the archive ends, you must call finishEncoding.

[Archiver finishEncoding];

// After the conversion is completed, activityData stores the NSData type data after the activity object is archived.

// Splicing path

NSString * ActivityPath = [activitypath stringByAppendingPathComponent: _ activity. title];

// Determine whether a file exists

// ①. Determine whether the required file does not exist

BOOL flag = [[NSFileManager defaultManager] fileExistsAtPath: ActivityPath];

NSLog (@ "flag = % d", flag );

// If the file exists,

If (flag ){

NSLog (@ "ActivityPath = % @", ActivityPath );

} Else {

// Write the activitydatadata behind the archive to activity.txt

[ActivityData writeToFile: ActivityPath atomically: YES];

NSLog (@ "ActivityPath = % @", ActivityPath );

}

2. Clear Cache

// Use GCD

Dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {

// Locate the cache path

NSString * path = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) lastObject];

// The text to be cleared

NSArray * files = [[NSFileManager defaultManager] subpathsAtPath: path]; // returns an array of all files in this path

For (NSString * p in files ){

NSError * error = nil;

NSString * cachPath = [path stringByAppendingPathComponent: p];

If ([[NSFileManager defaultManager] fileExistsAtPath: cachPath]) {

[[NSFileManager defaultManager] removeItemAtPath: cachPath error: & error]; // Delete

}

}

});

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.