文章目录
  1. 1. 前言
  2. 2. 字段类型
  3. 3. 升级字段
  4. 4. 迁移文件
  5. 5. 压缩
  6. 6. 用法
  7. 7. 参考资料
  8. 8. 文档信息

前言


Realm Database is an alternative to SQLite and Core Data. Thanks to its zero-copy design, Realm Database is much faster than an ORM, and often faster than raw SQLite. Get started in minutes, not hours.

The Realm Mobile Database works on all major mobile platforms to provide offline-first functionality & data persistence through an easy-to-use API. When paired with the Realm Mobile Platform, all your data is automatically synced, with no work from you.

Realm Database 是一个移动端跨平台数据存储文件系统

字段类型


支持类型
All properties must be primitives, NSString, NSDate, NSData, NSNumber, RLMArray, RLMLinkingObjects, or subclasses of RLMObject.

升级字段


修改字段(删除或新增)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
config.schemaVersion = 20190313;

config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
// We haven’t migrated anything yet, so oldSchemaVersion == 0
if (oldSchemaVersion < 1) {
/*
// The enumerateObjects:block: method iterates
// over every 'Person' object stored in the Realm file
*/
[migration enumerateObjects:Person.className block:^(RLMObject *oldObject, RLMObject *newObject) {
// combine name fields into a single field
newObject[@"fullName"] = [NSString stringWithFormat:@"%@ %@", oldObject[@"firstName"], oldObject[@"lastName"]];
}];
}
if (oldSchemaVersion < 2) {
/*
[migration renamePropertyForClass:Person.className oldName:@"yearsSinceBirth" newName:@"age"];
*/
}
};
[RLMRealmConfiguration setDefaultConfiguration:config];

迁移文件


项目中由于某些原因需要对 realm 存储文件进行迁移,下面可行办法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
NSURL * groupFileUrl = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.devhitao.blog"];
groupFileUrl = [[groupFileUrl URLByAppendingPathComponent:@"document"] URLByAppendingPathExtension:@"realm"];

@autoreleasepool {
// NSURL * defaultFileURL = config.fileURL;
// RLMRealm * rlm = [RLMRealm realmWithURL:defaultFileURL]; // 指定路径

RLMRealm * rlm = [RLMRealm defaultRealm]; // 使用默认配置路径
NSError * copyError;
if (![[NSFileManager defaultManager] fileExistsAtPath:groupFileUrl.relativePath]) {
if ([rlm writeCopyToURL:groupFileUrl encryptionKey:nil error:&copyError]) {
NSLog(@"copy Susess");
}else{
NSLog(@"copy failure");
}
}else{
NSLog(@"已经存在");
}
}
config.fileURL = groupFileUrl;
[RLMRealmConfiguration setDefaultConfiguration:config];

注意:使用上面方法之前,schemaVersion 必须大于 0,也就是说必须提前设置。

另一种方法,你也可以使用 NSFileManager 类对目标文件进行复制。

1
[[NSFileManager defaultManager] copyItemAtURL:config.fileURL toURL:groupFileUrl error:&copyError]

压缩


不知你是否注意到 realm 存储文件大小问题,当 realm 存储文件过大时,轻则会占用手机存储空间,重则会导致 App 崩溃,尤其是在一些可用存储空间不足的设备上,一般错误提示信息为:RLMException mmap() failed: Cannot allocate memory size: 1342177280 offset: 0 ,这时需要我们对 realm 存储文件进行压缩。

首先建议将 realm 升级到最新稳定版本,然后进行压缩,代码如下:

1
2
3
4
5
6
7
8
9
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
config.schemaVersion = 20152020;
config.shouldCompactOnLaunch = ^BOOL(NSUInteger totalBytes, NSUInteger bytesUsed) {
// 压缩
NSUInteger overSizeMB = 500 * 1024 * 1024; //上限 500MB
return (totalBytes > overSizeMB) && (((double)bytesUsed / totalBytes) < 0.5);
};
[RLMRealmConfiguration setDefaultConfiguration:config];
[RLMRealm defaultRealm];

上面代码中,当 realm 存储文件大小超过 500 MB 同时使用率低于 50% 时,会触发压缩条件。

用法


objectsWhere 用法

1
2
3
4
5
6
// Query using a predicate string
RLMResults<Dog *> *tanDogs = [Dog objectsWhere:@"color = 'tan' AND name BEGINSWITH 'B'"];

// Query using an NSPredicate
NSPredicate *pred = [NSPredicate predicateWithFormat:@"color = %@ AND name BEGINSWITH %@", @"tan", @"B"];
tanDogs = [Dog objectsWithPredicate:pred];

参考资料


文档信息


  • 版权声明:自由转载-保持署名-非商用-非衍生 ( CC BY-NC-ND 4.0 )
文章目录
  1. 1. 前言
  2. 2. 字段类型
  3. 3. 升级字段
  4. 4. 迁移文件
  5. 5. 压缩
  6. 6. 用法
  7. 7. 参考资料
  8. 8. 文档信息