您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關如何在iOS中使用Realm數據庫,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
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.小結
只說了常用的功能,貼上官方中文文檔供大家參考
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數據庫有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。