Skip to content

Instantly share code, notes, and snippets.

@kombadzomba
Last active September 16, 2016 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kombadzomba/fcd27a0a439aa7413f1f12b4e4e354d4 to your computer and use it in GitHub Desktop.
Save kombadzomba/fcd27a0a439aa7413f1f12b4e4e354d4 to your computer and use it in GitHub Desktop.
public synchronized List<? extends CommonEntity> getData() {
Query orderedQuery = view.createQuery();
orderedQuery.setStartKey(startKey);
orderedQuery.setEndKey(endKey);
orderedQuery.setDescending(descending);
// data is protected variable
if (data == null) {
data = new LinkedList();
}
data.clear();
try {
QueryEnumerator results = orderedQuery.run();
if (view.getReduce() == null) {
// map only
for (Iterator<QueryRow> it = results; it.hasNext(); ) {
QueryRow row = it.next();
addDocumentToData(row.getDocument());
}
} else {
// map reduce
QueryEnumerator result = orderedQuery.run();
QueryRow aggregate = result.next();
if (getReduceResultClass() == List.class) {
if (aggregate != null && aggregate.getValue() != null) {
data.addAll((Collection) aggregate.getValue());
}
} else if (getReduceResultClass() == Number.class) {
data.add(aggregate == null ? 0 : (Number) aggregate.getValue());
}
}
} catch (CouchbaseLiteException e) {
Log.e(TAG, String.format("Error querying view %s.", getViewName()), e);
}
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment