一、項目地址: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 post
2)、在代碼中使用它google
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'的記錄atom
當查詢的條件只有一個時,可使用類方法「findBy屬性名」來查找相關記錄。當須要對多個屬性進行查詢的時候,就須要使用類方法「 findFirstByCriteria」來查找,其參數所使用的字段名稱爲實際數據庫表中的字段名,而不是類中聲明的屬性名。spa
四、使用索引.net
在本身定義的類中重載類方法indices,定義一個返回數組元素的數組。code
+(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對象做爲屬性,能夠表現「主-從」表關係,以下代碼:
//代碼詳見SQLitePersistentObject自帶的Sample Code\SimpleConsoleText //Class Post @interface Post : SQLitePersistentObject { NSString *title; NSString *text; NSString *transientBit; } @property (nonatomic,readwrite,retain) NSString *title; @property (nonatomic,readwrite,retain) NSString *text; @property (nonatomic,readwrite,retain) NSString *transientBit; @end; //Class PostComment @class Post; @interface PostComment : SQLitePersistentObject { NSString *title; Post *post; } @property (nonatomic,readwrite,retain) NSString *title; @property (nonatomic,readwrite,retain) Post *post; @end; //使用 Post *p = [[Post alloc] init]; p.title = @"Test post 1"; p.text = @"text"; if ([p existsInDB] == NO) LOG(@"Confirmed not in DB yet"); [p save]; PostComment *co = [[PostComment alloc] init]; co.title = @"Comment 1 to Post 2"; co.post = p; [co save]; [p release]; [co release]; //查找Post全部的從表 Post *p = [Post findByPK:1]; for(PostComment *co in [p findRelated:[PostComment class]]) { LOG(@"\t\t%@", co); } //訪問PostComment的主表 PostComment *co = [PostComment findByPK:1]; NSLog(@"%@",co.post.text)
須要注意的是:當Post和PostComment的數據都被修改後,主表Post保存時並不會保存從表PostComment的數據,但從表Postcomment保存時,會同時保存主表Post的數據。
七、使用clearCache
在某個SQLitePersistentObject子類被直接或間接被引用時候,它會將相關聯的表中數據加載到內存中,因此當數據不少的時候會長期佔用 一部份內存,爲了節省內存,能夠在某些類再也不被使用的時候,利用類方法clearCache來清除內存中駐留的表數據。clearCache函數實現代碼 也是至關簡單,以下:
+ (void)clearCache { if(objectMap != nil) [objectMap removeAllObjects]; if(checkedTables != nil) [checkedTables removeAllObjects]; }
八、其它
1)、它會在內存中維護一個全部持久化對象有映射,全部持久化對象不會被屢次加載,若是要實現不一樣的持久話實例的話,須要在持久化類中實現NSCopying接口。
2)、目前沒有rollback方法。
3)、SQLitePersistentObject未使用私有非正式的類庫。