Adding writeCopy() to Realm

The method `writeCopy()` writes the entire Realm to a new write. During the write, the data is compacted, and the resulting file is smaller. The method is useful when an app migrates to a new version, and it wishes to reduce the file size to a minimum.

@bmunkholm @cmelchior @emanuelez 
> writeCopy is a very confusing name imo, also why do we do it to another
file? What is the use case for that?

The main use cases is actually not to compact the existing file, but to be
able to pack up all the data in a separate compact file that you can send
to another device. This is especially useful on the server side, where you
can prepare the file for distribution to all devices, but it could also be
useful for transmitting large amount of data home.

You cannot do this with the realm that is in use, as others may be writing
to it while you copy it. But since the writeCopy method works within a read
transaction, it can write out a new compact file, even while the original
realm is concurrently being modified.


On Tue, Nov 25, 2014 at 10:27 AM, Christian Melchior <
notifications@github.com> wrote:

> In realm/src/main/java/io/realm/Realm.java:
>
> > @@ -488,6 +488,22 @@ private static Realm createAndValidate(String absolutePath, byte[] key, boolean
> >      }
> >
> >      /**
> > +     * Write a compacted copy of the Realm to the given path.
> > +     *
> > +     * The destination file cannot already exist.
> > +     *
> > +     * Note that if this is called from within a write transaction it writes the
> > +     * current data, and not data when the last write transaction was committed.
> > +     *
> > +     * @param path Path to save the Realm to
> > +     * @throws
> > +     */
> > +    public void writeCopy(String path) {
>
> writeCopy is a very confusing name imo, also why do we do it to another
> file? What is the use case for that? From a user standpoint I would call
> this realm.compressFile() or something similar. While it might write an
> intermediate copy behind my back, that is an implementation detail.
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/realm/realm-java/pull/616/files#r20883477>.
>