理解IOS数据持久化

来源:互联网 发布:阿里云免费邮箱入口 编辑:程序博客网 时间:2024/05/17 23:16

iphone提供的数据持久化的方法:

从数据保存的方式上讲可以分为三大部分:

属性列表、

对象归档、

嵌入式数据库(SQLite3)、

其他方法如cocoadata。

一、属性列表NSUserDefaults

NSUserDefaults类的使用和NSKeyedArchiver有很多类似之处,但是查看NSUserDefaults的定义可以看出,NSUserDefaults直接继承自NSObject而NSKeyedArchiver继承自NSCoder。这意味着NSKeyedArchiver实际上是个归档持久化的类,也就可以使用NSCoder类的[encodeObject:(id)objv forKey:(NSString *)key]方法来对数据进行持久化存储。

 


- (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:persistentArrayforKey:@"myDefault"];
 
NSString *descriptionDefault =[persistentDefaults description];
 
NSLog(@"NSUserDefaults description is:%@",descriptionDefault);
 

 
//unarchive
 
NSArray *UnpersistentArray =

[persistentDefaultsobjectForKey:@"myDefault"];


 
NSString *UnstrOne = [UnpersistentArrayobjectAtIndex:0];
 
NSString *UnstrTwo = [UnpersistentArrayobjectAtIndex:1];
 

 
NSLog(@"UnstrOne = %@,UnstrTwo = %@",UnstrOne,UnstrTwo);
 

 
// Override point for customization afterapplication launch
 
[window makeKeyAndVisible];
}


二、对象归档NSKeyedArchiver和NSKeyedUnarchive
r

iPhone和symbian3rd一样,会为每一个应用程序生成一个私有目录,这个目录位于

/Users/sundfsun2009/Library/Application Support/iPhoneSimulator/User/Applications下,并随即生成一个数字字母串作为目录名,在每一次应用程序启动时,这个字母数字串都是不同于上一次的,上一次的应用程序目录信息被转换成名为.DS_Store隐藏文件,这个目录的文件结构如下图:

 

通常使用Documents目录进行数据持久化的保存,而这个Documents目录可以通过NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserdomainMask,YES)得到,代码如下:

-(void)applicationDidFinishLaunching:(UIApplication *)application{
 
NSString *strOne = @"Persistent data1";
 
NSString *strTwo = @"Persistent data 2";
 

 
NSArray *persistentArray = [NSArrayarrayWithObjects: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 = [filePathstringByAppendingPathComponent:@"myFile.rtf"];
 

 
NSLog(@"myfile's path is :%@",myFilename);
 

 
// no files generated in correspond directorynow
 

 
[NSKeyedArchiverarchiveRootObject:persistentArray toFile:myFilename];
 
// now the myFile.rtf is generated
 

 
// Override point for customization afterapplication launch
 
[window makeKeyAndVisible];
}

 

NSSearchPathForDirectoriesInDomains()的第二个参数是个枚举值,在笔者的测试代码中,只有NSUserDomainMask和NSAllDomainsMask可以获取到目录数为1,其余的皆为0,打印出来的结果如下:

 

[Session started at 2009-11-1021:30:08 +0800.]
2009-11-10 21:30:10.516 PersistentExample[763:207] path number is:1
2009-11-10 21:30:10.518 PersistentExample[763:207] 0 path is:/Users/sundfsun2009/Library/Application Support/iPhoneSimulator/User/Applications/C93DC783-F137-4660-AE5A-08C3E11C774B/Documents
2009-11-10 21:30:10.521 PersistentExample[763:207] myfile's path is:/Users/sundfsun2009/Library/Application Support/iPhoneSimulator/User/Applications/C93DC783-F137-4660-AE5A-08C3E11C774B/Documents/myFile.rtf
Terminating in response to SpringBoard's termination.

[Session started at 2009-11-1021:32:27 +0800.]
2009-11-10 21:32:30.091 PersistentExample[803:207] path number is:1
2009-11-10 21:32:30.092 PersistentExample[803:207] 0 path is:/Users/sundfsun2009/Library/Application Support/iPhoneSimulator/User/Applications/763E6772-E754-452F-8532-80C2CE4466B5/Documents
2009-11-10 21:32:30.100 PersistentExample[803:207] myfile's path is:/Users/sundfsun2009/Library/Application Support/iPhoneSimulator/User/Applications/763E6772-E754-452F-8532-80C2CE4466B5/Documents/myFile.rtf
Terminating in response to SpringBoard's termination.


从打印的结果如下,每次应用程序启动时生成的数字字母串目录名字并不一样。在调用[NSKeyedArchiverarchiveRootObject:persistentArraytoFile:myFilename]方法前,文件myFile.rtf并每生成,只有在调用此方法后才产生相应的文件。

下面需要把数据从属性列表中读取出来,在上面的代码中,笔者使用NSArray保存数据。但在大多数应用程序中,数据的尺寸并不是固定的,这个时候就需要使用NSMutalbeArray动态的保存数据,代码优化如下:

 

        (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 = [pathArrayobjectAtIndex:i];
 
 
 
  NSLog(@"%d path is:%@",i,filePath);
 
}
 

 
myFilename = [filePathstringByAppendingPathComponent:@"myFile.rtf"];
 

 
NSLog(@"myfile's path is :%@",myFilename);
 

 
[NSKeyedArchiverarchiveRootObject:persistentArray toFile:myFilename];
 
}
 

 
// unarchive
 
