Android prebuild Database with inputstream CB 2.1

java
#1

Hello is there a way to copy the preloaded database from an input stream, currently we are experimenting an app loading issue copying the database twice, once from assets folder to temp file and then from temp file to Database.copy(), not sure if there is a way to send the file directly from apk or just send you the input stream to avoid writing the file again.

#2

You might want to have a look at this code, to see if it addresses your issue:

#4

don’t you need to replace UUIDs through Database.copy() method?

#5

That code works. There is some dubious stuff in that app, but it is not that bit.

#6

I do think that the call to Database.copy() is needed. It will work for one app without it, but do it on more than one and you are in trouble since they will share checkpoints in Sync Gateway and step on each others’ replication progress.

#7

Yes, as Jim mentioned, in a real world app, you must use Database.copy() so you will have a unique database with each instance of deployed app. The iOS version of the Travel sample app (referenced above) uses Database.copy(). I have updated the Android version
From https://github.com/couchbaselabs/mobile-travel-sample/blob/master/android/app/src/main/java/com/couchbase/travelsample/util/DatabaseManager.java#L61

 public void OpenDatabaseForUser(Context context,String username) {
        File dbFile = new File(context.getFilesDir()+"/"+ username, "travel-sample.cblite2");
        DatabaseConfiguration config = new DatabaseConfiguration(context);
        config.setDirectory(String.format("%s/%s", context.getFilesDir(),username));
        currentUser = username;

        if (!dbFile.exists()) {
            AssetManager assetManager = context.getAssets();
            try {
                File path = new File(context.getFilesDir()+"");
                unzip(assetManager.open("travel-sample.cblite2.zip"),path);
                Database.copy(new File(context.getFilesDir(),"travel-sample.cblite2"), "travel-sample", config);

            }
            catch (IOException e) {
                e.printStackTrace();
            }
            catch (CouchbaseLiteException e) {
                e.printStackTrace();
            }

        }
        try {
            database = new Database("travel-sample", config);
            createFTSQueryIndex();
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }
    }