How to get conflicts

I have two mobile apps. Both with Phonegap and CouchBase Lite, and synchronized to the CouchBase server through Sync_Gateway.
I tried to emulate conflicts modifying the same doc (turning off connection to the server) from each app. It generates two different revisions on each mobile. Then I reestablished the connection to the server and the documents start to sync.
My problem is that I can’t see any conflict. The winner revision is now at the apps and at the server.
I suppose that I should see the conflict, with a conflict view, at the server but I don’t. The view always give me no results.

function(doc,meta) {
   if(meta._conflicts){
      emit(doc.id, null);
   }
}

Couchbase Server doesn’t know about conflicts (or revisions at all, for that matter.) So a Couchbase Server map function will never see a _conflicts property the way it would in CouchDB. There isn’t currently a practical way to detect conflicts in a view, since the revision information is buried inside an opaque property (managed by Sync Gateway) inside the document.

In any case, it’s more common to deal with conflicts on the client side because resolving them often requires user interaction. Couchbase Lite has APIs for detecting and resolving conflicts; see the docs. If you’re using PhoneGap, you’d do it the CouchDB way, with a client-side view that checks the _conflicts property.

1 Like

HI Jens,

The _conflicts property should come back in documents when you query a view with the ‘include_docs’ and ‘conflicts’ set to true. This works ok for standard queries, but has the drawback to retreive the whole document for each query row just to have the the conflict information…

Would there be a way to emit this conflict info in the view’s map ? But it seems that we do not have access to the the _conflict property while emitting docs in a map function for a view on CouchBase Lite so how could we do that ?

Thx

You could file an issue on Sync Gateway on Github requesting such a feature. It’s feasible to implement it, although I’m not sure about the performance implications.

Backing up a bit: What’s the problem you’re trying to solve? It sounds like you have a requirement to resolve conflicts on the server, not the client?

No, we are speaking about resolving conflicts on the couchbase lite client not the server. We speak bout views executed on the client side.

oh, I assumed you were talking about running a view query on the server because in your first message you wrote:

I suppose that I should see the conflict, with a conflict view, at the server but I don’t. The view always give me no results.

Also, your map function has a meta parameter, which is only used by Couchbase Server. Couchbase Lite (like CouchDB) passes only one parameter to the map function.

After digging into the view code a bit and looking back into the CouchDB docs:

  • CouchDB puts a _conflicts property in the doc if the document is conflicted, but this doesn’t seem to be well documented; the only reference I found was in a wiki page: “Views only get the winning revision of a document. However they do also get a _conflicts member if there are any conflicting revisions.”
  • Couchbase Lite doesn’t support this. Probably it should, for compatibility.
  • However, CBL’s REST API does have include_conflicts and only_conflicts parameters on _all_docs queries, i.e. _all_docs?only_conflicts=true will make the query return only documents with conflicts, and the row values will include info about the conflicting revisions.

Hello,
Yes this is what I figured out looking at the code . I wanted to know if there was an other way to do that exactly the same way CouchBD doese it. This way we could construct the view the way we want and return only the fields we need… Unfortunatly as you said, we do not have the meta parameter in the map function in couchbase lite.

Next version probably : )

Thx a lot

I can’t see evidence of the only_conflicts parameter in the docs. And when I use it in Android I just seem to get all documents back - is this an IOS only feature?

Sorry about that; the REST docs were adapted from CouchDB, and are still missing a few of the features we added. I’ve filed a request to add those parameters.

@hideki, do you know if Android supports those two query parameters?

Couchbase Lite Android/Java does not support both only_conflicts and include_conflicts query parameters for _all_docs REST API.

I filed the ticket to CBL Android/Java