{
 
NSArray *unarchiveArray = [NSKeyedUnarchiverunarchiveObjectWithFile:myFilename];
 
NSString *UnstrOne = [unarchiveArrayobjectAtIndex:0];
 
NSString *UnstrTwo = [unarchiveArrayobjectAtIndex:1];
 

 
NSLog(@"UnstrOne = %@,UnstrTwo = %@",UnstrOne,UnstrTwo);
 
}
 

 

 
// Override point for customization afterapplication launch
 
[window makeKeyAndVisible];
}

 

输出结果如下:

 

[Session started at 2009-11-1022:41:57 +0800.]
2009-11-10 22:41:59.344 PersistentExample[1082:207] path number is:1
2009-11-10 22:41:59.346 PersistentExample[1082:207] 0 path is:/Users/sundfsun2009/Library/Application Support/iPhoneSimulator/User/Applications/055CD17C-864E-4A83-ABF0-5F01EE85BD5A/Documents
2009-11-10 22:41:59.355 PersistentExample[1082:207] myfile's pathis :/Users/sundfsun2009/Library/Application Support/iPhoneSimulator/User/Applications/055CD17C-864E-4A83-ABF0-5F01EE85BD5A/Documents/myFile.rtf
2009-11-10 22:41:59.357 PersistentExample[1082:207] UnstrOne =Persistent data1,UnstrTwo = Persistent data 2
Terminating in response to SpringBoard's termination.


从上面的图中可以看到,目录中还有个tmp目录,读者也可以把数据保存在tmp目录中,获取这个目录使用NSTemporaryDirectory()方法。


三、嵌入式数据库(SQLite3)

嵌入式数据库持久化数据就是把数据保存在iphone的嵌入式数据库系统SQLite3中,本质上来说,数据库持久化操作是基于文件持久化基础之上的。
要使用嵌入式数据库SQLite3,首先需要加载其动态库libsqlite3.dylib,这个文件位于/Xcode3.1.4/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.sdk/usr/lib目录下。在Framework文件夹上右击,选择“Adding->ExistingFiles...”,定位到上述目录并加载到文件夹。

首先在头文件中做如下修改:

 

#import<UIKit/UIKit.h>

#include "sqlite3.h"
#define kFileName @"mydb.sql"

@interfacePersistentExampleAppDelegate : NSObject<UIApplicationDelegate> {
 
sqlite3 *database;
 
UIWindow *window;
}

@property (nonatomic, retain)IBOutlet UIWindow *window;

@end

 

 


