How invalidate affects .realm and how writeToCopyURL affects .realm

Asked 1 years ago, Updated 1 years ago, 44 views

Could you tell me the effect of invalidate on .realm and writeToCopyURL on .realm?

Both functions can reduce the size of the .realm file, but I would like to know what Realm is doing to reduce the file.
The data stored in the realm includes very important information such as personal information.
Please tell me how each function affects .realm and what exactly you are doing.

realm

2022-09-30 21:25

1 Answers

First of all, invalidate() has the effect of indirectly suppressing file growth, but it does not make the file smaller.

Realm manages transactions through a Multiversion current control (MVCC).
Simply put, each transaction has a point-in-time snapshot, and if there is a read in another transaction, it returns a snapshot of the old data to maintain data consistency while the read does not conflict with the write.This may indirectly reduce file size growth.

However, if a transaction is open for a long time, a large amount of snapshot data can be created.invalidate() is a manual method that tells Realm that it is okay to discard any more objects that are currently alive (=snapshots).This is expected to prevent unnecessary snapshots from being created.

Any snapshot space that is no longer needed will be reused later, but Realm will not automatically remove it.So the Realm file size is basically increasing but not decreasing.

However, it is inconvenient to do so, so there is an operation to remove and optimize unnecessary snapshot data that will be retained as free space in the future.

Realm refers to this operation as a compression.The API for the compaction is not published and cannot be called directly.Instead, you can use the writeToCopyURL() method to make the destination file a compressed file.The original file does not become smaller.And it doesn't necessarily get smaller.If there is unnecessary snapshot space, the file size is reduced because it is removed.

For your information, the semi-automatically compacting API is shouldCompactOnLaunch.This allows you to create a copy of the file internally and replace it with the original when you first open Realm.


2022-09-30 21:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.