Hi,
I am working on an app in Android. getDatabase is working fine when Android application is started. If DB does not exist, it creates one. However, it is not working as expected while browsing the application.
Here is the scenario … When application is started, startSyncingAuthor is called to start multiple pull replications.
startSyncingAuthor(“abc-1”);
startSyncingAuthor(“abc-2”);
startSyncingAuthor(“abc-3”);
public void startSyncingAuthor(String authorDBName) {
if (authorDBName == null)
return;
try {
String serverDbAddress = StaticVariables.HTTP_ADDRESS_WITH_PORT + authorDBName;
this.couchbaseDatabase = this.couchbaseManager.getDatabase(authorDBName);
if (this.couchbaseDatabase != null) {
URL syncServerAddressUrl = new URL(serverDbAddress);
Replication pullReplication = this.couchbaseDatabase.createPullReplication(syncServerAddressUrl);
pullReplication.setContinuous(true);
pullReplication.setFilter("by_type/by_android_replication");
Authenticator auth = new BasicAuthenticator(StaticVariables.USERNAME, StaticVariables.PASSWORD);
pullReplication.setAuthenticator(auth);
pullReplication.start();
}
} catch (CouchbaseLiteException e) {
Crashlytics.log(android.util.Log.ERROR, "", LogMessenger.getMessage(e.getMessage()));
Crashlytics.getInstance().core.logException(e);
e.printStackTrace();
} catch (MalformedURLException e) {
Crashlytics.log(android.util.Log.ERROR, "", LogMessenger.getMessage(e.getMessage()));
Crashlytics.getInstance().core.logException(e);
throw new RuntimeException(e);
} catch (Exception e) {
Crashlytics.log(android.util.Log.ERROR, "", LogMessenger.getMessage(e.getMessage()));
Crashlytics.getInstance().core.logException(e);
e.printStackTrace();
}
}
It works as expected… Later, within the app, the same function startSyncingAuthor is called again but the statement
this.couchbaseDatabase = this.couchbaseManager.getDatabase(authorDBName);
returns null.
It returns null when DB does not exist. As per document it should create DB if it does not exist. If DB already exists, it returns DB as expected.
I tested it separately by creating dummy DBs by calling following function testingNewDBs within the app. Again, getDatabase() is returning null for non existing DBs. However this same function works as expected if called when application is first started.
public void testingNewDBs() {
try {
String authorDBName;
for (int i = 0; i <= 5; i++) {
authorDBName = "new-database" + i;
this.couchbaseDatabase = this.couchbaseManager.getDatabase(authorDBName);
if (this.couchbaseDatabase != null) {
LogMessenger.printMessage("this.couchbaseDatabase", "IS NOT NULL");
} else {
LogMessenger.printMessage("this.couchbaseDatabase", "IS NULL");
}
}
} catch (CouchbaseLiteException e) {
Crashlytics.log(android.util.Log.ERROR, "", LogMessenger.getMessage(e.getMessage()));
Crashlytics.getInstance().core.logException(e);
e.printStackTrace();
} catch (Exception e) {
Crashlytics.log(android.util.Log.ERROR, "", LogMessenger.getMessage(e.getMessage()));
Crashlytics.getInstance().core.logException(e);
e.printStackTrace();
}
}
Any pointers would be of great help.
Thanks