Hi,
I am running the LiveQuery and it is working correctly for single database. However, I want to run it on multiple databases.
Constraints
- I do not know beforehand the number of databases on which I want to run LiveQuery
- 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();
}
}