Persistence is the saving of data to the hard disk so that the data can be accessed after the application or machine restarts. There are many ways to persist data in iOS development, and I’ll try to introduce five of them:
- Plist file (serialization)
- Preference
- NSKeyedArchiver (archive)
- SQLite3
- CoreData
In this section I’ll just write about archiving, preferences, and property lists, which are common ways of persisting small data. SQLite3 and CoreData are both ways of storing big data in databases, and I’ll write about them later.
Before introducing the various storage methods, it is necessary to explain the sandbox mechanism. By default, iOS programs can only access their own directory, which is called a “sandbox.”
Since the sandbox is a folder, let’s see what’s inside. The directory structure of the sandbox is as follows:
- Application package: Holds the source files of an application, including resource files and executables
NSString *path = [[NSBundle mainBundle] bundlePath];
Copy the code
- Documents: The most commonly used directory. The contents of this folder are synchronized when iTunes synchronizes the application. It is suitable for storing important data
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory.NSUserDomainMask.YES).firstObject;
Copy the code
Library
- Caches: iTunes doesn’t sync this folder, making it great for non-essential data that is large and doesn’t need to be backed up.
NSSearchPathForDirectoriesInDomains(NSCachesDirectory.NSUserDomainMask.YES).firstObject;
Copy the code
- Preferences: iTunes syncs the contents of this folder when it syncs the app, usually holding the app’s Settings.
- TMP: iTunes does not synchronize this folder. Files in this directory may be deleted before the application is running. Therefore, this directory is suitable for storing temporary files in the application.
NSString *path = NSTemporaryDirectory(a);Copy the code
Plist files are files that store specific classes in directories as XML files. ###### can be serialized of only the following types:
NSArray; / / array NSMutableArray; // NSDictionary; / / dictionary NSMutableDictionary; // mutable dictionary NSData; // Binary data NSMutableData; // variable binary data NSString; // String NSMutableString; // mutable string NSNumber; // Basic data NSDate; / / dateCopy the code
###### Using NSArray as an example, the other classes are used in exactly the same way:
- (void)writeToPlist{
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory.NSUserDomainMask.YES).firstObject;
NSString *fileName = [path stringByAppendingPathComponent:@"123.plist"];
NSArray *array = @[@ "1234".@"tony".@"java"];
// serialize the array to a plist file
[array writeToFile:fileName atomically:YES];
}
- (NSArray *)readFromPlist{
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory.NSUserDomainMask.YES).firstObject;
NSString *fileName = [path stringByAppendingPathComponent:@"123.plist"];
// Deserialize the data from the plist file into an array
NSArray *result = [NSArray arrayWithContentsOfFile:fileName];
NSLog(@ "% @", result);
return result;
}
Copy the code
The writeToFile:atomically: method is used for storage. Atomically indicates whether to write a secondary file first and then copy the secondary file to the target file address. This is a safer way to write to a file, usually YES.
Preference Settings are specifically used to save the configuration information of the application. Generally, do not save other data in Preference Settings. Preferences are stored and read as key-values, using a singleton object called NSUserDefaults ###### :
//1. Get the NSUserDefaults file
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//2. Write to preferences
[userDefaults setObject:@"AAA" forKey:@"a"];
[userDefaults setBool:YES forKey:@"sex"];
[userDefaults setInteger:21 forKey:@"age"];
//2.1 Immediate synchronization
[userDefaults synchronize];
//3. Read preferences
NSString *name = [userDefaults objectForKey:@"a"];
BOOL sex = [userDefaults boolForKey:@"sex"];
NSInteger age = [userDefaults integerForKey:@"age"];
NSLog(@"%@, %d, %ld", name, sex, age);
Copy the code
- If not called
synchronize
Method is saved to a file at any time depending on the I/O situation. So if you need to write to the file immediately, it must be calledsynchronize
Methods.
- Preferences will save all data in the same file and use the same
key
The stored data overwrites the previously stored data.
Archiving in iOS is another form of serialization, as long as the NSCoding protocol objects can be serialized through it. Because most classes that support storing data follow the NSCoding protocol, archiving is relatively easy for most classes to implement. ####1. Follow the NSCoding protocol ###### The NSCoding protocol states that two methods must be implemented:
EncodeWithCoder: Used to show how to encode objects into an archive. InitWithCoder: Used to show how to unfile to get a new object.
//1. Follow the NSCoding protocol
@interface Person : NSObject <NSCoding>
//2. Set the properties
@property (strong.nonatomic) UIImage *avatar;
@property (copy.nonatomic) NSString *name;
@property (assign.nonatomic) NSInteger age;
@end
@implementation Person
/ / solution
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
self.avatar = [aDecoder decodeObjectForKey:@"avatar"];
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
/ / archive
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.avatar forKey:@"avatar"];
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
}
@end
Copy the code
# # # # 2. NSKeyedArchiver archive call NSKeyedArchiver archiveRootObject: toFile: method of the object file
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory.NSUserDomainMask.YES).firstObject;
NSString *file = [path stringByAppendingPathComponent:@"person.data"];
Person *person = [[Person alloc] init];
person.avatar = [UIImage imageNamed:@"icon.png"];
person.name = @"Xiao Ming";
person.age = 17;
[NSKeyedArchiver archiveRootObject:person toFile:file];
Copy the code
####3. NSKeyedUnarchiver Call the unarchiveObjectWithFile: method of NSKeyedUnarchiver to unfile an object from a file
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory.NSUserDomainMask.YES).firstObject;
NSString *file = [path stringByAppendingPathComponent:@"person.data"];
Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
if (person) {
self.avatarView.image = person.avatar;
self.nameField.text = person.name;
self.ageField.text = [NSString stringWithFormat:@"%ld", person.age];
}
Copy the code
- The NSCoding protocol must be followed and implemented
- The extension of the save file can be specified arbitrarily
- Inheritance must first call the parent class’s archive unfile method, which is not necessary here because the parent class is NSObject
These are the three common methods for small data persistence. In the next section, I will continue to write common methods for big data persistence. Here comes a beautiful woman, no picture obsessive-compulsive attack!
###### if you like, please pay attention, if you don’t like, please like, O(∩_∩)O ha!