realm官網(wǎng): https://realm.io/cn/docs/objc/latest/
1.將Realm庫pod到工程中
pod 'Realm'
2.建立數(shù)據(jù)模型
#import <Realm/Realm.h>
@interface Student : RLMObject
@property NSString *name;
@property NSInteger age;
/**
獲取數(shù)據(jù)模型
*/
+(Student *)StudentWithName:(NSString *)name;
/**
刪除數(shù)據(jù)
*/
+(void)deleteStudentWithName:(NSString *)name;
/**
更新數(shù)據(jù)
*/
- (void)updateAge:(NSInteger)age;
@end
#import "Student.h"
@implementation Student
/**
設(shè)置主鍵
*/
+(NSString *)primaryKey{
//主鍵
return @"name";
}
/**
獲取數(shù)據(jù)模型
*/
+(Student *)StudentWithName:(NSString *)name
{
RLMRealm *realm = [RLMRealm defaultRealm];
Student *student = [Student objectForPrimaryKey:name];
if (student == nil) {
student = [[Student alloc] init];
student.name = name;
[realm beginWriteTransaction];
[realm addObject:student];
[realm commitWriteTransaction];
}
return student;
}
/**
更新數(shù)據(jù)
*/
- (void)updateAge:(NSInteger)age{
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
self.age = age;
[realm commitWriteTransaction];
}
/**
刪除數(shù)據(jù)
*/
+ (void)deleteStudentWithName:(NSString *)name{
RLMRealm *realm = [RLMRealm defaultRealm];
Student *student = [Student objectForPrimaryKey:name];
[realm beginWriteTransaction];
[realm deleteObject:student];
[realm commitWriteTransaction];
}
@end
3.增、刪、改、查 操作
/**
獲取數(shù)據(jù)模型【增】
*/
+(Student *)StudentWithName:(NSString *)name
{
RLMRealm *realm = [RLMRealm defaultRealm];
Student *student = [Student objectForPrimaryKey:name];
if (student == nil) {
student = [[Student alloc] init];
student.name = name;
//增加模型
[realm beginWriteTransaction];
[realm addObject:student];
[realm commitWriteTransaction];
}
return student;
}
/**
刪除數(shù)據(jù)【刪】
*/
+ (void)deleteStudentWithName:(NSString *)name{
RLMRealm *realm = [RLMRealm defaultRealm];
Student *student = [Student objectForPrimaryKey:name];
[realm beginWriteTransaction];
//刪除模型
[realm deleteObject:student];
[realm commitWriteTransaction];
}
/**
更新數(shù)據(jù)【改】
*/
- (void)updateAge:(NSInteger)age{
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
//更改數(shù)據(jù)
self.age = age;
[realm commitWriteTransaction];
}
/**
查詢數(shù)據(jù)【查】
*/
_allStudent = [Student allObjects];
4.數(shù)據(jù)庫升級(jí)
所謂數(shù)據(jù)庫升級(jí),比如,rev1.0版本發(fā)布了,其數(shù)據(jù)中原始數(shù)據(jù)模型為只有 name 和 age 兩個(gè)屬性。rev2.0需要給模型添加 score 和 class 兩個(gè)屬性,這時(shí)候,如果直接給模型添加屬性,不做數(shù)據(jù)庫升級(jí)操作,則需要將APP卸載,重新安裝才能使用,不然就是直接crash。
添加一個(gè)數(shù)據(jù)模型也是一樣的,比如直接新建一個(gè) Math 模型,其有teacher 和 class屬性。直接新建添加,不做數(shù)據(jù)庫升級(jí),也是直接crash。
當(dāng)涉及到更改數(shù)據(jù)庫模型時(shí)就需要對(duì)數(shù)據(jù)庫進(jìn)行升級(jí)操作了,realm數(shù)據(jù)庫升級(jí)操作很簡單。在AppDelegate中調(diào)用即可。
- (void)migrationRealm{
// 在 [AppDelegate didFinishLaunchingWithOptions:] 中進(jìn)行配置
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
// 設(shè)置新的架構(gòu)版本。這個(gè)版本號(hào)必須高于之前所用的版本號(hào)(如果您之前從未設(shè)置過架構(gòu)版本,那么這個(gè)版本號(hào)設(shè)置為 0)
config.schemaVersion = 2;
// 設(shè)置閉包,這個(gè)閉包將會(huì)在打開低于上面所設(shè)置版本號(hào)的 Realm 數(shù)據(jù)庫的時(shí)候被自動(dòng)調(diào)用
config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
// 什么都不要做!Realm 會(huì)自行檢測(cè)新增和需要移除的屬性,然后自動(dòng)更新硬盤上的數(shù)據(jù)庫架構(gòu)
if (oldSchemaVersion < 1) {
}
};
// 告訴 Realm 為默認(rèn)的 Realm 數(shù)據(jù)庫使用這個(gè)新的配置對(duì)象
[RLMRealmConfiguration setDefaultConfiguration:config];
// 現(xiàn)在我們已經(jīng)告訴了 Realm 如何處理架構(gòu)的變化,打開文件之后將會(huì)自動(dòng)執(zhí)行遷移
[RLMRealm defaultRealm];
}
然后調(diào)用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//數(shù)據(jù)庫升級(jí)
[self migrationRealm];
return YES;
}