参考博客:

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学习总结的更多相关文章

  1. Realm Java的学习、应用、总结

    从React Native珠三角沙龙会议了解到Realm这个开源库,然后开始学习.理解和使用Realm.Realm是跨平台.支持多种主流语言,这里主要是对Realm Java结合实际项目的一些情况进行 ...

  2. shiro学习笔记_0600_自定义realm实现授权

    博客shiro学习笔记_0400_自定义Realm实现身份认证 介绍了认证,这里介绍授权. 1,仅仅通过配置文件来指定权限不够灵活且不方便.在实际的应用中大多数情况下都是将用户信息,角色信息,权限信息 ...

  3. shiro学习笔记_0400_自定义realm实现身份认证

     自定义Realm实现身份认证 先来看下Realm的类继承关系: Realm接口有三个方法,最重要的是第三个方法: a) String getName():返回此realm的名字 b) boolean ...

  4. 【我的Android进阶之旅】Realm数据库学习资料汇总(持续更新)

    介绍 realm是一个跨平台移动数据库引擎,支持iOS.OS X(Objective-C和Swift)以及Android. 2014年7月发布.由YCombinator孵化的创业团队历时几年打造,是第 ...

  5. iOS中 Realm的学习与使用 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 有问题或技术交流可以咨询!欢迎加入! 这篇直接搬了一份官方文档过来看的 由于之前没用markdown搞的乱七八糟的 ...

  6. shiro框架学习-5-自定义Realm

    1. 自定义Realm基础 步骤: 创建一个类 ,继承AuthorizingRealm->AuthenticatingRealm->CachingRealm->Realm 重写授权方 ...

  7. shiro框架学习-3- Shiro内置realm

    1. shiro默认自带的realm和常见使用方法 realm作用:Shiro 从 Realm 获取安全数据 默认自带的realm:idae查看realm继承关系,有默认实现和自定义继承的realm ...

  8. Shiro学习(6)Realm整合

    6.1 Realm [2.5 Realm]及[3.5 Authorizer]部分都已经详细介绍过Realm了,接下来再来看一下一般真实环境下的Realm如何实现. 1.定义实体及关系 即用户-角色之间 ...

  9. Shiro入门学习之自定义Realm实现授权(五)

    一.自定义Realm授权 前提:认证通过,查看Realm接口的继承关系结构图如下,要想通过自定义的Realm实现授权,只需继承AuthorizingRealm并重写方法即可 二.实现过程 1.新建mo ...

随机推荐

  1. percona-toolkit主从同步整理(MySQL)

    前言:MYSQL主从同步架构是目前使用最多的数据库架构之一,尤其是负载比较大的网站,因此对于主从同步的管理也就显得非常重要.而数据作为软件的核心部分,对于其有效的管理显得更为重要.随着时间的推移,软件 ...

  2. GNU Radio下QT GUI Tab Widget的使用方法

    期望显示出的效果: 即将要显示的图放在各自的标签页中. 整体框图: 具体设置: QT GUI Tab Widget的设置: 其中 ID改为自己想改的,这里我写的是display GUI Hint所代表 ...

  3. 用tensorflow实现SVM

    环境配置 win10 Python 3.6 tensorflow1.15 scipy matplotlib (运行时可能会遇到module tkinter的问题) sklearn 一个基于Python ...

  4. 关于WebAssembly

    一.WebAssembly是什么? WebAssembly(缩写为Wasm)是基于堆栈的虚拟机的二进制指令格式.Wasm被设计为一个可移植的目标,用于编译C / C ++ / Rust等高级语言,支持 ...

  5. 基于VS2013的MFC窗体按钮事件触发案例(亲测可用)

    学过python的小朋友们一定对python freeze命令不陌生,这一命令用于导出python安装模块,用于新电脑可以快速的配置安装所需的模块,以便快速的加入项目. 那么我们大可以用 window ...

  6. 【EXE报错】win10运行C#程序保存报错:HTTP 无法注册URL ,进程不具有此命名空间的访问权限

    在win10系统运行C#程序出现以下报错 异常信息 [1]异常信息:HTTP 无法注册 URL http://+:13000/Core/Real/HandheldService/.进程不具有此命名空间 ...

  7. mysql: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

    https://www.cnblogs.com/jpfss/p/9734487.html (mysql.sock错误解决方案)

  8. C++头文件中#pragma once与#ifndef……#define……#endif

    两者功能一样,防止重复包含被多次编译.建议头文件加入#pragma once C++头文件开头的两句与结尾的一句#ifndef <标识>#define <标识>类代码#endi ...

  9. PHP mysqli_kill() 函数

    定义和用法 mysqli_kill() 函数请求服务器杀死一个由 processid 参数指定的 MySQL 线程. 语法 mysqli_kill(connection,processid);   实 ...

  10. 【csp模拟赛5】加减法--宽搜维护联通快

    题目大意: 一开始想用并查集,发现很难维护联通块的代表元素,所以用了宽搜,开数组会炸,所以开一个优先队列维护,每扫完一个联通块,统计答案,清空优先队列,!!千万记住注意数组的大小!!! 代码: #in ...