TheKringar TheKringar - 1 year ago 58
Swift Question

Realm Filesize varies though same data is saved

I am writing an iOS-App that stores sites. Each of the sites has a different address. For a test I created 32k sites and also 32k addresses. When I first created all those datapoint the realm file took up about 9gb of data. To be sure about the amount of space that it takes to store this data I rerun the test several times.

Every time I ran these test, the file size was different, in a range from 25mb to 10gb. I am not sure though, what the problem seems to be. Are there any suggestions what I might look for?

Answer Source

I am more well-versed in Realm-java than Realm-Swift, but if you retain old instances of a Realm that belong to non-looping threads that cannot update themselves (in swift world, apparently that's background threads and dispatch queues), you need to close the Realm instance, and in swift, you need to wrap your Realm in an autoreleasepool in order to auto-close it. Here is the relevant documentation regarding this behavior.

I also think it helps if you reduce the number of transactions you execute on a given background thread.

The old versions that ended up getting retained take up additional space, this can be compacted using compactRealm. For that to work, all instances of the Realm need to be closed on every thread.