iOS realm數(shù)據(jù)庫使用筆記

1,引入數(shù)據(jù)庫?

這里是使用cocoapods導入,手動導入以后有時間會去做

2,查看數(shù)據(jù)庫路徑,以及查看數(shù)據(jù)庫的文件

路徑一般都是在Documents里面,打印方法:

NSString *path = NSHomeDirectory();//主目錄

NSLog(@"NSHomeDirectory:%@",path);

NSString *userName = NSUserName();//與上面相同

NSString *rootPath = NSHomeDirectoryForUser(userName);

NSLog(@"NSHomeDirectoryForUser:%@",rootPath);

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory=[paths objectAtIndex:0];//Documents目錄

NSLog(@"NSDocumentDirectory:%@",documentsDirectory);

找到數(shù)據(jù)庫的路徑,創(chuàng)建成功的話,能看到以.realm 結(jié)尾的文件,就是數(shù)據(jù)庫文件,打開該文件的軟件我用的是:Realm Browser。 ? 在app store里面可以搜索到的,是免費軟件

3,創(chuàng)建數(shù)據(jù)庫

如果是第一次創(chuàng)建數(shù)據(jù)庫,就不用判斷版本。但是如果不是第一次建立數(shù)據(jù)庫,比如升級app的時候,在新版本的app數(shù)據(jù)庫中添加了幾個屬性,這時候就要判斷app內(nèi)數(shù)據(jù)庫的版本,因為如果不判斷版本,就會出現(xiàn)崩潰的現(xiàn)象,代碼:《一般會在判斷版本數(shù)據(jù)庫路徑最后,創(chuàng)建數(shù)據(jù)庫這里只是把創(chuàng)建的方法單獨提出來了》

//獲取數(shù)據(jù)庫路徑

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];

NSString * pathName = [path stringByAppendingString:@"/realmTest.realm"];

RLMRealmConfiguration * config = [RLMRealmConfiguration defaultConfiguration];

//最新版本,每次更新,都要在上次的基礎上加一,不能低于之前的版本

config.schemaVersion = 3;

//配置新路徑

config.fileURL = [NSURL URLWithString:pathName];

config.migrationBlock = ^(RLMMigration * _Nonnull migration, uint64_t oldSchemaVersion) {

if (oldSchemaVersion < 3) {

NSLog(@"進行數(shù)據(jù)遷移");

}else{

NSLog(@"不進行數(shù)據(jù)遷移");

}

};

[RLMRealmConfiguration setDefaultConfiguration:config];

創(chuàng)建數(shù)據(jù)庫:

默認版本創(chuàng)建:

RLMRealm * realm = [RLMRealm defaultRealm];

自定義創(chuàng)建:

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];

NSString * pathName = [path stringByAppendingString:@"/realmTest.realm"];

RLMRealm * realm = [RLMRealm realmWithURL:[NSURL URLWithString:pathName]];

4,添加數(shù)據(jù)

首先創(chuàng)建模型

m文件的內(nèi)容,必不可少

h文件的內(nèi)容

首先是添加一種數(shù)據(jù)到數(shù)據(jù)庫,沒有包含關系的:

SYDog * dog = [[SYDog alloc]init];

dog.name = [NSString stringWithFormat:@"dog_%u",arc4random()%10];

dog.owner = [NSString stringWithFormat:@"%d",i];

dog.sex = arc4random()%2;

NSInteger cun = self.colorArray.count;

dog.colorStr = self.colorArray[arc4random()%cun];

//默認數(shù)據(jù)庫路徑以及名字

/*

RLMRealm *realm = [RLMRealm defaultRealm];

[realm transactionWithBlock:^{

[realm addObject:dog];

}];

*/

//自定義數(shù)據(jù)庫名字和路徑

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];

NSString * pathName = [path stringByAppendingString:@"/realmTest.realm"];

RLMRealm * realm = [RLMRealm realmWithURL:[NSURL URLWithString:pathName]];

[realm transactionWithBlock:^{

[realm addObject:dog];

}];

添加包含數(shù)組的數(shù)據(jù)到數(shù)據(jù)庫《這里的思路跟添加單個差不多,就相當于添加完單個的,再把單個模型作為一個元素添加到數(shù)組中》:

SYPreson * presen = [[SYPreson alloc]init];

presen.name = [NSString stringWithFormat:@"地主——%d",i];

presen.age = arc4random()%20;

if (arc4random()%2 == 1) {

presen.isMan = YES;

}else{

presen.isMan = NO;

}

NSInteger a = arc4random()%self.palceArray.count;

presen.adress = [NSString stringWithFormat:@"%@",self.palceArray[a]];

for (int i = 0; i < 10; i++) {

SYDog * dog = [[SYDog alloc]init];

dog.name = [NSString stringWithFormat:@"dog_%u",arc4random()%10];

dog.owner = [NSString stringWithFormat:@"%d",i];

dog.sex = arc4random()%2;

NSInteger cun = self.colorArray.count;

dog.colorStr = self.colorArray[arc4random()%cun];

[presen.dogs addObject:dog];

}

