On Tue, May 13, 2014 at 10:59:52AM +0200, Sascha Lüdecke wrote:
> Do have a code sample you can share for using the Document.update()?
Sure. For example, imagine an app that tracks a list of tasks, and the
user has marked a task complete.
public boolean completeTask(String taskId) {
Document doc = mDatabase.getExistingDocument(taskId);
SavedRevision rev = doc.update(new Document.DocumentUpdater() {
@Override
public boolean update(UnsavedRevision newRevision) {
Map<String, Object> props = newRevision.getProperties();
if (DocUtils.getBoolean(props, "completed")) {
// This task has already been marked completed by
// another user -- skip the update.
return false;
}
props.put("completed", true);
props.put("updatedBy", mUserId);
props.put("updatedAt", DocUtils.nowDateString());
return true;
}
});
return (rev != null);
}
(where DocUtils is a homegrown collection of helpers for stuff we have
to do all the time, like convert types into/out of documents).
Obviously, it's much longer than the ORMLite example, although it does a
bit more work:
- In the simplest case, it just updates the document.
- If we get an update conflict because another user marked the task
completed too, we can skip our update.
- Otherwise, if another user made some unrelated change, we'll go ahead
with our update to the "completed" property.
Usually, I've got one class that handles both DB -> domain object and
domain object -> DB translation for each type of document in the system,
so all the DB-related code for each type is in one place. It's not too
different from what ToDo-Lite is doing with static methods, but I prefer
to keep them instance methods on a second class as an easier seam for
testing. Just personal preference.
--
You received this message because you are subscribed to the Google Groups
"Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/mobile-couchbase/20140513133728.GA334%40mqmb.lan.
For more options, visit https://groups.google.com/d/optout.