Hi,
I have an Android app that uses Couchbase Lite 1.4.0 (SQLite engine), Sync Gateway 1.4.1 & Couchbase Server 4.5.
I’m trying to resolve conflicts on the client. For simulation purposes we have:
db.setMaxRevTreeDepth(2);
This is the detect & resolve conflicts code:
Query query = database.createAllDocumentsQuery();
query.setAllDocsMode(Query.AllDocsMode.ONLY_CONFLICTS);
QueryEnumerator enumerator = query.run();
while (enumerator.hasNext()) {
QueryRow row = enumerator.next();
List<SavedRevision> conflictRevs = row.getConflictingRevisions();
Map<String, Object> latestDocProps = findLatestDocProps(conflictRevs);
SavedRevision current = row.getDocument().getCurrentRevision();
for (SavedRevision rev : conflictRevs) {
UnsavedRevision newRev = rev.createRevision();
if (rev.getId().equals(current.getId())) {
newRev.setProperties(latestDocProps);
} else {
newRev.setIsDeletion(true);
}
newRev.save(true);
}
private Map<String, Object> findLatestDocProps(List<SavedRevision> conflictRevs) {
Map<String, Object> result = new HashMap<>();
long lastUpdateTime = 0;
for (SavedRevision rev : conflictRevs) {
Map<String, Object> revProps = rev.getUserProperties();
Long updatedAt = Long.valueOf(revProps.get("updatedAt").toString());
if (updatedAt > lastUpdateTime) {
lastUpdateTime = updatedAt;
result = revProps;
}
}
return result;
}
Basically, the conflict resolution code is copy-paste from the docs. The objective is to resolve the conflicts so that only the most recent document (shown by “updatedAt”) stays.
Here’s the scenario:
- The document is created and synced with SG
- The device goes offline
- The document is updated 5 times (
maxRevTreeDepth
is 2) - The device goes online and conflict is detected
- The conflict is resolved locally. The document in SG shows property
new_rev
along withrev
. Locally, there are no more conflicts. That seems like a problem, right?
If the following is commented out:
// newRev.setProperties(latestDocProps);
Everything runs just fine. No new_rev
in SG and the conflict is resolved locally. Any thoughts on the issue?
Thank you!