How to use shouldCompactOnLaunch in RealmSwift

The example in the documentation ( https://realm.io/docs/swift/latest/#compacting-realms ) is not very clear to me, since I donโ€™t know if the compaction could be called all the time while using the application or only once start up. Whether the implementation below is correct or it would be better to make a separate configuration, including shouldCompactOnLaunch, to be called once at application startup.

If I add mustCompactOnLaunch to the default configuration, I see that the block is called every time I instantiate the scope.

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 } 

And I was even more interesting: what happens if the seal fails? The reason is too small. Will I be able to access the data and the compaction will simply be skipped?

+1
source share
2 answers

So, the solution for me was to create configurations. The configurations are identical except for the shouldCompactOnLaunch block. This config with mustCompactOnLaunch I just use it once when the application starts , so it does not start every time.

Here is a link to a problem with github Realm

If the failure is compressed , the application will continue to use a non-compact version of the database.

0
source

The RLMRealmConfiguration header RLMRealmConfiguration has additional information:

 /** 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; 

Admittedly, we should make it more apparent in the documentation on the site that this block should only be called the first time an instance of Realm is created that represents a specific Realm file.

If you definitely see that this block is being called for the same Realm file several times, please open the problem on the Realm Cocoa GitHub page with detailed playback instructions.

Once you have created an instance of Realm with a specific Configuration , it is usually best to avoid mutating this configuration after the fact. You should not create two different Configuration objects, one with a compaction unit and one without it. Realm internally caches Realm instances based on its Configuration , so you can get unpredictable behavior.

The seal must not be interrupted. The only serious scenario when this becomes a problem will be that your device is already on the verge of running out of hard disk space. Depending on how much empty space was in this kingdom, compacted kingdoms are usually usually much smaller, so the entire operation will not require twice the size of the vault. In iOS, if the system detects that its storage space is low, this will cause a โ€œCleanupโ€ phase, in which the Caches directory of other applications will be cleaned Caches , which in the vast majority of cases alleviates the problem that is sufficient to complete the process.

If all this fails, an attempt to perform the compaction will raise an exception; which your error handling code should catch.

+1
source

Source: https://habr.com/ru/post/1247999/


All Articles