参考博客:

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. css网页使用自定义字体方法

    @font-face可以加载服务器端的字体到浏览器端,这样设计师就可以不受客户端字体库的限制. 一般来说有四种格式的字体文件即可覆盖所有浏览器.这四种格式为: .EOT:适用于Internet Exp ...

  2. 【2019 CCPC 秦皇岛】J - MUV LUV EXTRA

    原题: 题意: 给你两个整数a和b,再给你一个正小数,整数部分忽略不计,只考虑小数部分的循环节,对于所有可能的循环节,令其长度为l,在小数部分循环出现的长度为p,最后一个循环节允许不完整,但是缺少的部 ...

  3. CodeForces - 1221E Game With String 分类讨论

    首先分析A能获胜的情况 A能获胜 当且仅当A拿完后所有剩下的都<b 所以一旦存在一个大小为X的 且 b<=X<a 则必是后手赢 当X为 a<=x<2*b 的时候 无论A或 ...

  4. 扫描局域网ip存活

    #!/bin/bash # #******************************************************************** #encoding -*-utf ...

  5. Python3-for-enumerate

    languages = ["C", "C++", "Perl", "Python"] for x in language ...

  6. 01_Deepin15 下搭建python开发环境

    https://blog.csdn.net/iimpact/article/details/90239193 https://github.com/josonle/Coding-Now#Linux系统 ...

  7. Can't connect to MySQL server on xxx (10061)

    报错原因,数据库服务没有启动,在JDBC连接mysql数据库时会报错 解决方式,在服务中启用Mysql 备注:运行环境: windows10 x64 JDK 1.8.0_181 mysql-conne ...

  8. Verilog写入变量值到文件语句

    integer signed fid_out1,fid_out2; initial begin fid_out1 = $fopen("dataout_i.txt","w& ...

  9. trigger(type,[data]) 在每一个匹配的元素上触发某类事件。

    trigger(type,[data]) 概述 在每一个匹配的元素上触发某类事件.大理石平台价格表 这个函数也会导致浏览器同名的默认行为的执行.比如,如果用trigger()触发一个'submit', ...

  10. 收藏一个ST表模板

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...