iOS开发之Realm数据库的简单使用
1.安装
如果使用
cocoapods
,示例语句如下
platform :ios, '8.0'
target 'Simona_Realm' do
pod 'Realm', '~> 3.0.0-beta.3'
end
2.创建简易模型,继承于RLMObject
,存储在数据库的模型都需要继承于RLMObject
-
Person.h
-
-
RLM_ARRAY_TYPE(Person);
-
@interface Person : RLMObject
-
-
@property NSString *personId;
-
@property BOOL status;
-
@property NSString *sex;
-
-
@end
-
Person.m
-
-
-
@implementation Person
-
-
// 设置主键,确保数据唯一性
-
+ (NSString *)primaryKey {
-
return @”personId”;
-
}
-
-
// 设置属性不为nil
-
+ (NSArray<NSString *> *)requiredProperties {
-
return @[@”sex”];
-
}
-
-
// 设置忽略属性
-
+ (NSArray<NSString *> *)ignoredProperties {
-
return @[];
-
}
-
-
// 设置默认值,对于不为null的属性,默认给空
-
+ (NSDictionary *)defaultPropertyValues {
-
return @{@”sex”:@””};
-
}
-
-
//索引属性,主要用于搜索,根据性别进行搜索
-
+ (NSArray<NSString *> *)indexedProperties {
-
return @[@”sex”,@”woman”];
-
}
-
-
@end
关于RLMObject
1.Realm
忽略了OC
的属性特性(如nonatomic, atomic, strong,retain, weak,copy
等),所以在声明属性时可不写,,这些特性会一直生效直到被写入数据库。
2.Realm
支持以下的类型BOOL, NSInteger, long, double, CGFloat, NSString, NSDate, NSData
等
3.定义了RLM_ARRAY_TYPE(Person)
表示支持RLMArray
属性,相当于允许RLMArray<Person>
属性的使用,例如:在其他属性里可@property RLMArray<Person *><Person> *personal
如此使用,相当于继承关系(本文暂不做赘述)
3.使用
<1.>存储
-
模拟存储一万条假数据
-
RLMRealm *realm = [RLMRealm defaultRealm];
-
[realm transactionWithBlock:^{
-
for (int i = 0; i < 10000; i++) {
-
Person *person = [[Person alloc]init];
-
person.personId = [NSString stringWithFormat:@“张%d”,i];
-
person.sex = @“Not scanned”;
-
person.status = YES;
-
[realm addObject:person];
-
}
-
}];
这里的
[realm transactionWithBlock:^{ }];
等同于
[realm beginWriteTransaction];
[realm commitWriteTransaction];
每次打开关闭数据库时都应执行此操作
<2.>删除
-
RLMRealm *realm = [RLMRealm defaultRealm];
-
[];
-
[];
-
[];
<3.>查询
-
1.根据谓词查询
-
RLMResults相当于查询到的数组
-
NSPredicate *pred = [NSPredicate predicateWithFormat:@”status = %ld”, NO];
-
RLMResults *result = [Person objectsWithPredicate:pred];
-
Person *per = result[indexPath.row];
-
-
2.根据条件查询
-
RLMResults *result = [Person objectsWhere:@”status = ‘NO’ AND personId BEGINSWITH ‘张1′”];
-
Person *per = result[indexPath.row];
-
-
3.查询所有
-
RLMResults *persons = [Person allObjects];
-
Person *per = persons[indexPath.row];
<4.>更新
-
1.更新数据里某一属性值
-
NSPredicate *pred = [NSPredicate predicateWithFormat:@“personId = %@”, strScan];
-
RLMResults *result = [Person objectsWithPredicate:pred];
-
if (result.count != 0) {
-
-
RLMRealm *r = [RLMRealm defaultRealm];
-
[r beginWriteTransaction];
-
//直接修改
-
Person *person = result[0];
-
person.sex = @“Has been scanned”;
-
person.status = NO;
-
[r commitWriteTransaction];
-
}
-
-
2.创建一个新值并更新数据库,createOrUpdateInRealm:通过主键来更新插入
-
-
NSPredicate *pred = [NSPredicate predicateWithFormat:@“personId = %@”, strScan];
-
RLMResults *result = [Person objectsWithPredicate:pred];
-
if (result.count != 0) {
-
-
Person *person = [[Person alloc]init];
-
person.personId = @“Simona1”;
-
person.sex = @“woman”;
-
person.status = NO;
-
RLMRealm *r = [RLMRealm defaultRealm];
-
[r beginWriteTransaction];
-
[Person createOrUpdateInRealm:r withValue:person];
-
[r commitWriteTransaction];