gpt4 book ai didi

ios - 存储不同的 NSData 时 Realm 文件大小差异过大

转载 作者:可可西里 更新时间:2023-11-01 00:59:19 26 4
gpt4 key购买 nike

我目前正在使用 Swift 开发一个 iOS 项目,我将 Realm 用于我的数据库。我正在存储一个 Realm 对象,其属性之一是一个 NSData 对象(实际上它是一个我转换为 NSData 的 UIImage)。

我的问题很容易理解:当我存储一个大小为 3 Mo 的 NSData 对象时,我的 Realm 文件大小约为 15 Mo。但是当我的 NSData 对象大小为 6 Mo 时,我的 Realm 文件大小变为大约 80莫。

有人遇到过这个问题吗?这种差异有什么原因吗?有什么办法可以解决这个问题吗?

最佳答案

由于某些原因, Realm 文件大小不等于存储的数据大小。

一个是数据的内部版本。

If your Realm file is much larger than you expect, it may be because you have a RLMRealm that is referring to an older version of the data in the database.

In order to give you a consistent view of your data, Realm only updates the active version accessed at the start of a run loop iteration. This means that if you read some data from the Realm and then block the thread on a long-running operation while writing to the Realm on other threads, the version is never updated and Realm has to hold on to intermediate versions of the data which you may not actually need, resulting in the file size growing with each write. The extra space will eventually be reused by future writes, or may be compacted — for example by calling writeCopyToPath:error: .

https://realm.io/docs/objc/latest/#file-size--tracking-of-intermediate-versions

此外,

on mobile platforms, Realm file size start at 4KB and double in size every time more than the current allocation is required. This doubling stops at 32MB, at which point the file grows in increments of 16MB. So 16MB -> 32MB -> 48MB.

https://github.com/realm/realm-cocoa/issues/2631#issuecomment-145566190

这就是文件大小超出预期的原因。

要优化(删除所有可用空间)Realm 文件大小,您可以使用 writeCopyToPath:error: 方法。复制的文件将被压缩。

关于ios - 存储不同的 NSData 时 Realm 文件大小差异过大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37686701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com