欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

基于iOS Realm数据库的使用实例详解

程序员文章站 2023-12-19 08:05:40
首先下载realm源代码, 将下载的文件解压,从 ios/static/ 目录中将 realm.framework 拖曳到 xcode 工程的文件导航器内,然后在 xco...

首先下载realm源代码,

将下载的文件解压,从 ios/static/ 目录中将 realm.framework 拖曳到 xcode 工程的文件导航器内,然后在 xcode 文件导航器中选中工程。然后选择应用目标,前往 build phases 选项卡。在 link binary with libraries 部分中单击 + 按钮,然后添加 libc++.tbd 和 libz.tbd。这样还没有完,我们还需要安装插件,打开解压文件夹下的plugin,运行realmplugin.xcodeproj,然后重启xcode。

这样环境配置就完了,现在说重点如何去使用。

1.创建实体类

new file翻到最下面点击创建realm实体类

#import <realm/realm.h>
@interface testbean : rlmobject
@property nsstring* name;
@property nsinteger age;

@end
// this protocol enables typed collections. i.e.:
// rlmarray<testbean *><testbean>
rlm_array_type(testbean)
#import "testbean.h"
@implementation testbean

@end

2.增删改查

这个增加,可以看到直接添加实体类就行了,这个rlmrealm就是数据库的操作类,以后所有操作都靠他。

-(void)addaction{
 nslog(@"add");
 self.name = _tf_name.text;
 self.age = [_tf_age.text integervalue];

 testbean* bean = [[testbean alloc]init];
 bean.name = self.name;
 bean.age = self.age;
 rlmrealm *realm = [rlmrealm defaultrealm];
 [realm transactionwithblock:^{
  [realm addobject:bean];
 }];

}

删除也是通过删除实体类来实现,但是我们需要首先通过查询出实体类才行。这个查询我们可以看到通过我们创建的实体类就能实现

-(void)deleteaction{

 nslog(@"delete");
 rlmresults *dogs = [testbean allobjects];
 rlmrealm *realm = [rlmrealm defaultrealm];
 [realm transactionwithblock:^{
  for (testbean *bean in dogs) {
   [realm deleteobject:bean];
  }
 }];
}

更新也是通过改变实体类的数据来实现

-(void)updateaction{
 nslog(@"update");
 rlmresults *dogs = [testbean allobjects];
 [[rlmrealm defaultrealm] transactionwithblock:^{
  for (testbean *bean in dogs) {
   bean.name = @"李四";
  }
 }];
}

查询我们还可以添加条件

// 使用断言字符串来查询
rlmresults<dog *> *tandogs = [dog objectswhere:@"color = 'tan' and name beginswith 'b'"];
// 使用 nspredicate 来查询
nspredicate *pred = [nspredicate predicatewithformat:@"color = %@ and name beginswith %@",
              @"tan", @"b"];
tandogs = [dog objectswithpredicate:pred];

3.排序

这个是在查询完后去实现,通过实体类的key来排序。

-(void)sortaction{
 nslog(@"query");
 rlmresults *dogs = [[testbean allobjects] sortedresultsusingkeypath:@"name" ascending:yes];
 [[rlmrealm defaultrealm] transactionwithblock:^{
  for (testbean *bean in dogs) {
   nslog(@"%@",bean.name);
  }
 }];

}

4.小结

只说了常用的功能,贴上官方中文文档供大家参考

5.例子代码

//
// viewcontroller.m
// bmobtest
//
// created by shanreal-ios on 2017/12/9.
// copyright © 2017年 shanreal.longzhenhao. all rights reserved.
//
#import "viewcontroller.h"
#import "testbean.h"
@interface viewcontroller ()
@property(nonatomic,strong)uibutton* add;
@property(nonatomic,strong)uibutton* delete;
@property(nonatomic,strong)uibutton* update;
@property(nonatomic,strong)uibutton* query;
@property(nonatomic,strong)uibutton* sort;

