Skip to content

Instantly share code, notes, and snippets.

@ramden
Created December 28, 2016 19:28
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 ramden/4e268d31239c4578f6f9a01dfa5bd930 to your computer and use it in GitHub Desktop.
Save ramden/4e268d31239c4578f6f9a01dfa5bd930 to your computer and use it in GitHub Desktop.
// The view
getAllItemsView = Couchbase.getDatabaseInstance().getView("getAllItems");
getAllItemsView.setDocumentType("item");
getAllItemsView.setMap(new Mapper() {
@Override
public void map(Map<String, Object> document, Emitter emitter) {
emitter.emit(document.get("title"), document);
}
}, "2");
// The query
public List<Poco> getAll() {
List<Poco> ret = new ArrayList<>();
Query query = getAllItemsView.createQuery();
//query.setPrefetch(true);
QueryEnumerator result = null;
try {
result = query.run();
} catch (CouchbaseLiteException e) {
e.printStackTrace();
Log.e("LOG", "Error querying view.", e);
return new ArrayList<>();
}
for (Iterator<QueryRow> it = result; it.hasNext(); ) {
QueryRow row = it.next();
String docId = row.getDocumentId();
Log.i("DR", "Found item record with id " + docId);
Poco m = getById(docId);
ret.add(m);
}
return ret;
}
private Poco getById(String docId) {
Document document = Couchbase.getDatabaseInstance().getDocument(docId);
Log.i("LOG", "USER PROPERTIES " + document.getUserProperties().toString());
return mapDocumentToPoco(document);
}
// Creation call
public Document create(Poco item) {
Map<String, Object> properties = new HashMap<>();
Document document = Couchbase.getDatabaseInstance().createDocument();
properties.put("title", item.title);
properties.put("type", "item");
properties.put("subitems", poco.subitems); // subitems = arraylist of Sublist containing one string
try {
document.putProperties(properties);
} catch (CouchbaseLiteException e) {
Log.i("saveMovie", "Failed to write document to Couchbase database!");
}
document.createRevision();
return document;
}
@HodGreeley
Copy link

Your code is doing some unusual things.

  1. Is there a reason to emit the whole document as the value of the view? This will make the view large.
    • You retrieve the document by id anyway in getById, so it's not clear why you would emit it.
  2. create always creates a new document.
    • You persist the document with the call to putProperties, then create a new revision. Is this what you want?

I don't see the code for mapDocumentToPoco. I'm not certain, but I think maybe you're recreating the document, or updating the existing one somehow without the subitem array. You could check the revision ID of your documents returned from the query to verify whether this is happening or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment