Hi, i have implemented couchbase lite in my Android application. I am facing an issue on getting the already created database. When application is first time installed i create a database instance and use it for database operations like (saving,fetching documents) etc. But when i close the app and relaunch a new instance of database is created hence saved data is lost.
I tried to check if database exists using Database.exists() method but how can i retrieve the already created database instance?
Following is the class i use for all couchbase lite database operations. In constructor the instance couchDB is initialized. On every app relaunch it initializes and create a new resulting in losing all saved data.
public class NoSQLDatabaseManager {
private Context context;
private Database couchDB;
File directory;
private String databaseName;
public Database getCouchDB() {
return couchDB;
}
public File getDirectory() {
return directory;
}
public NoSQLDatabaseManager(Context context, String databaseName) {
try {
CouchbaseLite.init(context);
this.context = context;
this.databaseName = databaseName;
// Set Database configuration
DatabaseConfiguration config = new DatabaseConfiguration();
directory = context.getDir(databaseName, Context.MODE_PRIVATE);
config.setDirectory(directory.toString());
if (Database.exists(databaseName, directory)) {
couchDB= ?? *HOW TO GET THE EXSITING DATABASE*
} else {
// Create Open a couchDB with specified name and configuration
couchDB = new Database(databaseName, config);
//create users document
createBasicDocument();
}
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
}
//this class has all basic functions like creating deleting document etc
public void createDocument(String documentId, String documentType, JSONObject documentObject, NOSQLOperation nosqlOperation) throws JSONException {
try {
documentObject.put(NoSQLUtils.DOCUMENT_TYPE, documentType);
HashMap documentMap = new Gson().fromJson(documentObject.toString(), HashMap.class);
MutableDocument mutableDocument = new MutableDocument(documentId);
mutableDocument.setData(documentMap);
getCouchDB().save(mutableDocument);
nosqlOperation.success(null);
} catch (Exception e) {
nosqlOperation.error(e);
e.printStackTrace();
}
}
private void createBasicDocument(){
//creates general documents
}
}
Can someone help me out on this? Any help will be appreciated.
Thanks