1.安裝
如果使用
cocoapods
,示例語句如下
platform :ios, '8.0'
target 'Simona_Realm' do
pod 'Realm', '~> 3.0.0-beta.3'
end
2.創(chuàng)建簡易模型,繼承于RLMObject
,存儲(chǔ)在數(shù)據(jù)庫的模型都需要繼承于RLMObject
Person.h
#import <Realm/Realm.h>
RLM_ARRAY_TYPE(Person);
@interface Person : RLMObject
@property NSString *personId;
@property BOOL status;
@property NSString *sex;
@end
Person.m
#import "Person.h"
@implementation Person
// 設(shè)置主鍵,確保數(shù)據(jù)唯一性
+ (NSString *)primaryKey {
return @"personId";
}
// 設(shè)置屬性不為nil
+ (NSArray<NSString *> *)requiredProperties {
return @[@"sex"];
}
// 設(shè)置忽略屬性
+ (NSArray<NSString *> *)ignoredProperties {
return @[];
}
// 設(shè)置默認(rèn)值,對(duì)于不為null的屬性,默認(rèn)給空
+ (NSDictionary *)defaultPropertyValues {
return @{@"sex":@""};
}
//索引屬性,主要用于搜索,根據(jù)性別進(jìn)行搜索
+ (NSArray<NSString *> *)indexedProperties {
return @[@"sex",@"woman"];
}
@end
關(guān)于RLMObject
1.Realm
忽略了OC
的屬性特性(如nonatomic, atomic, strong,retain, weak,copy
等),所以在聲明屬性時(shí)可不寫,醋火,這些特性會(huì)一直生效直到被寫入數(shù)據(jù)庫悠汽。
2.Realm
支持以下的類型BOOL, NSInteger, long, double, CGFloat, NSString, NSDate, NSData
等
3.定義了RLM_ARRAY_TYPE(Person)
表示支持RLMArray
屬性,相當(dāng)于允許RLMArray<Person>
屬性的使用,例如:在其他屬性里可@property RLMArray<Person *><Person> *personal
如此使用,相當(dāng)于繼承關(guān)系(本文暫不做贅述)
3.使用
<1.>存儲(chǔ)
模擬存儲(chǔ)一萬條假數(shù)據(jù)
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];
每次打開關(guān)閉數(shù)據(jù)庫時(shí)都應(yīng)執(zhí)行此操作
<2.>刪除
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm deleteAllObjects];
[realm commitWriteTransaction];
<3.>查詢
1.根據(jù)謂詞查詢
// RLMResults相當(dāng)于查詢到的數(shù)組
NSPredicate *pred = [NSPredicate predicateWithFormat:@"status = %ld", NO];
RLMResults *result = [Person objectsWithPredicate:pred];
Person *per = result[indexPath.row];
2.根據(jù)條件查詢
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.更新數(shù)據(jù)里某一屬性值
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.創(chuàng)建一個(gè)新值并更新數(shù)據(jù)庫,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];