@property(nonatomic,strong)nsstring* name;
@property(nonatomic,assign)nsinteger* age;
@property(nonatomic,strong)uitextfield* tf_name;
@property(nonatomic,strong)uitextfield* tf_age;
@end
@implementation viewcontroller
- (void)viewdidload {
 [super viewdidload];
 // do any additional setup after loading the view, typically from a nib.

 _add = [[uibutton alloc]initwithframe:cgrectmake(10, 30, 100, 20)];
 [_add settitle:@"add" forstate:uicontrolstatenormal];
 [_add settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal];
 [_add addtarget:self action:@selector(addaction) forcontrolevents:uicontroleventtouchupinside];
 _delete = [[uibutton alloc]initwithframe:cgrectmake(10, 60, 100, 20)];
 [_delete settitle:@"delete" forstate:uicontrolstatenormal];
 [_delete settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal];
 [_delete addtarget:self action:@selector(deleteaction) forcontrolevents:uicontroleventtouchupinside];
 _update = [[uibutton alloc]initwithframe:cgrectmake(10, 90, 100, 20)];
 [_update settitle:@"update" forstate:uicontrolstatenormal];
 [_update settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal];
 [_update addtarget:self action:@selector(updateaction) forcontrolevents:uicontroleventtouchupinside];

 _query = [[uibutton alloc]initwithframe:cgrectmake(10, 120, 100, 20)];
 [_query settitle:@"query" forstate:uicontrolstatenormal];
 [_query settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal];
 [_query addtarget:self action:@selector(queryaction) forcontrolevents:uicontroleventtouchupinside];
 _sort = [[uibutton alloc]initwithframe:cgrectmake(10, 150, 100, 20)];
 [_sort settitle:@"sort" forstate:uicontrolstatenormal];
 [_sort settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal];
 [_sort addtarget:self action:@selector(sortaction) forcontrolevents:uicontroleventtouchupinside];

 _tf_name = [[uitextfield alloc]initwithframe:cgrectmake(50, 300, 200, 20)];
 _tf_name.placeholder=@"name";
 _tf_age = [[uitextfield alloc]initwithframe:cgrectmake(50, 350, 200, 20)];
 _tf_age.placeholder=@"age";
 [self.view addsubview:_add];
 [self.view addsubview:_delete];
 [self.view addsubview:_update];
 [self.view addsubview:_query];
 [self.view addsubview:_sort];
 [self.view addsubview:_tf_name];
 [self.view addsubview:_tf_age];
}
-(void)addaction{
 nslog(@"add");
 self.name = _tf_name.text;
 self.age = [_tf_age.text integervalue];

 testbean* bean = [[testbean alloc]init];
 bean.name = self.name;
 bean.age = self.age;
 rlmrealm *realm = [rlmrealm defaultrealm];
 [realm transactionwithblock:^{
  [realm addobject:bean];
 }];

}
-(void)deleteaction{

 nslog(@"delete");
 rlmresults *dogs = [testbean allobjects];
 rlmrealm *realm = [rlmrealm defaultrealm];
 [realm transactionwithblock:^{
  for (testbean *bean in dogs) {
   [realm deleteobject:bean];
  }
 }];
}
-(void)updateaction{
 nslog(@"update");
 rlmresults *dogs = [testbean allobjects];
 [[rlmrealm defaultrealm] transactionwithblock:^{
  for (testbean *bean in dogs) {
   bean.name = @"李四";
  }
 }];
}
-(void)queryaction{
 nslog(@"query");
 rlmresults *dogs = [testbean allobjects];
 [[rlmrealm defaultrealm] transactionwithblock:^{
  for (testbean *bean in dogs) {
   nslog(@"%@",bean.name);
  }
 }];

}
-(void)sortaction{
 nslog(@"query");
 rlmresults *dogs = [[testbean allobjects] sortedresultsusingkeypath:@"name" ascending:yes];
 [[rlmrealm defaultrealm] transactionwithblock:^{
  for (testbean *bean in dogs) {
   nslog(@"%@",bean.name);
  }
 }];

}
- (void)didreceivememorywarning {
 [super didreceivememorywarning];
 // dispose of any resources that can be recreated.
}

@end

以上这篇基于ios realm数据库的使用实例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:

下一篇: