Created
December 28, 2016 19:28
-
-
Save ramden/4e268d31239c4578f6f9a01dfa5bd930 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your code is doing some unusual things.
getById
, so it's not clear why you would emit it.create
always creates a new document.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.