- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
文档 ( https://realm.io/docs/swift/latest/#compacting-realms ) 中的示例对我来说不是很清楚,因为我不知道压缩是可以在应用程序使用期间一直调用还是仅在启动时调用一次。下面的实现是否正确,或者制作一个单独的配置(包括 shouldCompactOnLaunch 以在应用程序启动时调用一次)会更好吗?
如果我将 shouldCompactOnLaunch 添加到默认配置中,我会在每次创建 Realm 实例时看到该 block 被调用。
Realm.Configuration.defaultConfiguration = Realm.Configuration(schemaVersion: schemaVersion, migrationBlock: migrationBlock,shouldCompactOnLaunch: { totalBytes, usedBytes in
// totalBytes refers to the size of the file on disk in bytes (data + free space)
// usedBytes refers to the number of bytes used by data in the file
// Compact if the file is over 100MB in size and less than 50% 'used'
let oneHundredMB = 100 * 1024 * 1024
print ("totalbytes \(totalBytes)")
print ("usedbytes \(usedBytes)")
if (totalBytes > oneHundredMB) && (Double(usedBytes) / Double(totalBytes)) < 0.7{
print("will compact realm")
}
return (totalBytes > oneHundredMB) && (Double(usedBytes) / Double(totalBytes)) < 0.7
})
do {
// Realm is compacted on the first open if the configuration block conditions were met.
_ = try Realm(configuration: config)
} catch {
// handle error compacting or opening Realm
}
还有一件事对我来说很有趣:如果压缩失败会发生什么?存储空间太少是一个原因。我是否仍然能够访问数据并且只是跳过压缩?
最佳答案
RLMRealmConfiguration
的头文件中有额外的信息:
/**
A block called when opening a Realm for the first time during the life
of a process to determine if it should be compacted before being returned
to the user. It is passed the total file size (data + free space) and the total
bytes used by data in the file.
Return `YES` to indicate that an attempt to compact the file should be made.
The compaction will be skipped if another process is accessing it.
*/
@property (nonatomic, copy, nullable) RLMShouldCompactOnLaunchBlock shouldCompactOnLaunch;
诚然,我们应该在网站上的文档中更清楚地表明,仅应在第一次创建表示特定 Realm 文件的 Realm
实例时调用此 block 。
如果你确实看到这个 block 被同一个 Realm
文件多次调用,请在 Realm Cocoa GitHub page 上提出一个问题。详细的重现步骤。
一旦您使用特定的Configuration
创建了一个Realm
实例,通常最好的做法是避免事后更改该配置。您不应该创建两个不同的 Configuration
对象,一个带有压缩 block ,一个没有。 Realm 根据其 Configuration
在内部缓存 Realm
实例,因此您可能会遇到不可预测的行为。
压缩不应该失败。唯一会成为问题的主要场景是,如果您的设备已经快要用完硬盘空间了。根据该 Realm 中有多少空闲空间,压缩后的 Realms 通常要小得多,因此整个操作不需要 2 倍的存储大小。在 iOS 上,如果系统检测到其存储空间不足,则会触发“清理”阶段,清除其他应用程序的 Caches
目录,这在绝大多数情况下足以缓解问题完成这个过程。
如果所有这些都失败了,执行压缩的尝试将抛出异常;您的错误处理代码应该能够捕捉到。
关于swift - 如何在 RealmSwift 中正确使用 shouldCompactOnLaunch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44407812/
我正在使用 RealmSwift 为我正在构建的 iOS 应用程序创建个人识别码对象。我已经创建了一个构造函数和一些基本函数来检查 pin、输入新 pin 等。我可以使用在 RealmSwift 中创
我已经使用 Realmsvift 在我的应用程序中创建了数据库。这是控制台中的输出。请告诉我,如何在应用程序中从中读取数据?例如,我想显示键值对:Oslo - 2.89。感谢您的帮助。 class V
我正在尝试从我的领域数据库中查询 token 和user_id,然后传递给我的 private var token 和userId,但是我一直抛出此错误 void函数中非预期的非无效返回值 在retu
假设我有很多城市对象。所以,城市 ID 将是我的主键,因为它们不能重复。将会有相同的城市名称,但是,不同的 ID 和不同的 ShopAddress。这是我的 json 结果这是来 self 的后端 {
当我使用 RealmSwift 时,它第一次工作并存储数据。然后,我使用 RealmBrowser 打开它。使用RealmSwift用Realm Browser打开Realm文件后,项目就解散了,错误
我有一个 Report 类,例如: class Report: Object{ @objc dynamic var id : String? = nil @objc dynamic var
这是我第一次在 Swift 上使用 Realm,尽管我通读了整个 Realm 文档,但我似乎无法理解一些简单而基本的概念。 当您在 Realm 中创建一个 Object 类并创建该类的实例时,它会自动
我一直在尝试安装新版本的 realm 0.92.3。我已按照教程中的每一步操作,但仍然不断收到错误消息。 btplpinoudaynpddpsugjevklbvh/Build/Products/Deb
我有这个模型: import Foundation import RealmSwift class Event: Object { dynamic var id
我已经在版本 0.98.2 中安装了 RealmSwift 的动态框架。(CocoaPods 不工作。) 我收到以下错误: module-includes>:1:9: note: in file in
let selectedConsoles = ["Xbox", "Playstation 4"] let players = realm.objects(Person).filter("console
我有一个带有一些字符串大小写的枚举 public enum UserPermission: String, Decodable { case userManagement = "USER_MA
我在理解避免将数据重复写入 Realm 数据库方面遇到了一些问题。我有以下对象模型 Person.swift class Person: Object { let id = RealmOpti
我不知道如何根据用户选择组合谓词。这是我的数据概览。 Person.swift class Person: Object { let id = RealmOptional() dynami
我如何知道特定交易何时完成? 我想在交易完成后运行一段特定的代码。我该怎么做? 我在下面的 war 中执行写入 - do { try realm.write({ realm.add()
我是编程新手,我想知道如何使用 RealmSwift 将数据存储在列表中。 考虑以下模型: import RealmSwift class ScanResults: Object{ dynamic v
我正在为我的项目使用 RealmSwift。但是,我不确定如何解决以下问题: RMLException: Attempting to modify object outside of a write
我有一些从 Realm 中获取的 item: let items = realm.objects(ShoppingListItem.self) print("\(items.count) items"
我有两个类(class): 第一个: class GameObject: Object { @objc dynamic var gameOutcome: String? = nil @
使用 RealmSwift 执行此操作的正确方法是什么,它曾经是 RLMobject var stream:Results stream = Realm().objects(streams) 第一个作
我是一名优秀的程序员,十分优秀!