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 ...
 
随机推荐
- javascript只弹出一次框 再次刷新不弹出
			
.打开页面自动弹出 当关闭弹框的时候 设置cookie生存时间 再次刷新页面判断cookie是否失效 <html> <head> <meta charset=&qu ...
 - 低级键盘钩子,在WIN7以上版本的问题
			
最近在项目用到低级键盘钩子.发现一个很奇怪的事情,在开发环境和测试环境下都正常运行的键盘钩子, 到了现场环境,总是偶发性出现 键盘钩子不能用了,而且退出时产生1404 错误. 后经过阅读MSDN 的R ...
 - ubuntu18系统 Qt Error BadAccess
			
现象:在ubuntu18中报错 X Error: BadAccess (attempt to access private resource denied) 10 Extension: 130 ...
 - Rasterize order group
			
增加shade 这里的并行 可以让更多 ...并行只在write那里wait 语法 struct I {float a [[raster_order_group(0)]];};
 - TypeHandler简介及配置(mybatis源码篇)
			
作者:南柯梦 Mybatis中的TypeHandler是什么? 无论是 MyBatis 在预处理语句(PreparedStatement)中设置一个参数时,还是从结果集中取出一个值时,都会用类型处理器 ...
 - np中的随机函数
			
numpy.random.uniform介绍: 1. 函数原型: numpy.random.uniform(low,high,size) ==>也即其他函数是对该函数的进一步封装 功能: ...
 - ...cURL error 60: SSL certificate problem: unable to get local issuer certificate...
			
问题描述: 在做PHP爬虫的时候, 安装了 guzzle 和 dom-crawler 之后, 调用的时候出现问题, 如下 报错内容: Fatal error: Uncaught GuzzleHttp ...
 - IntelliJ IDEA 运行项目的时候提示 Command line is too long 错误
			
在 IntelliJ IDEA 项目运行的时候收到了下面的错误提示: Error running 'Application': Command line is too long. Shorten co ...
 - 在iOS开发中使用icon font的方法
			
http://iconfont.cn/help/iconuse.html 在开发阿里数据iOS版客户端的时候,由于项目进度很紧,项目里的所有图标都是用最平常的背景图片方案来实现.而为了要兼容普通屏与R ...
 - 用单元测试来调试SilverFish AI
			
[TestFixture] public class AiTest { [Test] public void Test() { Settings.Instance.LogFolderPath = @& ...