//
// ViewController.m
// RealmForSqlite
//
// Created by 陈胜华 on 2017/2/8.
// Copyright © 2017年 oneyd.me. All rights reserved.
//
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITextField *etxfName;
@property (weak, nonatomic) IBOutlet UITextField *etxfAge;
@property (weak, nonatomic) IBOutlet UITableView *etblList;
@property (strong,nonatomic) NSMutableArray<Person*> *dataList;
@property (strong,nonatomic) RLMRealm *realmManger;
@property (strong,nonatomic) Person *selctedPersonEntity;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_realmManger = [RLMRealm defaultRealm];
_dataList = [[NSMutableArray alloc]init];
//第一次初始化,读取数据,显示
RLMResults<Person*> *tempArray = [Person allObjectsInRealm:_realmManger];
[_dataList removeAllObjects];
for (Person *obj in tempArray) {
[_dataList addObject:obj];
}
//刷新数据
[_etblList reloadData];
}
//数据库--增
- (IBAction)onClickAdd:(id)sender {
if (_etxfName.text.length == 0 || _etxfAge.text.length == 0) return;
//对象
Person *person = [[Person alloc]init];
person.name = _etxfName.text;
person.age = [_etxfAge.text intValue];
//增
[_realmManger transactionWithBlock:^{
[_realmManger addObject:person];
[_realmManger commitWriteTransaction];
}];
//读取数据
RLMResults<Person*> *tempArray = [Person allObjectsInRealm:_realmManger];
[_dataList removeAllObjects];
for (Person *obj in tempArray) {
[_dataList addObject:obj];
}
//刷新数据
[_etblList reloadData];
}
//数据库--删
- (IBAction)onClickDelete:(id)sender {
__block BOOL isDelete = false;
RLMResults<Person*> *tempArray = [Person allObjectsInRealm:_realmManger];
if (_etxfName.text.length == 0 && _etxfAge.text.length != 0) {//以age方式delete
[_realmManger transactionWithBlock:^{
for (Person *obj in tempArray) {
if (obj.age == [_etxfAge.text intValue]) {
[_realmManger deleteObject:obj];
isDelete = true;
}
}
[_realmManger commitWriteTransaction];
}];
} else if (_etxfName.text.length != 0 && _etxfAge.text.length == 0) {//以name方式delete
[_realmManger transactionWithBlock:^{
for (Person *obj in tempArray) {
if ([obj.name isEqualToString:_etxfName.text]) {
[_realmManger deleteObject:obj];
isDelete = true;
}
}
[_realmManger commitWriteTransaction];
}];
} else if (_etxfName.text.length != 0 && _etxfAge.text.length != 0) {
[_realmManger transactionWithBlock:^{
for (Person *obj in tempArray) {
if (obj.age == [_etxfAge.text intValue] && [obj.name isEqualToString:_etxfName.text]) {
[_realmManger deleteObject:obj];
isDelete = true;
}
}
[_realmManger commitWriteTransaction];
}];
}
if (isDelete) {
_etxfAge.text = @"";
_etxfName.text = @"";
[self.view endEditing:YES];
}
//重新获取数据库表
tempArray = [Person allObjectsInRealm:_realmManger];
[_dataList removeAllObjects];
for (Person *obj in tempArray) {
[_dataList addObject:obj];
}
//刷新数据
[_etblList reloadData];
}
//数据库--查
- (IBAction)onClickSearch:(id)sender {
//读取数据
RLMResults<Person*> *tempArray = [Person allObjectsInRealm:_realmManger];
NSString *searchSqlite ;
/*---------------------------查询语句说明-------------------------------------*/
/* 查询语句的语法以 ‘NSPredicate’语法为准 */
/*---------------------------查询语句说明-------------------------------------*/
//完全匹配查询
//searchSqlite = [NSString stringWithFormat:@"name = '%@'",_etxfName.text];
//以某个条件开头的查询(半模糊查询)
//searchSqlite = [NSString stringWithFormat:@"name BEGINSWITH '%@' ",_etxfName.text];
//以某个条件结尾的查询(半模糊查询)
//searchSqlite = [NSString stringWithFormat:@"name ENDSWITH '%@' ",_etxfName.text];
//包含某些关键字
//searchSqlite = [NSString stringWithFormat:@"name CONTAINS[c] '%@' ",_etxfName.text];
if (_etxfName.text.length == 0 && _etxfAge.text.length == 0) {//查全部
tempArray = [Person allObjectsInRealm:_realmManger];
} else if(_etxfName.text.length == 0 && _etxfAge.text.length != 0 ){//查年龄
searchSqlite = [NSString stringWithFormat:@"age in {%@}",_etxfAge.text];
tempArray = [Person objectsWhere:searchSqlite];
} else if(_etxfName.text.length != 0 && _etxfAge.text.length == 0){//查姓名
searchSqlite = [NSString stringWithFormat:@"name CONTAINS[c] '%@'",_etxfName.text];
tempArray = [Person objectsWhere:searchSqlite];
} else if(_etxfName.text.length != 0 && _etxfAge.text.length != 0){//模糊查询
searchSqlite = [NSString stringWithFormat:@"name CONTAINS[c] '%@' && age in {%@}",_etxfName.text,_etxfAge.text];
tempArray = [Person objectsWhere:searchSqlite];
}
[_dataList removeAllObjects];
for (Person *obj in tempArray) {
[_dataList addObject:obj];
}
//刷新数据
[_etblList reloadData];
}
//数据库--改
- (IBAction)onClickModel:(id)sender {
if (!_selctedPersonEntity) return ;
//查询
RLMResults<Person*> *tempArray = [Person allObjectsInRealm:[RLMRealm defaultRealm]];
[_realmManger transactionWithBlock:^{
//修改
for (Person *obj in tempArray) {
if ([obj.name isEqualToString:_selctedPersonEntity.name] && obj.age == _selctedPersonEntity.age) {
obj.name = _etxfName.text ;
obj.age = [_etxfAge.text intValue];
}
}
[_realmManger commitWriteTransaction];
}];
_selctedPersonEntity = nil;
_etxfAge.text = @"";
_etxfName.text = @"";
[self.view endEditing:YES];
[_dataList removeAllObjects];
for (Person *obj in tempArray) {
[_dataList addObject:obj];
}
//刷新数据
[_etblList reloadData];
}
//数据库--清空列表
- (IBAction)onClickRemoveList:(id)sender {
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm deleteAllObjects];
[realm commitWriteTransaction];
[_dataList removeLastObject];
[_etblList reloadData];
}
//列表显示
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataList.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([self class])];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:NSStringFromClass([self class])];
}
cell.textLabel.text = [NSString stringWithFormat:@"姓名:%@",_dataList[indexPath.row].name];
cell.detailTextLabel.text = [NSString stringWithFormat:@"年龄:%@",@(_dataList[indexPath.row].age)];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
_selctedPersonEntity = _dataList[indexPath.row];
_etxfName.text = _selctedPersonEntity.name;
_etxfAge.text = [NSString stringWithFormat:@"%@",@(_selctedPersonEntity.age)];
}
@end
#import <Realm/Realm.h>
@interface Person : RLMObject
@property (nonatomic,copy ) NSString *name;
@property (nonatomic,assign) int age;
@end
# platform :ios, ‘8.0’
target 'RealmForSqlite' do
pod 'Realm', '~> 2.2.0'
end