-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.m
67 lines (50 loc) · 2.05 KB
/
ViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// ViewController.m
// realm_objc
//
// Created by work on 2019/5/11.
// Copyright © 2019 XQ. All rights reserved.
//
#import "ViewController.h"
#import "DataModel.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// DataModel *data = [[DataModel alloc] initWithValue:@{@"time":[NSDate dateWithTimeIntervalSinceNow:0],@"title":@"test"}];
// DataModel *data1 = [[DataModel alloc] initWithValue:@[[NSDate dateWithTimeIntervalSinceNow:0],@"test"]];
// NSLog(@"%@",data1.time);
DataModel *myDog = [[DataModel alloc] init];
myDog.name = @"myDog";
DataModel *yourDog = [[DataModel alloc] init];
yourDog.name = @"yourDog";
// Person *me = [[Person alloc] initWithValue:@[@"crylown",[NSDate dateWithTimeIntervalSinceNow:1],@[myDog,yourDog]]];
Person *mes = [[Person alloc] initWithValue:@{@"name":@"crylown",@"birthdate":[NSDate dateWithTimeIntervalSinceNow:1],@"dogs":@[myDog,yourDog]}];
yourDog.owner = mes;
myDog.owner = mes;
[self setDefaultRealmForUser:@"user"];
RLMRealm *realm = [RLMRealm defaultRealm];
// 开放RLMRealm事务
[realm beginWriteTransaction];
// 添加到数据库 me为RLMObject
[realm addObject:mes];
// 提交事务
[realm commitWriteTransaction];
RLMResults *dogs = [Person allObjects];
NSLog(@"%@",dogs);
}
- (void)setDefaultRealmForUser:(NSString *)username {
//先获取默认配置
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
//设置只读数据库
//config.readOnly = YES;
// 使用默认的目录,但是使用用户名来替换默认的文件名
config.fileURL = [[[config.fileURL URLByDeletingLastPathComponent]
URLByAppendingPathComponent:username]
URLByAppendingPathExtension:@"realm"];
// 将这个配置应用到默认的 Realm 数据库当中
[RLMRealmConfiguration setDefaultConfiguration:config];
}
@end