Database.addChangeListener not firing callbacks(cordova android)


#1

I am trying to implement some simple change detection on a database. However the call back is never firing. Here is my code. I am using forestDB.

private static Manager dbmgr = null;
private static HashMap<String, Database> dbs = null;

private void initDb(final JSONArray args, final CallbackContext callback) {
cordova.getThreadPool().execute(new Runnable() {
    public void run() {
        try {
            String dbName = args.getString(0);
            if (dbs == null) dbs = new HashMap<String, Database>();
            DatabaseOptions options = new DatabaseOptions();
            options.setCreate(true);
            options.setStorageType(Manager.FORESTDB_STORAGE);
            dbs.put(dbName, dbmgr.openDatabase(dbName, options));
            callback.success("CBL db init success");
        } catch (final Exception e) {
            callback.error(e.getMessage());
        }
    }
});
}

private void changesDatabase(final JSONArray args, final CallbackContext callback) {
    PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
    result.setKeepCallback(true);
    callback.sendPluginResult(result);

    try{
        String dbName = args.getString(0);
        dbs.get(dbName).addChangeListener(new Database.ChangeListener() {
            @Override
            public void changed(Database.ChangeEvent event) {
                List<DocumentChange> changes = event.getChanges();
                for (DocumentChange change : changes) {
                    PluginResult result = new PluginResult(PluginResult.Status.OK, change.getDocumentId());
                    result.setKeepCallback(true);
                    callback.sendPluginResult(result);
                }
            }
        });
    }
    catch(Exception e){}
}

#2

Hi @jfspencer
In your sample code, changeDatabase() method is never called. So Database.addChangeLister(....) method is never executed?
I hope this helps.


#3

thanks @hideki, the call is made from the cordova plugin interface, I just realized I was expecting replication to be set as continuous, but it wasn’t, turning on continuous replication gets it working :slight_smile: