日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Realm学习总结

發布時間:2023/11/29 编程问答 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Realm学习总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考博客:

http://www.jianshu.com/p/096bec929f2a

http://www.cnblogs.com/ilyy/p/5648051.html

參考的博客介紹很詳細,我就不寫了..寫了一個簡單的學習的demo.

GitHub地址:?https://github.com/PengSiSi/RealmDemo


?

代碼如下:

?

// // ViewController.m // RealmDemo // // Created by 思 彭 on 2017/7/20. // Copyright ? 2017年 思 彭. All rights reserved.// 注意區別默認的和自己自定義realm的 #import "ViewController.h" #import "PersonModel.h" #import <Realm.h> #import <RLMRealm.h>@interface ViewController () {RLMRealm *_customRealm; }@property (weak, nonatomic) IBOutlet UITextField *nameTextField; @property (weak, nonatomic) IBOutlet UITextField *sexTextField; @property (weak, nonatomic) IBOutlet UITextField *ageTextField;@property (nonatomic, strong) RLMResults *locArray; @property (nonatomic, strong) RLMNotificationToken *token;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad]; // 可以使用默認的 // _customRealm = [RLMRealm defaultRealm];//自己創建一個新的RLMRealmNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *pathStr = paths.firstObject;// pathStr = /Users/sipeng/Library/Developer/CoreSimulator/Devices/59E51096-9523-4845-84E8-2BB5360FB50E/data/Containers/Data/Application/A20B045E-6C86-4872-99DF-A52541FB1104/Documents NSLog(@"pathStr = %@",pathStr);_customRealm = [RLMRealm realmWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",pathStr,@"person.realm"]]]; }/**增@param sender <#sender description#>*/ - (IBAction)addAction:(id)sender {// 獲取默認的 Realm 實例 // RLMRealm *realm = [RLMRealm defaultRealm]; PersonModel *person = [[PersonModel alloc]init];person.name = self.nameTextField.text;person.sex = self.sexTextField.text;person.age = [self.ageTextField.text integerValue];NSLog(@"name - %@ sex = %@ age = %ld",person.name, person.sex, person.age);// 數據持久化[_customRealm transactionWithBlock:^{[_customRealm addObject:person];}];// 通過事務將數據添加到 Realm 中 // [_customRealm beginWriteTransaction]; // [_customRealm addObject:person]; // [_customRealm commitWriteTransaction];NSLog(@"增加成功啦");[self findAction:nil]; }/**刪@param sender <#sender description#>*/ - (IBAction)deleteAction:(id)sender {// 獲取默認的 Realm 實例 // RLMRealm *realm = [RLMRealm defaultRealm]; [_customRealm beginWriteTransaction];[_customRealm deleteAllObjects];[_customRealm commitWriteTransaction];[self findAction:nil]; }/**改@param sender <#sender description#>*/ - (IBAction)updateAction:(id)sender {for (PersonModel *person in self.locArray) {NSLog(@"name - %@ sex = %@ age = %ld",person.name, person.sex, person.age);}// 獲取默認的 Realm 實例 // RLMRealm *realm = [RLMRealm defaultRealm];PersonModel *model = self.locArray[0];[_customRealm beginWriteTransaction];model.name = @"思思棒棒噠";[_customRealm commitWriteTransaction];NSLog(@"修改成功");for (PersonModel *person in self.locArray) {NSLog(@"name - %@ sex = %@ age = %ld",person.name, person.sex, person.age);} }/**查@param sender <#sender description#>*/ - (IBAction)findAction:(id)sender {//自己創建一個新的RLMRealmNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *pathStr = paths.firstObject;NSLog(@"pathStr = %@",pathStr);// 查詢指定的 Realm 數據庫RLMRealm *personRealm = [RLMRealm realmWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",pathStr,@"person.realm"]]];// 獲得一個指定的 Realm 數據庫self.locArray = [PersonModel allObjectsInRealm:personRealm]; // 從該 Realm 數據庫中,檢索所有model// 這是默認查詢默認的realm // self.locArray = [PersonModel allObjects];NSLog(@"self.locArray.count = %ld",self.locArray.count); }// 創建數據庫 - (void)creatDataBaseWithName:(NSString *)databaseName{NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *path = [docPath objectAtIndex:0];NSString *filePath = [path stringByAppendingPathComponent:databaseName];NSLog(@"數據庫目錄 = %@",filePath);RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];config.fileURL = [NSURL URLWithString:filePath]; // config.objectClasses = @[MyClass.class, MyOtherClass.class];config.readOnly = NO;int currentVersion = 1.0;config.schemaVersion = currentVersion;config.migrationBlock = ^(RLMMigration *migration , uint64_t oldSchemaVersion) { // 這里是設置數據遷移的blockif (oldSchemaVersion < currentVersion) {}};[RLMRealmConfiguration setDefaultConfiguration:config]; }@end

?

轉載于:https://www.cnblogs.com/pengsi/p/7210981.html

總結

以上是生活随笔為你收集整理的Realm学习总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。