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]; //自己创建一个新的RLMRealm
NSArray *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[];
[_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 { //自己创建一个新的RLMRealm
NSArray *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:];
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) { // 这里是设置数据迁移的block
if (oldSchemaVersion < currentVersion) {
}
}; [RLMRealmConfiguration setDefaultConfiguration:config];
} @end
Realm学习总结的更多相关文章
- Realm Java的学习、应用、总结
从React Native珠三角沙龙会议了解到Realm这个开源库,然后开始学习.理解和使用Realm.Realm是跨平台.支持多种主流语言,这里主要是对Realm Java结合实际项目的一些情况进行 ...
- shiro学习笔记_0600_自定义realm实现授权
博客shiro学习笔记_0400_自定义Realm实现身份认证 介绍了认证,这里介绍授权. 1,仅仅通过配置文件来指定权限不够灵活且不方便.在实际的应用中大多数情况下都是将用户信息,角色信息,权限信息 ...
- shiro学习笔记_0400_自定义realm实现身份认证
自定义Realm实现身份认证 先来看下Realm的类继承关系: Realm接口有三个方法,最重要的是第三个方法: a) String getName():返回此realm的名字 b) boolean ...
- 【我的Android进阶之旅】Realm数据库学习资料汇总(持续更新)
介绍 realm是一个跨平台移动数据库引擎,支持iOS.OS X(Objective-C和Swift)以及Android. 2014年7月发布.由YCombinator孵化的创业团队历时几年打造,是第 ...
- iOS中 Realm的学习与使用 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博! 有问题或技术交流可以咨询!欢迎加入! 这篇直接搬了一份官方文档过来看的 由于之前没用markdown搞的乱七八糟的 ...
- shiro框架学习-5-自定义Realm
1. 自定义Realm基础 步骤: 创建一个类 ,继承AuthorizingRealm->AuthenticatingRealm->CachingRealm->Realm 重写授权方 ...
- shiro框架学习-3- Shiro内置realm
1. shiro默认自带的realm和常见使用方法 realm作用:Shiro 从 Realm 获取安全数据 默认自带的realm:idae查看realm继承关系,有默认实现和自定义继承的realm ...
- Shiro学习(6)Realm整合
6.1 Realm [2.5 Realm]及[3.5 Authorizer]部分都已经详细介绍过Realm了,接下来再来看一下一般真实环境下的Realm如何实现. 1.定义实体及关系 即用户-角色之间 ...
- Shiro入门学习之自定义Realm实现授权(五)
一.自定义Realm授权 前提:认证通过,查看Realm接口的继承关系结构图如下,要想通过自定义的Realm实现授权,只需继承AuthorizingRealm并重写方法即可 二.实现过程 1.新建mo ...
随机推荐
- 实用: 将程序的内容写出到excel中
pom <!-- 读取excel文件 --><dependency> <groupId>org.apache.poi</groupId> <art ...
- vue-cli3.0 环境变量与模式
vue-cli3.0移除了配置文件目录: config和build文件夹.可以说是非常的精简了,那移除了配置文件目录后如何自定义配置环境变量和模式呢? 为什么需要配置环境变量和模式呢? 所有方法肯定是 ...
- iOS 中通过kvc 获取数组的均值、求和、最大最小值等
NSArray *values = @[@, @, @, @, @, @, @, @, @, @, @, @, @, @, @, @, @]; NSNumber *avg = [values valu ...
- lnmp配置
yum源切换 下载wegt工具 yum install -y wget 备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS- ...
- Python3-def
def hello(): print("这是一个无参数的方法!") hello(); print("") def helloOne(str): print(st ...
- hdu 6074 Phone Call
题 O∧O http://acm.hdu.edu.cn/showproblem.php?pid=6074 2017 Multi-University Training Contest - Team 4 ...
- javascript---查找节点
快捷键: chazhaojiedian(查找节点) chuangjianjiedian(创建节点) 使用childNodes childElementCount//=====以下是第一种操作D ...
- 019_linuxC++之_函数模板引入
(一)首先我们来看非模板程序,函数只是输入不一样的变量就需要构件很多个不一样的函数,那么这样很麻烦,则引入函数模板 int& max(int& a, int& b) { ret ...
- Vue 项目中对路由文件进行拆分(解构的方法)
项目需求场景: 在开发项目过程中,在项目过于庞大,路由信息非常多的情况下,如果将路由配置信息都放在一个文件里面,那么这个JS是不方便维护的, 那么,这个时候需要我们把这个庞大的路由文件,根据项目功能分 ...
- BZOJ 1195: [HNOI2006]最短母串 AC自动机+状压+搜索
思路比较直接. 由于 $n$ 很小,直接定义 $f[i][j]$ 表示当前在自动机中的节点 $i,$ 被覆盖串的集合为 $j$ 的方案数. #include <bits/stdc++.h> ...