Running LiveQuery on Multiple databases

Hi,

I am running the LiveQuery and it is working correctly for single database. However, I want to run it on multiple databases.

Constraints

  1. I do not know beforehand the number of databases on which I want to run LiveQuery
  2. I do not know beforehand the name of databases on which I want to run LiveQuery

Any pointers would be of great help.

//The view gets created…
this.couchbaseDatabase = this.couchbaseManager.getExistingDatabase(authorDBName);

            if (this.couchbaseDatabase != null) {
                com.couchbase.lite.View viewItemsByDate = database.getView(String.format("%s/%s", designDocName, byDateViewName));
                viewItemsByDate.setMap(new Mapper() {
                    @Override
                    public void map(Map<String, Object> document, Emitter emitter) {
                        Object type = document.get("type");
                        if (type != null) {
                            if (type.toString().equals("message"))
                                emitter.emit(type.toString(), document);
                        }
                    }
                }, "1.0");

                startLiveQuery(viewItemsByDate);
            }

//Live query
private void startLiveQuery(com.couchbase.lite.View view) throws Exception {

    if (liveQuery == null) {
        liveQuery = view.createQuery().toLiveQuery();
        liveQuery.addChangeListener(new LiveQuery.ChangeListener() {
            public void changed(final LiveQuery.ChangeEvent event) {
                getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        QueryRow row;
                        Document document;
                        String docType;
                        LogMessenger.printMessage("event.getRows()", event.getRows());
                        for (Iterator<QueryRow> it = event.getRows(); it.hasNext(); ) {
                            row = it.next();
                            document = row.getDocument();

                            if (document != null) {
                                if (document.getProperty("type") != null) {
                                    docType = document.getProperty("type").toString();
                                    if (docType.equalsIgnoreCase("message")) {
                                        updateAdsAndMessagesSummary();
                                    }
                                }
                            }
                        }
                    }
                });
            }
        });

        liveQuery.start();

    }

}

Databases are entirely independent of each other. You’ll need to create an equivalent view in each database, and query that view in each database.

Thanks I already did it this morning.

However, I am facing a new issue. May be need to check replication status. The Android UI is not responding sometimes and it is crashing. Any pointer would be of great help.

SQLiteDatabase: Failed to open database ‘/data/data/com.nectotech.jainpath/files/temple-hutheesingjaintemple-555257.cblite2/db.sqlite3’.
com.couchbase.lite.internal.database.sqlite.exception.SQLiteCantOpenDatabaseException: unable to open database file (code 14): , while compiling: SELECT count(*) FROM sqlite_master
at com.couchbase.lite.internal.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at com.couchbase.lite.internal.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:718)
at com.couchbase.lite.internal.database.sqlite.SQLiteConnection.executeForLong(SQLiteConnection.java:522)
at com.couchbase.lite.storage.SQLiteStorageEngineBase.decrypt(SQLiteStorageEngineBase.java:126)
at com.couchbase.lite.storage.SQLiteStorageEngineBase.access$200(SQLiteStorageEngineBase.java:32)
at com.couchbase.lite.storage.SQLiteStorageEngineBase$ConnectionListener.onOpen(SQLiteStorageEngineBase.java:96)
at com.couchbase.lite.internal.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:198)
at com.couchbase.lite.internal.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:178)
at com.couchbase.lite.internal.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:461)
at com.couchbase.lite.internal.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:191)
at com.couchbase.lite.internal.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:183)
at com.couchbase.lite.internal.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:820)
at com.couchbase.lite.internal.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:803)
at com.couchbase.lite.internal.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:698)
at com.couchbase.lite.storage.SQLiteStorageEngineBase.open(SQLiteStorageEngineBase.java:70)
at com.couchbase.lite.store.SQLiteStore.open(SQLiteStore.java:188)
at com.couchbase.lite.Database.open(Database.java:1251)
at com.couchbase.lite.Manager.openDatabase(Manager.java:335)
at com.couchbase.lite.Manager.getDatabase(Manager.java:299)