一、項目地址: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方法,返回一個字符串數組。例如:
使用SQLitePersistentObject對象做爲屬性,能夠表現「主-從」表關係,以下代碼:
-
-
- @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 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 *p = [Post findByPK:1];
- for(PostComment *co in [p findRelated:[PostComment class]]) {
- LOG(@"\t\t%@", co);
- }
-
-
- 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未使用私有非正式的類庫。