《電子技術(shù)應(yīng)用》
您所在的位置:首頁(yè) > 微波|射頻 > 設(shè)計(jì)應(yīng)用 > 深入理解iPhone數(shù)據(jù)持久化
深入理解iPhone數(shù)據(jù)持久化
摘要: iphone提供的數(shù)據(jù)持久化的方法,從數(shù)據(jù)保存的方式上講可以分為三大部分:屬性列表、對(duì)象歸檔、嵌入式數(shù)據(jù)庫(kù)(SQLite3)、其他方法。
Abstract:
Key words :

  在所有的移動(dòng)開發(fā)平臺(tái)數(shù)據(jù)持久化都是很重要的部分:在j2me中是rms或保存在應(yīng)用程序的目錄中,在symbian中可以保存在相應(yīng)的磁盤目錄中和數(shù)據(jù)庫(kù)中。symbian中因?yàn)闄?quán)限認(rèn)證的原因,在3rd上大多數(shù)只能訪問應(yīng)用程序的private目錄或其它系統(tǒng)共享目錄。在iphone中,apple博采眾長(zhǎng),提供了多種數(shù)據(jù)持久化的方法,下面筆者會(huì)逐個(gè)進(jìn)行詳細(xì)的講解。

  iphone提供的數(shù)據(jù)持久化的方法,從數(shù)據(jù)保存的方式上講可以分為三大部分:屬性列表、對(duì)象歸檔、嵌入式數(shù)據(jù)庫(kù)(SQLite3)、其他方法。

  一、屬性列表NSUserDefaults

  NSUserDefaults類的使用和NSKeyedArchiver有很多類似之處,但是查看NSUserDefaults的定義可以看出,NSUserDefaults直接繼承自NSObject而NSKeyedArchiver 繼承自NSCoder。這意味著NSKeyedArchiver實(shí)際上是個(gè)歸檔持久化的類,也就可以使用NSCoder類的[encodeObject: (id)objv forKey:(NSString *)key]方法來對(duì)數(shù)據(jù)進(jìn)行持久化存儲(chǔ)。

  - (void)applicationDidFinishLaunching:(UIApplication *)application {

  NSString *strOne = @“Persistent data1”;

  NSString *strTwo = @“Persistent data 2”;

  NSMutableArray *persistentArray = [[NSMutableArray alloc] init];

 ?。踦ersistentArray addObject:strOne];

  [persistentArray addObject:strTwo];

  //archive

  NSUserDefaults *persistentDefaults = [NSUserDefaults standardUserDefaults];

 ?。踦ersistentDefaults setObject:persistentArray forKey:@“myDefault”];

  NSString *descriptionDefault = [persistentDefaults description];

  NSLog(@“NSUserDefaults description is :%@”,descriptionDefault);

  //unarchive

  NSArray *UnpersistentArray =

 ?。踦ersistentDefaults objectForKey:@“myDefault”];

  NSString *UnstrOne = [UnpersistentArray objectAtIndex:0];

  NSString *UnstrTwo = [UnpersistentArray objectAtIndex:1];

  NSLog(@“UnstrOne = %@,UnstrTwo = %@”,UnstrOne,UnstrTwo);

  // Override point for customization after application launch

 ?。踳indow makeKeyAndVisible];

  }

  二、對(duì)象歸檔NSKeyedArchiver和NSKeyedUnarchiver

  iPhone和symbian 3rd一樣,會(huì)為每一個(gè)應(yīng)用程序生成一個(gè)私有目錄,這個(gè)目錄位于

  /Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications下,并隨即生成一個(gè)數(shù)字字母串作為目錄名,在每一次應(yīng)用程序啟動(dòng)時(shí),這個(gè)字母數(shù)字串都是不同于上一次的,上一次的應(yīng)用程序目錄信息被轉(zhuǎn)換成名為.DS_Store隱藏文件,這個(gè)目錄的文件結(jié)構(gòu)如下圖:

  通常使用Documents目錄進(jìn)行數(shù)據(jù)持久化的保存,而這個(gè)Documents目錄可以通過NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserdomainMask,YES)得到,代碼如下:

  - (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(@“myfile‘s 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

 ?。踳indow makeKeyAndVisible];

  }

  NSSearchPathForDirectoriesInDomains()的第二個(gè)參數(shù)是個(gè)枚舉值,在筆者的測(cè)試代碼中,只有NSUserDomainMask和NSAllDomainsMask可以獲取到目錄數(shù)為1,其余的皆為0,打印出來的結(jié)果如下:

 ?。跾ession started at 2009-11-10 21: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/iPhone Simulator/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/iPhone Simulator/User/Applications/C93DC783-F137-4660-AE5A-08C3E11C774B/Documents/myFile.rtf

  Terminating in response to SpringBoard‘s termination.

 ?。跾ession started at 2009-11-10 21: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/iPhone Simulator/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/iPhone Simulator/User/Applications/763E6772-E754-452F-8532-80C2CE4466B5/Documents/myFile.rtf

  Terminating in response to SpringBoard‘s termination.

  從打印的結(jié)果如下,每次應(yīng)用程序啟動(dòng)時(shí)生成的數(shù)字字母串目錄名字并不一樣。在調(diào)用[NSKeyedArchiver archiveRootObject:persistentArray toFile:myFilename]方法前,文件myFile.rtf并每生成,只有在調(diào)用此方法后才產(chǎn)生相應(yīng)的文件。

  在所有的移動(dòng)開發(fā)平臺(tái)數(shù)據(jù)持久化都是很重要的部分:在j2me中是rms或保存在應(yīng)用程序的目錄中,在symbian中可以保存在相應(yīng)的磁盤目錄中和數(shù)據(jù)庫(kù)中。symbian中因?yàn)闄?quán)限認(rèn)證的原因,在3rd上大多數(shù)只能訪問應(yīng)用程序的private目錄或其它系統(tǒng)共享目錄。在iphone中,apple博采眾長(zhǎng),提供了多種數(shù)據(jù)持久化的方法,下面筆者會(huì)逐個(gè)進(jìn)行詳細(xì)的講解。

  iphone提供的數(shù)據(jù)持久化的方法,從數(shù)據(jù)保存的方式上講可以分為三大部分:屬性列表、對(duì)象歸檔、嵌入式數(shù)據(jù)庫(kù)(SQLite3)、其他方法。

  一、屬性列表NSUserDefaults

  NSUserDefaults類的使用和NSKeyedArchiver有很多類似之處,但是查看NSUserDefaults的定義可以看出,NSUserDefaults直接繼承自NSObject而NSKeyedArchiver 繼承自NSCoder。這意味著NSKeyedArchiver實(shí)際上是個(gè)歸檔持久化的類,也就可以使用NSCoder類的[encodeObject: (id)objv forKey:(NSString *)key]方法來對(duì)數(shù)據(jù)進(jìn)行持久化存儲(chǔ)。

  - (void)applicationDidFinishLaunching:(UIApplication *)application {

  NSString *strOne = @“Persistent data1”;

  NSString *strTwo = @“Persistent data 2”;

  NSMutableArray *persistentArray = [[NSMutableArray alloc] init];

 ?。踦ersistentArray addObject:strOne];

 ?。踦ersistentArray addObject:strTwo];

  //archive

  NSUserDefaults *persistentDefaults = [NSUserDefaults standardUserDefaults];

 ?。踦ersistentDefaults 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

 ?。踳indow makeKeyAndVisible];

  }

  二、對(duì)象歸檔NSKeyedArchiver和NSKeyedUnarchiver

  iPhone和symbian 3rd一樣,會(huì)為每一個(gè)應(yīng)用程序生成一個(gè)私有目錄,這個(gè)目錄位于

  /Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications下,并隨即生成一個(gè)數(shù)字字母串作為目錄名,在每一次應(yīng)用程序啟動(dòng)時(shí),這個(gè)字母數(shù)字串都是不同于上一次的,上一次的應(yīng)用程序目錄信息被轉(zhuǎn)換成名為.DS_Store隱藏文件,這個(gè)目錄的文件結(jié)構(gòu)如下圖:

  通常使用Documents目錄進(jìn)行數(shù)據(jù)持久化的保存,而這個(gè)Documents目錄可以通過NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserdomainMask,YES)得到,代碼如下:

  - (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(@“myfile‘s 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

 ?。踳indow makeKeyAndVisible];

  }

  NSSearchPathForDirectoriesInDomains()的第二個(gè)參數(shù)是個(gè)枚舉值,在筆者的測(cè)試代碼中,只有NSUserDomainMask和NSAllDomainsMask可以獲取到目錄數(shù)為1,其余的皆為0,打印出來的結(jié)果如下:

 ?。跾ession started at 2009-11-10 21: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/iPhone Simulator/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/iPhone Simulator/User/Applications/C93DC783-F137-4660-AE5A-08C3E11C774B/Documents/myFile.rtf

  Terminating in response to SpringBoard‘s termination.

  [Session started at 2009-11-10 21: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/iPhone Simulator/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/iPhone Simulator/User/Applications/763E6772-E754-452F-8532-80C2CE4466B5/Documents/myFile.rtf

  Terminating in response to SpringBoard‘s termination.

  從打印的結(jié)果如下,每次應(yīng)用程序啟動(dòng)時(shí)生成的數(shù)字字母串目錄名字并不一樣。在調(diào)用[NSKeyedArchiver archiveRootObject:persistentArray toFile:myFilename]方法前,文件myFile.rtf并每生成,只有在調(diào)用此方法后才產(chǎn)生相應(yīng)的文件。

  下面需要把數(shù)據(jù)從屬性列表中讀取出來,在上面的代碼中,筆者使用NSArray保存數(shù)據(jù)。但在大多數(shù)應(yīng)用程序中,數(shù)據(jù)的尺寸并不是固定的,這個(gè)時(shí)候就需要使用NSMutalbeArray動(dòng)態(tài)的保存數(shù)據(jù),代碼優(yōu)化如下:

  - (void)applicationDidFinishLaunching:(UIApplication *)application {

  NSString *myFilename;

  // archive

  {

  NSString *strOne = @“Persistent data1”;

  NSString *strTwo = @“Persistent data 2”;

  NSMutableArray *persistentArray = [[NSMutableArray alloc] init];

 ?。踦ersistentArray addObject:strOne];

 ?。踦ersistentArray 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(@“myfile’s 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

 ?。踳indow makeKeyAndVisible];

  }

  輸出結(jié)果如下:

  [Session started at 2009-11-10 22: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/iPhone Simulator/User/Applications/055CD17C-864E-4A83-ABF0-5F01EE85BD5A/Documents

  2009-11-10 22:41:59.355 PersistentExample[1082:207] myfile‘s path is :/Users/sundfsun2009/Library/Application Support/iPhone Simulator/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.

  從上面的圖中可以看到,目錄中還有個(gè)tmp目錄,讀者也可以把數(shù)據(jù)保存在tmp目錄中,獲取這個(gè)目錄使用NSTemporaryDirectory()方法。

  三、嵌入式數(shù)據(jù)庫(kù)(SQLite3)

  嵌入式數(shù)據(jù)庫(kù)持久化數(shù)據(jù)就是把數(shù)據(jù)保存在iphone的嵌入式數(shù)據(jù)庫(kù)系統(tǒng)SQLite3中,本質(zhì)上來說,數(shù)據(jù)庫(kù)持久化操作是基于文件持久化基礎(chǔ)之上的。

  要使用嵌入式數(shù)據(jù)庫(kù)SQLite3,首先需要加載其動(dòng)態(tài)庫(kù)libsqlite3.dylib,這個(gè)文件位于/Xcode3.1.4/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.sdk/usr/lib目錄下。在Framework文件夾上右擊,選擇“Adding-》Existing Files.。.”,定位到上述目錄并加載到文件夾。

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

  #import 《UIKit/UIKit.h》

  #include “sqlite3.h”

  #define kFileName @“mydb.sql”

  @interface PersistentExampleAppDelegate : NSObject 《UIApplicationDelegate》 {

  sqlite3 *database;

  UIWindow *window;

  }

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

  @end

  - (void)applicationDidFinishLaunching:(UIApplication *)application {

  NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

  NSString *paths = [[path objectAtIndex:0] stringByAppendingPathComponent:kFileName];

  NSFileManager *fileManager = [NSFileManager defaultManager];

  BOOL findFile = [fileManager fileExistsAtPath:paths];

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

  // 如果找到了數(shù)據(jù)庫(kù)文件

  if(findFile)

  {

  NSLog(@“Database file have already existed.”);

  if(sqlite3_open([paths UTF8String], &database) != SQLITE_OK)//打開數(shù)據(jù)庫(kù)失敗

  {

  sqlite3_close(database);

  NSAssert(0,@“Failed to open database”);

  }

  }else

  {

  NSLog(@“Database file does not exsit!”);

  if(sqlite3_open([paths UTF8String], &database) != SQLITE_OK)//打開數(shù)據(jù)庫(kù)失敗

  {

  sqlite3_close(database);

  NSAssert(0,@“Failed to open database”);

  }

  }

  char *errorMsg;

  //創(chuàng)建表

  NSString *createSQL = @“create table if not exists fields (row integer primary key, field_data text);”;

  if(sqlite3_exec(database, [createSQL UTF8String],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];

 ?。踦ersistentArray 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,[upDataSQL UTF8String],NULL,NULL,&errorMsg)

  != SQLITE_OK)

  {

  sqlite3_close(database);

  NSAssert(0,@“Failed to open database”);

  }

  }

  //unarchive

  NSString *query = @“select row, field_data from fields order by row”;//查找表中的數(shù)據(jù)

  sqlite3_stmt *statement;

  if(sqlite3_prepare_v2(database, [query UTF8String], -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);

 ?。踗ieldName release];

  [fieldValue release];

  }

  sqlite3_finalize(statement);

  }

  // Override point for customization after application launch

 ?。踳indow makeKeyAndVisible];

  }

  在上面的代碼中,我們使用

  NSFileManager *fileManager = [NSFileManager defaultManager];

  BOOL findFile = [fileManager fileExistsAtPath:paths];

  來判斷數(shù)據(jù)庫(kù)文件是否已經(jīng)存在,其實(shí)在大多數(shù)情況下是沒有必要的,sqlite3_open()方法會(huì)自動(dòng)幫我們判斷數(shù)據(jù)庫(kù)文件是否存在,如果不存在則創(chuàng)建心的數(shù)據(jù)庫(kù)文件。

  四、其它方法

  除了上面的三種方法來保存持久化數(shù)據(jù)以外,我們還可以用寫文件到磁盤的方式來保存持久化數(shù)據(jù)。

  - (void)applicationDidFinishLaunching:(UIApplication *)application {

  NSString *strOne = @“Persistent data1”;

  NSString *strTwo = @“Persistent data 2”;

  NSMutableArray *persistentArray = [[NSMutableArray alloc] init];

 ?。踦ersistentArray addObject:strOne];

 ?。踦ersistentArray 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 = [[NSMutableArray alloc] init];

  if([[NSFileManager defaultManager] fileExistsAtPath:filePath])

  saveDataArray = [NSMutableArray arrayWithContentsOfFile:filePath];

  else

  saveDataArray = [NSMutableArray arrayWithContentsOfFile:[[NSBundle

  - mainBundle] pathForResource:@“Savedatas” ofType:@“plist”]];

  -

  NSArray *strArray = [saveDataArray objectAtIndex:0];

  NSString *UnstrOne = [strArray objectAtIndex:0];

  NSString *UnstrTwo = [strArray objectAtIndex:1];

  // Override point for customization after application launch

 ?。踳indow makeKeyAndVisible];

  }

此內(nèi)容為AET網(wǎng)站原創(chuàng),未經(jīng)授權(quán)禁止轉(zhuǎn)載。