SQLitePersistentObject使用要點[2010.12.31更新]

一、項目地址:http://code.google.com/p/sqlitepersistentobjects/;sql

二、如何引入項目中:將Src目錄下的文件複製到本身的項目中,並連接到 libsqlite3.dylib庫文件;
三、如何使用它:
1)、定義類
#import <foundation/foundation.h>
#import
"SQLitePersistentObject.h"

@
interface PersistablePerson : SQLitePersistentObject {
NSString *lastName;
NSString *firstName;
}
@property (nonatomic, retain) NSString * lastName;
@property (nonatomic, retain) NSString * firstName;
@end
2)、在代碼中使用它
PersistablePerson *person = [[PersistablePerson alloc] init];
person.firstName =
@"Joe" ;
person.lastName =
@"Smith" ;
[person save];
//保存到數據庫中
NSArray *people = [PersistablePerson findByLastName:
@"Smith" ] //從數據庫中查詢LastName="Smith"的記錄
PeristablePerson *joeSmith = [PersistablePerson findFirstByCriteria:
@"WHERE last_name = 'Smith' AND first_name = 'Joe'];//從數據庫中查詢lastName="Smith"而且firstName= 'Joe'的記錄
當查詢的條件只有一個時,可使用類方法「findBy屬性名」來查找相關記錄。當須要對多個屬性進行查詢的時候,就須要使用類方法「
findFirstByCriteria 」來查找,其參數所使用的字段名稱爲實際數據庫表中的字段名,而不是類中聲明的屬性名。
四、使用索引
在本身定義的類中重載類方法 indices,定義一個返回數組元素的數組。
+(NSArray *)indices;
定義:
+(NSArray *)indices
{
NSArray *index1 = [NSArray arrayWithObject:
@"lastName" ];
NSArray *index2 = [NSArray arrayWithObjects:
@"lastName" , @"firstName" , nil];
NSArray *index3 = [NSArray arrayWithObjects:
@"age" , @"lastName" , @"firstName" , nil];
return [NSArray arrayWithObjects:index1, index2, index3, nil];
}
五、如何標記
transient屬性
所謂 transient就是隻在類中使用而不被持久化到數據庫中的屬性。 跟標記索引差很少,須要重載類方法 transients方法,返回一個字符串數組。例如:
+(NSArray *)transients
{
  
return [NSArray arrayWithObject: @"transientNumber" ];
}
六、查找關聯表
        使用SQLitePersistentObject對象做爲屬性,能夠表現「主-從」表關係,以下代碼:
  
  
  
  
  1. //代碼詳見SQLitePersistentObject自帶的Sample Code\SimpleConsoleText 
  2. //Class Post 
  3. @interface Post : SQLitePersistentObject { 
  4.     NSString *title; 
  5.     NSString *text; 
  6.     NSString *transientBit; 
  7. @property (nonatomic,readwrite,retain) NSString *title; 
  8. @property (nonatomic,readwrite,retain) NSString *text; 
  9. @property (nonatomic,readwrite,retain) NSString *transientBit; 
  10. @end; 
  11.  
  12. //Class PostComment  
  13. @class Post; 
  14. @interface PostComment : SQLitePersistentObject { 
  15.     NSString *title; 
  16.     Post *post; 
  17. @property (nonatomic,readwrite,retain) NSString *title; 
  18. @property (nonatomic,readwrite,retain) Post *post; 
  19. @end; 
  20.  
  21. //使用 
  22.     Post *p = [[Post alloc] init]; 
  23.     p.title = @"Test post 1"
  24.     p.text = @"text"
  25.         if ([p existsInDB] == NO) LOG(@"Confirmed not in DB yet"); 
  26.     [p save]; 
  27.  
  28.     PostComment *co = [[PostComment alloc] init]; 
  29.     co.title = @"Comment 1 to Post 2"
  30.     co.post = p; 
  31.     [co save]; 
  32.         [p release]; 
  33.         [co release]; 
  34.  
  35. //查找Post全部的從表 
  36.         Post *p = [Post findByPK:1]; 
  37.     for(PostComment *co in [p findRelated:[PostComment class]]) { 
  38.         LOG(@"\t\t%@", co); 
  39.     } 
  40.  
  41. //訪問PostComment的主表 
  42.         PostComment *co = [PostComment findByPK:1]; 
  43.         NSLog(@"%@",co.post.text) 
須要注意的是:當Post和PostComment的數據都被修改後,主表Post保存時並不會保存從表PostComment的數據,但從表Postcomment保存時,會同時保存主表Post的數據。
七、使用clearCache
        在某個SQLitePersistentObject子類被直接或間接被引用時候,它會將相關聯的表中數據加載到內存中,因此當數據不少的時候會長期佔用一部份內存,爲了節省內存,能夠在某些類再也不被使用的時候,利用類方法clearCache來清除內存中駐留的表數據。clearCache函數實現代碼也是至關簡單,以下:
  
  
  
  
  1. + (void)clearCache 
  2.   if(objectMap != nil) 
  3.     [objectMap removeAllObjects]; 
  4.    
  5.   if(checkedTables != nil) 
  6.     [checkedTables removeAllObjects]; 
  7.    
八、其它
1)、它會在內存中維護一個全部持久化對象有映射,全部持久化對象不會被屢次加載,若是要實現不一樣的持久話實例的話,須要在持久化類中實現 NSCopying接口。
2)、目前沒有 rollback方法。
3)、SQLitePersistentObject未使用私有非正式的類庫。
相關文章
相關標籤/搜索