美文网首页程序员IOSiOS Developer
iOS开发·第三方数据库处理框架:Realm 基本用法

iOS开发·第三方数据库处理框架:Realm 基本用法

作者: 小码僧 | 来源:发表于2017-08-16 14:47 被阅读730次

除了FMDB,Realm也是一种很流行的数据库方案。

1. 官方资料

2. 用法示例

2.1 新建数据模型

Student.h

#import <Realm/Realm.h>
#import <UIKit/UIKit.h>

@interface Student : RLMObject

@property int age;
@property NSString *name;
@property (nonatomic,strong) NSData *showImgData;

@end

// This protocol enables typed collections. i.e.:
// RLMArray<Student>
RLM_ARRAY_TYPE(Student)
2.2 调用前初始化数据库
- (BOOL)initRealm {
   
   self.isFisrtUser= YES;
   [self setDefaultRealmForUser:[CommonUtils getStrValueInUDWithKey:@"account"]];
   self.userNameLbl.text = @"user1";
   config.fileURL = [[[config.fileURL URLByDeletingLastPathComponent]
   URLByAppendingPathComponent:@"user1"]
   URLByAppendingPathExtension:@"realm"];
   NSLog(@"Realm file path: %@", config.fileURL);
   NSError *error;
   _realm = [RLMRealm realmWithConfiguration:config error:&error];
   if (nil != error) {
     NSLog(@"Create Realm Error");
     return NO;
   }
   NSLog(@"create realm success");
   return YES;
}
2.3 增数据
//增
- (IBAction)onAdd:(id)sender {
   if (_nameTF.text.length ==0 || _ageTF.text.length == 0) {
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Empty"
                                                     message:@"Name or Age is empty!"
                                                    delegate:self
                                           cancelButtonTitle:@"Return"
                                           otherButtonTitles:nil];
     [alert show];
     return ;
   }

   Student *s= [Student new];
   s.name = _nameTF.text;
   s.age = [_ageTF.text intValue];
   s.showImgData = UIImagePNGRepresentation(self.showImgeView.image);

   [_realm beginWriteTransaction];
   [_realm addObject:s];
   [_realm commitWriteTransaction];
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sucess"
                                                 message:@"Add Sucess!"
                                                delegate:self
                                       cancelButtonTitle:@"OK"
                                       otherButtonTitles:nil];
   [alert show];
   _allStudents = [Student allObjectsInRealm:_realm];
   [_dataTV reloadData];
}
2.4 删数据
//删
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    Student *s = [_allStudents objectAtIndex:indexPath.row];
    if (s == nil) {
        NSLog(@"s is nil");
        return;
    }
    [_realm beginWriteTransaction];
    [_realm deleteObject:s];
    [_realm commitWriteTransaction];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
2.5 改数据
- (IBAction)onModify:(id)sender {
    
    if (_nameTF.text.length ==0 || _ageTF.text.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Empty"
                                                        message:@"Name or Age is empty!"
                                                       delegate:self
                                              cancelButtonTitle:@"Return"
                                              otherButtonTitles:nil];
        [alert show];
        return ;
    }
    
    
    [_realm beginWriteTransaction];
    _student.name = _nameTF.text;
    _student.age = _ageTF.text.intValue;
    [_realm commitWriteTransaction];
    
    UINavigationController *vc = (UINavigationController *)self.parentViewController;
    [vc popViewControllerAnimated:YES];
}
2.6 查数据
//查
- (IBAction)onQuery:(id)sender {
    if (_filterTF.text.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Empty"
                                                        message:@"Filter Age is empty!"
                                                       delegate:self
                                              cancelButtonTitle:@"Return"
                                              otherButtonTitles:nil];
        [alert show];
        return ;
    }

    NSString *filter = [NSString stringWithFormat:@"age > %d", [_filterTF.text intValue ]];
    if (_realm) {
        _allStudents = [Student objectsInRealm:_realm where:filter];
    }
    
    [_dataTV reloadData];
}
2.7 切换用户

很多App有这样一个需求:第一个账号保存第一个数据库,切换到第二个账号时,再新建第二个数据库,并且无法查看第一个用户的数据,当用户切换回第一个账号时,能继续使用第一个数据库。

- (void)setDefaultRealmForUser:(NSString *)username {
    RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
    
    // 使用默认的目录,但是使用用户名来替换默认的文件名
    config.fileURL = [[[config.fileURL URLByDeletingLastPathComponent]
                       URLByAppendingPathComponent:username]
                      URLByAppendingPathExtension:@"realm"];
    
    // 将这个配置应用到默认的 Realm 数据库当中
    [RLMRealmConfiguration setDefaultConfiguration:config];
}

相关文章

网友评论

    本文标题:iOS开发·第三方数据库处理框架:Realm 基本用法

    本文链接:https://www.haomeiwen.com/subject/rhtkkxtx.html