一、项目地址: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未使用私有非正式的类库。