Multiple times update doc crash :com.couchbase.lite.CouchbaseLiteException: Conflicts not allowed and there is already an existing doc with id: 129047
liufsd opened this issue · 8 comments
I have create document, and then i have update document multiple times.but crash,
public final void saveOrUpdate()
{
// Create document if required
if (mDocument == null) {
mDocument = database.getDocument(getDocumentUniqueId());
UnsavedRevision revision = mDocument.createRevision();
revision.setUserProperties(mProperties);
revision.save();
}else{
// com.couchbase.lite.CouchbaseLiteException: Conflicts not allowed and there is already an existing doc with id: 129047
mDocument.putProperties(mProperties);
}
}
Why ???
How to update a document???
Thanks
So i update my code:
create:
mDocument = database.getDocument(getDocumentUniqueId());
UnsavedRevision revision = mDocument.createRevision();
revision.setUserProperties(mProperties);
revision.save();
and update:
try {
Document document = DBController.getInstance().getCouchDatabase().getDocument(reviewInfoDocument.getDocumentUniqueId());
Map<String, Object> baseMaps = new HashMap<String, Object>();
baseMaps.putAll(document.getProperties());
boolean change = false;
for (Map.Entry<String, Object> entry : reviewInfoDocument.getProperties().entrySet()) {
if (baseMaps.containsKey(entry.getKey())) {
if (!baseMaps.get(entry.getKey()).equals(entry.getValue())) {
change = true;
baseMaps.put(entry.getKey(), entry.getValue());
}
}else{
change = true;
baseMaps.put(entry.getKey(), entry.getValue());
}
}
if (change) {
document.putProperties(baseMaps);
Log.e(LOG_TAG, "Updated Document details :: " + document.getId() + "---" + document.getCurrentRevisionId());
}
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
But crash again:
com.couchbase.lite.CouchbaseLiteException: Conflicts not allowed and there is already an existing doc with id: 129049
My log:
01-14 23:17:54.552 18559-18751/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129049---1-462b05fd989040ebbf87839f9ba4fd5b
.
01-14 23:17:59.079 18559-18751/com.eusoft.recite.fr E/ReciteSupportController﹕ Updated Document details :: 129044---1-ae6234208b28e5d64c36a6453df5ed65
01-14 23:17:59.887 18559-18751/com.eusoft.recite.fr E/ReciteSupportController﹕ Updated Document details :: 129026---1-dc770cb8426c73f493e82d5a3dc7d06a
01-14 23:18:00.643 18559-18751/com.eusoft.recite.fr E/ReciteSupportController﹕ Updated Document details :: 129040---1-24061733df271e9e4a2e01f94b610b3c
01-14 23:18:01.373 18559-18751/com.eusoft.recite.fr E/ReciteSupportController﹕ Updated Document details :: 129049---1-462b05fd989040ebbf87839f9ba4fd5b
.
.
01-14 23:18:25.547 18559-18751/com.eusoft.recite.fr E/ReciteSupportController﹕ Updated Document details :: 129051---2-0e4cb04c6005a21aa509f34634be58cd
01-14 23:18:26.408 18559-18751/com.eusoft.recite.fr E/ReciteSupportController﹕ Updated Document details :: 129049---2-6ac7b4cdb6f7fa0ea4c7079d25d8ce0f
Crash by execute last update line.
Yes, you will note that . update doc same rev.
I have just update my doc value,if it has change.But when i create it ,and then update, the rev is still not change ,and then update again ,it update to rev-2, but crash here.(some times rev-1 have not repeat.it has rev-1 and then rev-2,rev-3 but still crash(look at below log : 129051))
01-14 23:59:58.851 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129022---1-e6bb5057d1e713e8e4837004235f1695
01-14 23:59:58.908 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129023---1-a6ddc667e5b1126d368d9eb3fda82565
01-14 23:59:58.990 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129024---1-f50b52496a0e89200c62314329825369
01-14 23:59:59.045 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129025---1-8c54d6f9b7f9afdb9cb25991c1b34afe
01-14 23:59:59.102 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129026---1-dc770cb8426c73f493e82d5a3dc7d06a
01-14 23:59:59.158 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129027---1-aad2f1c80c9eabe0494ee418d815dfd8
01-14 23:59:59.208 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129028---1-1082a781d6837d3e666693fea3302fce
01-14 23:59:59.260 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129029---1-e889e108e61f181f74b4eec9732cc859
01-14 23:59:59.317 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129030---1-4fff35618b0b10f2e30432d0d3659bca
01-14 23:59:59.377 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129031---1-584d6b0a600978a524fdbebf46c4e801
01-14 23:59:59.428 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129032---1-27440287aebb9d3849f7bf7d437c473f
01-14 23:59:59.484 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129033---1-41862f186e3c0244509c1e5cec3b3412
01-14 23:59:59.528 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129034---1-da533fda450d4d5285a1a50db9848357
01-14 23:59:59.579 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129035---1-05b9f28381e098e038a3d49fc00b4f7b
01-14 23:59:59.628 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129036---1-87d042c91cf9030f861069b6a8b38fbe
01-14 23:59:59.676 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129037---1-7e708f65ded3e03f00520d85c5c733ed
01-14 23:59:59.729 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129038---1-a6f3212d623607d43b813069c537d023
01-14 23:59:59.775 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129039---1-54bb034960c84423b9f4a0c345e77ab5
01-14 23:59:59.829 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129040---1-24061733df271e9e4a2e01f94b610b3c
01-14 23:59:59.884 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129041---1-2ab804d2ea20a3e09eb1c5d6e4aebe13
01-14 23:59:59.938 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129042---1-216509ef51c311a8ab754467dddf2270
01-14 23:59:59.990 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129043---1-76da7bc567e2e3c28194f762e7509bb7
01-15 00:00:00.077 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129044---1-ae6234208b28e5d64c36a6453df5ed65
01-15 00:00:00.186 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129045---1-b7a7fb4968a17b112f7045acdb2cbfe8
01-15 00:00:00.296 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129046---1-a59cab6e7a8c79fc5b3598329619a5f6
01-15 00:00:00.361 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129047---1-7aaae3b23d315d052f9ea9dc229c7467
01-15 00:00:00.471 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129048---1-5083b24196041c41b730278e8b19e1e2
01-15 00:00:00.569 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129049---1-462b05fd989040ebbf87839f9ba4fd5b
01-15 00:00:00.638 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129050---1-764e7f6f26287aca2088cf7ca6e54279
#01-15 00:00:00.695 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ create Document here :: 129051---1-3848f79f774bc991643c0b6246dc6bea
01-15 00:00:04.115 26398-26688/com.eusoft.recite.fr E/TEST_LOG﹕ ==== *** start test recite *** ====
01-15 00:00:04.831 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129032---2-f95979fe934c8dbadac100567f078d82
01-15 00:00:05.549 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129037---2-f87d930d707c79263ef26846951e25c0
01-15 00:00:06.376 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129030---2-3b237e336e79deaa6d9fa24a9654e1a6
01-15 00:00:07.085 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129031---2-1a771c301bc1af949a8650459955c758
01-15 00:00:07.817 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129033---2-f34ce761a3f23c0b91d56b0f3bf07641
01-15 00:00:08.509 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129050---2-437e4e1902caea86070d9edd65b40055
01-15 00:00:09.227 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129022---2-24282140168c4c41aa522ae0efd91919
01-15 00:00:09.950 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129042---2-4a7c18d7a26dd4aae62637d07fae2971
01-15 00:00:10.659 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129049---2-158de723ff4ed5435a5a9a7afecdadd6
01-15 00:00:11.429 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129024---2-55a60fff64aefbd67d9c3cf941b64526
01-15 00:00:12.196 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129029---2-7dd1d83ef2404136c8f53dcbcbc30d54
01-15 00:00:12.909 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129043---2-4e7f59d00af542bd2d1db76037c17a90
01-15 00:00:13.607 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129045---2-0fdf01262407dcb90c00ae24d6a82359
01-15 00:00:14.330 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129025---2-2457fd5b61d888c8574cb38e1ba873ea
01-15 00:00:15.043 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129035---2-4b49a1761e559dc6d238b6ec6a4188ed
01-15 00:00:15.748 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129040---2-07a0421e5f4a413d1b971e87ad826bed
01-15 00:00:16.503 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129046---2-83c46be148329567cb3c717557d8fd2b
01-15 00:00:17.203 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129039---2-3d408fd69a365a1b1b16ccc622ac9c7b
01-15 00:00:18.060 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129023---2-be17ac4833d35e79e89d183fec40033d
01-15 00:00:18.789 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129036---2-96316054ea6240e4409d5e472299148f
01-15 00:00:19.525 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129028---2-864be1551204838003bd51ce26506ac6
01-15 00:00:20.249 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129047---2-69ed58ea1dfb7595f70e87cfa1c28801
01-15 00:00:20.994 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129034---2-409d401134609d6a2ce16e8fd026bac6
01-15 00:00:21.777 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129048---2-01d685221695577b19075f033b501a08
01-15 00:00:22.508 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129026---2-c765bc296f5c218cb8f321f8a78cf4f7
01-15 00:00:23.257 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129044---2-ce2243f6560424ab192b0bdb30baac83
01-15 00:00:24.000 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129041---2-72c77a14fb5a8858aaf4746bac9ccaf9
01-15 00:00:24.745 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129038---2-5ee9ffef9627784b7fc9fe586a17d7c5
#01-15 00:00:25.524 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129051---2-22ddbbb59eb5fe9d449d2172eacf9806
01-15 00:00:26.354 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129027---2-38f7ff7374d87beddd4d91102c1fca18
01-15 00:00:26.365 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ unit: 101_0 progress: 0.0
01-15 00:00:29.966 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129045---3-1d2cac2d1048eaf486084dd4fa5199ee
01-15 00:00:30.670 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129043---3-ff892c76c960316c1a71e13a17055c80
01-15 00:00:31.384 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129041---3-55aa55ff5ce4658233f43d96124b6486
01-15 00:00:32.079 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129040---3-ed4051132bc175d1cc057329e98488f1
01-15 00:00:32.831 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129032---3-2c819597ebfa1b32d085ddd26c6a0218
#01-15 00:00:33.589 26398-26688/com.eusoft.recite.fr E/ReciteSupportController﹕ doc change update :: 129051---3-51abc16d4f0841f5941e09175b009737
#crash 'com.couchbase.lite.CouchbaseLiteException: Conflicts not allowed and there is already an existing doc with id: 129051'
What's wrong ???
The properties you save to a document have to include a _rev
property containing the document's current revision ID. This is to avoid overwriting a newer revision you didn't know about. Please read the docs to see the correct way to update a document. Basically you get the existing properties, modify them (preserving _rev
) and save those as the new properties.
First thanks.but I think you have not got my word. My log show rev,it's doc private properties,I just print it on my logcat.
(Log.e(LOG_TAG, "Updated Document details :: " + document.getId() + "---" + document.getCurrentRevisionId());)
I have not add rev. It's automatically create by couch base.My doc just have "uuid,name,exp." properties! And as I know some times update success.but crash by first update often. I Guss maybe it's cache problems? I have test sample project,and update more than 100 times,it's succes.but why my project can't update?
My question is how to update a doc to fix 409 errors!
Thanks!
oh, i think, i have use doc instance in my memory cache,and then update it multiple times ,may be it's the key ?
If you reuse the same properties dictionary, yes, you'll get conflict errors. The dictionary will still have the old _rev
value, not the new one that was assigned to the latest revision, so the next time you re-use it you'll get the error.
oh.i see.i try to fix it .
// Create document if required
if (mDocument == null) {
mDocument = database.getDocument(getDocumentUniqueId());
UnsavedRevision revision = mDocument.createRevision();
revision.setUserProperties(mProperties);
revision.save();
Log.e("ReciteSupportController", "create doc here :: " + mDocument.getId() + "---" + mDocument.getCurrentRevisionId());
}else {
//updte document if required
Map<String, Object> baseMaps = new HashMap<String, Object>();
baseMaps.putAll(mDocument.getProperties());
boolean hasChange = false;
for (Map.Entry<String, Object> entry : mProperties.entrySet()) {
if (baseMaps.containsKey(entry.getKey())) {
if (!baseMaps.get(entry.getKey()).equals(entry.getValue())) {
baseMaps.put(entry.getKey(), entry.getValue());
hasChange = true;
}
}else{
baseMaps.put(entry.getKey(), entry.getValue());
hasChange = true;
}
}
if (hasChange) {
//update old doc if required
//(note:com.couchbase.lite.CouchbaseLiteException: Conflicts not allowed and there is already an existing doc with id: 129051)
mDocument.putProperties(baseMaps);
//and then reload doc to fix Conflicts
mDocument = database.getDocument(getDocumentUniqueId());
if (mDocument != null) {
mProperties = new HashMap<String, Object>();
mProperties.putAll(mDocument.getProperties());
Log.e("ReciteSupportController", "doc change update :: " + mDocument.getId() + "---" + mDocument.getCurrentRevisionId());
}
}
}
Am i right ???
Thanks.
I don't know. Honestly I don't have time to read and figure out that much code.
Yes.Thanks.I have updated the code.and run success.The problem to be fixed.
Thanks!