Keeping data after app uninstall

I sense it might not be a CBL issue but here I go. Due to business requirements, some documents will not be syncing all the way to couch base. But now challenge is user might uninstall the app before they are ready to sync and lose their data.

So I thought I would keep CBL databases outside the app directory. When I reinstall the app though, it crashes immediately with error “file/data is not in the requested format [1, 21] (CouchbaseLite, 21) [CouchbaseLite Android v3.2.1-9@33”

but it opens if I simply relaunch it. Except the unsynced documents are forever lost.

I am setting its path as follows

 private val customPath = File(context.getExternalFilesDir(null), "likaydb")

 Database(
                "likaydagabase",
                DatabaseConfigurationFactory.newConfig(databasePath = customPath.absolutePath),
            )

in the app, I no more see the cblite directories in my app so I guess they are indeed saved else where (in my current emulator: /storage/emulated/0/Android/data/com.likayltd.com/files/likaydb)

whose content I can’t see but given I see data being populated (pulled down), I assume they are there.

I really want to preserve the unsynced data in case of uninstallation. Is my approach okay? What can I do?

Your approach might work on some Androids: What you are trying to do is to use a database in “external storage”, outside your apps sandbox (which normally goes away on uninstall). In general Android really does not want you to do that.

Couchbase Lite does not support Android’s “Storage Access Framework”. We need access to a whole directory of files and that just won’t work.

Different versions of Android have had different rules about the use external storage: dealing with them all is going to be a nightmare. I would imagine that on recent versions, some use of the “MANAGE_EXTERNAL_STORAGE” permissions and a database located in “/sdcard/Android” somewhere would work. Doing that, though, is likely to get your app rejected by the Play Store.

Perhaps it would be better just to warn your users…

1 Like