Attacchment lost when updating Document


#1

Hi everyone. I have this problem. I have a Singleton document (has a fixed ID and it’s shared between every client). This document can have an attacchment that can be edited from everyone… Easy so far… This is the use case that is causing the problem:

  1. Document is at revision 1-abcdefgh…
  2. User A set an Attacchment to the document
  3. User B gets the update and sees the attacchment
  4. User C edit something in the document and save it. This cause its revision to increase and so go to 2-abcdefgh…
  5. User A and B sync the document so that both have the document to revision 2-abcdefgh…
  6. The attachment is lost (The attacchment is bound to the revision, not the document).

So… Is there anything already built that can attach the attacchment always to the last revision? Or am I (developer) responsible to bind the attacchment to the last revision? I know there is a simple solution which is basically iterate from last revision to the first and get the first attacchment found. But, you know, I don’t like this approach… I don’t want to iterate through 10K revisions after 2 years to find that the attacchment was bound to the first one -.-

Thank you


#2

The attachment won’t get lost unless you’re specifically removing it, so you must be removing it by accident. Can you post the code that updates the document?


#3

Sorry, I didn’t want to say that it gets lost. It’s simply bound to the old revision. So, after document’s update if I call

document.getCurrentRevision().getAttachment(“myattachment”); //returns Null

it returns null. But if I do something like:

List<SavedRevision> revisions = document.getRevisionHistory();
revisions.get(revisions.size() - 2).getAttachment(“myattachment”); //returns not null

In this case I’m looking for the old revision and get its attachment. And in this case the attachment exists.

Sorry if I didn’t explain it clearly


#4

That shouldn’t be happening. Again, can you please post the code you’re using to update the document?


#5

Sure. Here’s the code:

Document document = database.getDocument(bean.get_id());
bean.set_rev(document.getCurrentRevisionId());
try {
document.putProperties(CouchbaseDatabaseManager.getMap(bean));
}
catch (CouchbaseLiteException e) {
e.printStackTrace();
}
}

The getMap function is a method that takes the Bean fields and transform them in a Map<String,Object> object


#6

Hi @edoardotognoni,

I wonder if return value of CouchbaseDatabaseManager.getMap(bean) has all necessary keys. For your case, “_id”, “_rev”, and “_attachments”.

Could you please print Map object from document.getProperties() and Map object from CouchbaseDatabaseManager.getMap(bean) before calling document.putProperties(...).


#7

Oh god… Now I see. I don’t “export” the _attachment field into the returning Map. Ok now it’s clear. I guessed the attacchment was not related to the document but to the Revision. I’ll test tomorrow but I’m pretty sure the problem is this.

Thank you all!


#8

Ok I confirm. I was missing the “_attachments” field on the bean object. For every Android guy who needs it, it’s a Map<String,Object> field.

Thank you!