SYClass * clas = [[SYClass alloc]init];

NSInteger year = arc4random()%8;

NSInteger ca = arc4random()%50;

clas.className = [NSString stringWithFormat:@"%ld年級 %ld班",year,ca];

clas.classNumber = [NSString stringWithFormat:@"%u",arc4random()%120];

[presen.classs addObject:clas];

//默認數(shù)據(jù)庫路徑

/*

RLMRealm * realm = [RLMRealm defaultRealm];

[realm transactionWithBlock:^{

[realm addObject:presen];

}];

*/

//自定義數(shù)據(jù)庫名字和路徑

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];

NSString * pathName = [path stringByAppendingString:@"/realmTest.realm"];

RLMRealm * realm = [RLMRealm realmWithURL:[NSURL URLWithString:pathName]];

[realm transactionWithBlock:^{

[realm addObject:presen];

}];

查詢數(shù)據(jù)庫:

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];

NSString * pathName = [path stringByAppendingString:@"/realmTest.realm"];

RLMRealm * realm = [RLMRealm realmWithURL:[NSURL URLWithString:pathName]];

//查詢所有

RLMResults * presons = [SYPreson allObjectsInRealm:realm];

//? ? NSLog(@" === %@",presons);

for (SYPreson * pre in presons) {

//? ? ? ? NSLog(@"pwer == %@",pre.name);

}

//按照年齡查詢,年齡為int類型

RLMResults * ages = [SYPreson objectsInRealm:realm where:@"age = 12"];

for (SYPreson * age in ages) {

//? ? ? ? NSLog(@"age === %@ %ld",age.name,age.age);

}

//按照名字查詢,名字是string類型

RLMResults * dogs = [SYDog objectsInRealm:realm where:@"name = 'dog_2'"];

for (SYDog * dog in dogs) {

//? ? ? ? NSLog(@"dog === %@? %@",dog.name,dog.owner);

}

//多種條件查詢

RLMResults * res = [SYPreson objectsInRealm:realm where:@"age < 12 AND adress = '秦朝'"];

for (SYPreson * p in res) {

//? ? ? ? NSLog(@"------ %@ %@ %ld",p.name,p.adress,p.age);

}

//斷言查詢,查詢速度比較快,推薦使用

//斷言查詢單個數(shù)據(jù)

NSPredicate * pre = [NSPredicate predicateWithFormat:@"ANY dogs.sex = %ld",1];

RLMResults * resPre = [SYPreson objectsInRealm:realm withPredicate:pre];

for (SYPreson * pres in resPre) {

//? ? ? ? NSLog(@"------ %@ %@ %ld",pres.name,pres.adress,pres.age);

}

//斷言查詢多個數(shù)據(jù)條件

NSPredicate * pres = [NSPredicate predicateWithFormat:@"adress = %@",@"秦朝"];

NSPredicate * presN = [NSPredicate predicateWithFormat:@"ANY dogs.name = %@",@"dog_3"];

//鏈式查詢,

RLMResults * resPres = [[SYPreson objectsInRealm:realm withPredicate:pres] objectsWithPredicate:presN];

for (SYPreson * pr in resPres) {

NSLog(@"------ %@ %@ %ld",pr.name,pr.adress,pr.age);

}

修改和刪除都是在查詢的基礎上進行的操作,這里只寫出了簡單的修改和刪除操作

修改數(shù)據(jù)庫

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];

NSString * pathName = [path stringByAppendingString:@"/realmTest.realm"];

RLMRealm * realm = [RLMRealm realmWithURL:[NSURL URLWithString:pathName]];

//查詢出年齡為1的person

RLMResults * per = [SYPreson objectsInRealm:realm where:@"age = 1"];

for (SYPreson * p in per) {

[realm transactionWithBlock:^{

p.name = @"年齡為1,修改名字";

}];

}

刪除數(shù)據(jù)庫

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];

NSString * pathName = [path stringByAppendingString:@"/realmTest.realm"];

RLMRealm * realm = [RLMRealm realmWithURL:[NSURL URLWithString:pathName]];

//刪除某一類元素,這里的查詢操作跟上面查詢操作一樣,查詢到單個或者多個數(shù)據(jù),使用delete方法直接刪除

RLMResults * ages = [SYPreson objectsInRealm:realm where:@"age > 14"];

[realm transactionWithBlock:^{

[realm deleteObjects:ages];

}];

//直接刪除所有數(shù)據(jù)

RLMResults * per = [SYPreson allObjectsInRealm:realm];

[realm transactionWithBlock:^{

//單獨刪除每個數(shù)據(jù),如果要刪除所有的,要使用循環(huán)刪除

//? ? ? ? [realm deleteObject:per.lastObject];

//一次性刪除一個數(shù)組的數(shù)據(jù)

[realm deleteObjects:per];

}];

RLMResults * dogs = [SYDog allObjectsInRealm:realm];

[realm transactionWithBlock:^{

[realm deleteObjects:dogs];

}];

簡單的增刪改查,關于realm更深層次的用法會在以后慢慢更新

demo地址:realmModel

realm 官網(wǎng)地址:realm

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容