重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
今天就跟大家聊聊有关如何在iOS中使用Realm数据库,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
创新互联公司成立与2013年,是专业互联网技术服务公司,拥有项目网站制作、成都网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元融安做网站,已为上家服务,为融安各地企业和个人服务,联系电话:135182197921.创建实体类
new File翻到最下面点击创建Realm实体类
#import@interface TestBean : RLMObject @property NSString* name; @property NSInteger age; @end // This protocol enables typed collections. i.e.: // RLMArray 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*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.小结
只说了常用的功能,贴上官方中文文档供大家参考
https://realm.io/cn/docs/objc/latest
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数据库有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联网站建设公司行业资讯频道,感谢大家的支持。
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。