iOS Development journal 14-nsfilemanager (sandbox and clear cache)

Source: Internet
Author: User

Today Bo Master has a document management needs, encountered some difficulties, here and we share, hope to progress together.

The sandbox mechanism for iOS, apps can only access files in their own app directory. Unlike Android, iOS does not have an SD card concept and does not have direct access to images, videos, and other content. Content generated by iOS apps, like, files, cached content, and so on, must be stored in their own sandbox. By default, each sandbox contains 3 folders: Documents, Library, and TMP. The library contains caches, preferences directories.

Documents: Apple recommends that the files generated by the program creation and the file data generated by the app's browsing be saved in this directory, which is included in itunes backup and restore

Library: The default settings or other status information of the stored program;

Library/caches: Store the cache file, save the persisted data of the application, use it to apply the upgrade or save the data after the app is closed, and not be synced by itunes, so in order to reduce the time of synchronization, consider putting some larger files into this directory without needing backup.

TMP: Provides an immediate place to create temporary files, but does not need to be persisted, after the application is closed, the data in the directory will be deleted, or the system may be cleared when the program is not running

The code comments are detailed, I put the code out, I hope everyone to figure out

1. File operation

#pragma mark---------Nsfilemnager managing file Classes

File path for stitching data

NSString *documentpath=nssearchpathfordirectoriesindomains (NSDocumentDirectory, NSUserDomainMask, YES). LastObject ;

1. Create a folder

Stitching path (under which folder to create a new folder, the name of the folder)

NSString *activitypath=[documentpath stringbyappendingpathcomponent:@ "Activitypath"];

Create a folder

[[Nsfilemanager Defaultmanager] Createdirectoryatpath:activitypath withintermediatedirectories:yes attributes:nil Error:nil];

#pragma mark----The complex object store file----(complex object, archive->nsdata-> stitching file path->writetofile)

NSLog (@ "%ld", _activity.wisher_count);

Create a mutable Nsmutabledata object that holds 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 activity (activity-NSData)

[Archiver encodeobject:_activity forkey:@ "activity"];

#warning----------

When the archive is finished, be sure to call finishencoding

[Archiver finishencoding];

Completed the conversion, at this time Activitydata storage is the activity object is archived after the NSData type data

Stitching Path

NSString *activitypath=[activitypath Stringbyappendingpathcomponent:_activity.title];

Determine if a file exists

①. Determine if 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 Activitydata data after archive to Activity.txt

[Activitydata Writetofile:activitypath Atomically:yes];

NSLog (@ "activitypath=%@", Activitypath);

}

2. Clear the Cache

Using GCD

Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{

Find the path where the cache is stored

NSString * Path = [Nssearchpathfordirectoriesindomains (Nscachesdirectory,nsuserdomainmask, YES) lastObject];

To clear the text

Nsarray * files = [[Nsfilemanager defaultmanager]subpathsatpath:path];//returns an array of all files under 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

}

}

});

iOS Development journal 14-nsfilemanager (sandbox and clear cache)

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.