Author: Sun Dongfeng)
Data persistence on all mobile development platforms is a very important part: In j2s, It is rms or stored in the application directory, in symbian, it can be stored in the corresponding disk directory and in the database. In symbian, due to permission authentication, most 3rd users can only access private directories of applications or shared directories of other systems. On the iphone, apple has made great strides and provided a variety of data persistence methods. I will explain them in detail one by one.
The data persistence method provided by the iphone can be divided into three parts from the data storage method: attribute list, object archiving, Embedded Database (SQLite3), and other methods.
I. Attributes list NSUserDefaults
The use of the NSUserDefaults class is similar to that of the NSKeyedArchiver class. However, according to the definition of NSUserDefaults, NSUserDefaults inherits from NSObject and NSKeyedArchiver inherits from NSCoder. This means that NSKeyedArchiver is actually an archive persistence class. You can use the [encodeObject: (id) objv forKey :( NSString *) key] method of the NSCoder class to store data persistently.
-(Void) applicationDidFinishLaunching :( UIApplication *) application {
NSString * strOne = @ "Persistent data1 ";
NSString * strTwo = @ "Persistent data 2 ";
NSMutableArray * persistentArray = [[NSMutableArray alloc] init];
[PersistentArray addObject: strOne];
[PersistentArray addObject: strTwo];
// Archive
NSUserDefaults * persistentDefaults = [NSUserDefaults standardUserDefaults];
[PersistentDefaults setObject: persistentArray forKey: @ "myDefault"];
NSString * descriptionDefault = [persistentDefaults description];
NSLog (@ "NSUserDefaults description is: % @", descriptionDefault );
// Unarchive
NSArray * UnpersistentArray =
[PersistentDefaults objectForKey: @ "myDefault"];
NSString * UnstrOne = [UnpersistentArray objectAtIndex: 0];
NSString * UnstrTwo = [UnpersistentArray objectAtIndex: 1];
NSLog (@ "UnstrOne =%@, UnstrTwo =%@", UnstrOne, UnstrTwo );
// Override point for customization after application launch
[Window makeKeyAndVisible];
}
2. Object archiving NSKeyedArchiver and NSKeyedUnarchiver
Like the symbian 3rd, the iPhone generates a private directory for each application, which is located in
/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications, and a digital sequence string is generated as the directory name. When each Application is started, this letter and digit string is different from the last time, and the last application directory information is converted. DS_Store hides the file. The file structure of this directory is as follows:
The Documents directory is usually used for persistent data storage. The Documents directory can be obtained through NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserdomainMask, YES). The Code is as follows:
-(Void) applicationDidFinishLaunching :( UIApplication *) application {
NSString * strOne = @ "Persistent data1 ";
NSString * strTwo = @ "Persistent data 2 ";
NSArray * persistentArray = [NSArray arrayWithObjects: strOne, strTwo, nil];
NSArray * pathArray = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSAllDomainsMask, YES );
Int pathLen = [pathArray count];
NSLog (@ "path number is: % d", pathLen );
NSString * filePath;
For (int I = 0; I <pathLen; I ++)
{
FilePath = [pathArray objectAtIndex: I];
NSLog (@ "% d path is: % @", I, filePath );
}
NSString * myFilename = [filePath stringByAppendingPathComponent: @ "myFile. rtf"];
NSLog (@ "myfiles path is: % @", myFilename );
// No files generated in correspond directory now
[NSKeyedArchiver archiveRootObject: persistentArray toFile: myFilename];
// Now the myFile. rtf is generated
// Override point for customization after application launch
[Window makeKeyAndVisible];
}
The second parameter of NSSearchPathForDirectoriesInDomains () is an enumeration value. In the author's test code, only NSUserDomainMask and NSAllDomainsMask can obtain the number of directories as 1 and the remaining values as 0, the output is as follows:
[Session started at 21:30:08 + 0800.]
21:30:10. 516 PersistentExample [763: 207] path number is: 1
21:30:10. 518 PersistentExample [763: 207] 0 path is:/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/C93DC783-F137-4660-AE5A-08C3E11C774B/Documents
21:30:10. 521 PersistentExample [763: 207] myfiles path is:/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/C93DC783-F137-4660-AE5A-08C3E11C774B/Documents/myFile. rtf
Terminating in response to SpringBoards termination.
[Session started at 21:32:27 + 0800.]
21:32:30. 091 PersistentExample [803: 207] path number is: 1
21:32:30. 092 PersistentExample [803: 207] 0 path is:/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/763E6772-E754-452F-8532-80C2CE4466B5/sources ents
21:32:30. 100 PersistentExample [803: 207] myfiles path is:/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/Documents/myFile. rtf
Terminating in response to SpringBoards termination.
The printed result is as follows. The directory names of the numeric string generated each time the application starts are different. Before calling the [NSKeyedArchiver archiveRootObject: persistentArray toFile: myFilename] method, the file myFile. rtf is generated and the corresponding file is generated only after this method is called.
Next, we need to read the data from the attribute list. In the code above, I use NSArray to save the data. However, in most applications, the data size is not fixed. In this case, you need to use NSMutalbeArray to dynamically Save the data. The code optimization is as follows:
-(Void) applicationDidFinishLaunching :( UIApplication *) application {
NSString * myFilename;
// Archive
{
NSString * strOne = @ "Persistent data1 ";
NSString * strTwo = @ "Persistent data 2 ";
NSMutableArray * persistentArray = [[NSMutableArray alloc] init];
[PersistentArray addObject: strOne];
[PersistentArray addObject: strTwo];
NSArray * pathArray = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSAllDomainsMask, YES );
Int pathLen = [pathArray count];
NSLog (@ "path number is: % d", pathLen );
NSString * filePath;
For (int I = 0; I <pathLen; I ++)
{
FilePath = [pathArray objectAtIndex: I];
NSLog (@ "% d path is: % @", I, filePath );
}
MyFilename = [filePath stringByAppendingPathComponent: @ "myFile. rtf"];
NSLog (@ "myfiles path is: % @", myFilename );
[NSKeyedArchiver archiveRootObject: persistentArray toFile: myFilename];
}
// Unarchive
{
NSArray * unarchiveArray = [NSKeyedUnarchiver unarchiveObjectWithFile: myFilename];
NSString * UnstrOne = [unarchiveArray objectAtIndex: 0];
NSString * UnstrTwo = [unarchiveArray objectAtIndex: 1];
NSLog (@ "UnstrOne =%@, UnstrTwo =%@", UnstrOne, UnstrTwo );
}
// Override point for customization after application launch
[Window makeKeyAndVisible];
}
The output result is as follows:
[Session started at 22:41:57 + 0800.]
22:41:59. 344 PersistentExample [1082: 207] path number is: 1
22:41:59. 346 PersistentExample [1082: 207] 0 path is:/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/055CD17C-864E-4A83-ABF0-5F01EE85BD5A/Documents
22:41:59. 355 PersistentExample [1082: 207] myfiles path is:/Users/sundfsu