realm / realm-kotlin

Kotlin Multiplatform and Android SDK for the Realm Mobile Database: Build Better Apps Faster.
Apache License 2.0
889 stars 52 forks source link

[Feature request] Write Realm copy to OutputStream #289

Open lukaspieper opened 3 years ago

lukaspieper commented 3 years ago

I hope it's OK to cross-post my issue here. I thought it might be easier to consider it here, since this is still an early stage of development.

I saw in PR #116 that there is a class RealmFile, maybe that would be a good starting point. From my point of view, however, the class should not be path-based, but have some kind of (read and) write methods that can then be implemented under Android via stream.

I think the SAF issue will get even more attention once developers are forced to raise the target SDK accordingly.

Original issue text (realm-java)

Describe your problem or use case

I want to allow the user to create a backup of the Realm database. This is already perfectly possible with writeCopyTo() and writeEncryptedCopyTo(). Due to the new file access limitations in Android 10/11 I am forced to use the Storage Access Framework (SAF). Because of this I cannot provide an instance of java.io.File but a java.io.OutputStream.

Describe the solution you'd like

I would like to have an overload of writeCopyTo() and writeEncryptedCopyTo() that takes java.io.OutputStream as parameter.

Additional context

To be fair, it is currently possible to work around this issue by creating a copy in internal storage with the existing methods, and to move this copy to external storage via InputStream and OutputStream. But this adds a lot of overhead.

cmelchior commented 3 years ago

Hi @lukaspieper

Yes, it is fine to crosspost here. You are correct, we might as well add these overloads from the beginning when implementing the APi's for Kotlin. They have just not been prioritized for Java since, as you mention, a workaround do exist.

nguyencongbinh commented 2 years ago

@lukaspieper @cmelchior on Android 11, I would like to using writeCopyTo() to create a backup file (.realm) then immediately share the file (via Intent) to let user share this file via other apps (Whatsapp, Gmail, Telegram...). Can you please give me an example code in this case?