- (void)applicationDidFinishLaun
ching:(UIApplication *)application{
 

 
NSArray *path =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
 
NSString *paths = [[path objectAtIndex:0]stringByAppendingPathComponent:kFileName];
 

 
NSFileManager *fileManager = [NSFileManagerdefaultManager];
 
BOOL findFile = [fileManagerfileExistsAtPath:paths];
 

 
NSLog(@"Database file path =%@",paths);
 

 
// 如果找到了数据库文件
 
if(findFile)
 
{
 
NSLog(@"Database file have alreadyexisted.");
 

 
if(sqlite3_open([paths UTF8String],&database) != SQLITE_OK)//打开数据库失败
 
{
 
  sqlite3_close(database);
 
  NSAssert(0,@"Failed to opendatabase");
 
}
 
}else
 
{
 
NSLog(@"Database file does not exsit!");
 
if(sqlite3_open([paths UTF8String],&database) != SQLITE_OK)//打开数据库失败
 
{
 
  sqlite3_close(database);
 
  NSAssert(0,@"Failed to opendatabase");
 
}
 
}
 

 
char *errorMsg;

 

//创建表
 
NSString *createSQL = @"create table if notexists fields (row integer primary key, field_data text);";
 
if(sqlite3_exec(database, [createSQLUTF8String],NULL,NULL,&errorMsg)!=SQLITE_OK)
 
{
 
sqlite3_close(database);
 
NSAssert1(0,@"Error creating table:%s",errorMsg);
 
}
 

 
NSString *strOne = @"Persistent data1";
 
NSString *strTwo = @"Persistent data 2";
 

 
NSMutableArray *persistentArray =[[NSMutableArray alloc] init];
 
[persistentArray addObject:strOne];
 
[persistentArray addObject:strTwo];
 

 
for (int i = 0; i <[persistentArray count]; i++) {
 
NSString *upDataSQL = [[NSString alloc]initWithFormat:@"insert or replace into

fields (row,field_data) values(%d,'%@');",i,[persistentArray objectAtIndex:i]];


 
char* errorMsg;
 
if(sqlite3_exec(database,[upDataSQLUTF8String],NULL,NULL,&errorMsg)

!= SQLITE_OK)
 
{
 
  sqlite3_close(database);
 
  NSAssert(0,@"Failed to opendatabase");
 
}
 
}
 

 
//unarchive
 
NSString *query = @"select row, field_data fromfields order by row";//查找表中的数据
 
sqlite3_stmt *statement;
 
if(sqlite3_prepare_v2(database, [queryUTF8String], -1, &statement, nil)

== SQLITE_OK)
 
{
 
while(sqlite3_step(statement) ==SQLITE_ROW)
 
{
 
  int row =sqlite3_column_int(statement, 0);
 
  char *rowData = (char*)sqlite3_column_text(statement, 1);
 
 
 
  NSString *fieldName =[[NSString alloc] initWithFormat:@"show%d",row];
 
  NSString *fieldValue =[[NSString alloc] initWithUTF8String:rowData];

   NSLog(@"fieldName is:%@,fieldValue is :%@",fieldName,fieldValue);
 
 
 
  [fieldName release];
 
  [fieldValue release];
 
}
 
sqlite3_finalize(statement);
 
}
 

 
// Override point for customization afterapplication launch
 
[window makeKeyAndVisible];
}


在上面的代码中,我们使用
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL findFile = [fileManager fileExistsAtPath:paths];
来判断数据库文件是否已经存在,其实在大多数情况下是没有必要的,sqlite3_open()方法会自动帮我们判断数据库文件是否存在,如果不存在则创建心的数据库文件。

四、其它方法

除了上面的三种方法来保存持久化数据以外,我们还可以用写文件到磁盘的方式来保存持久化数据。

 

        (void)applicationDidFinishLaunching:(UIApplication *)application{
 

 
NSString *strOne = @"Persistent data1";
 
NSString *strTwo = @"Persistent data 2";
 

 
NSMutableArray *persistentArray =[[NSMutableArray alloc] init];
 
[persistentArray addObject:strOne];
 
[persistentArray addObject:strTwo];
 

 

 

 
NSArray *filePathArray =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
 
NSString *filePath =

        [[filePathArray objectAtIndex:0]stringByAppendingPathComponent:@"mydatas.plist"];
 

 
[[NSArray arrayWithObjects:persistentArray,nil]writeToFile:filePath atomically:NO];
 

 
//load
 
NSMutableArray *saveDataArray = [[NSMutableArrayalloc] init];
 
if([[NSFileManager defaultManager]fileExistsAtPath:filePath])
 
saveDataArray = [NSMutableArrayarrayWithContentsOfFile:filePath]; 
 
else
 
saveDataArray = [NSMutableArrayarrayWithContentsOfFile:[[NSBundle

                mainBundle] pathForResource:@"Savedatas" ofType:@"plist"]];

        
 
NSArray *strArray = [saveDataArrayobjectAtIndex:0];
 

 
NSString *UnstrOne = [strArrayobjectAtIndex:0];
 
NSString *UnstrTwo = [strArrayobjectAtIndex:1];

 // Overridepoint for customization after application launch
 
[window makeKeyAndVisible];


原创粉丝点击