SDK: saveDocument with existing doc using ConcurrencyControl.failOnConflict

According to the section “” Implications on Creating Documents with the same Id" in this (official) blog, it states that

Hence, if you want to ensure that you do not inadvertently update an existing document, you must specify the ConcurrenyControl argument with failOnConflict . This will then return an error that you can handle appropriately.

Below is an example in Swift

do {
    let docToSave = MutableDocument(id: "docId")
    if !(try db.saveDocument(docToSave, concurrencyControl: .failOnConflict)) {
        // Document with Id already exists. Handle appropriately
    else {
        // save was a success. docToSave is the new saved revision. Proceed as usual
catch {
    print("Some other error in saving \(error)")

My understanding is that when one tries to save an existing document with failOnConflict, it returns an error.

However, it seems that it’s not always the case:

I tried to save an existing document with the ConcurrencyControl flag set to failOnConflict in two different applications.
One of them does not return an error; while the other does.

Am I missing something here?

Do I need to do some configuration in order that saving document using ConcurrencyControl.failOnConflict will alway return an error on existing document?

I’d have to see a case of it not throwing an error to account for all the moving parts here. I can’t think of a reason for it to be inconsistent off the top of my head. The case you show should consistently fail. Note that if you reuse the document docToSave here instead of creating a brand new one it will succeed.

First, thanks for your quick response.

You’re right. I was reusing the same mutable document to save the existing document, so it was returning true all the time. After I’d changed to use a cloned document with the same id, the call failed on saving the existing document.

By the way, I don’t think this. behavior is documented anywhere.
Is the use of failOnConflict to prevent unintentionally overwriting an existing document officially supported?

Thanks again

This variant of save fails on any operation that would be considered a conflict. Saving a document that already exists is one such operation. The other common one is that the document is modified by replication between retrieval and save. If you consider the history of a document as something like a git branch it is more clear: any branch off of the main branch will be considered a conflict.

An unsaved new mutable document is basically “generation 0”, and so it will conflict with any document that is inside the database. But, for example, if generation 3 exists in the database, you retrieve it and begin to modify it, and then a replication inserts generation 4, your save will then be a conflict since it is a differing generation 4 (i.e. both you and the replicator made an edit which was based on generation 3). With LastWriteWins, your edit will be coerced into generation 5 instead but with FailOnConflict, it will inform you of the issue in case you wish to merge with the other version.

Thanks for the clarification.