Fetch attachment names in a scenario where the document is not yet synced


#1

We use CouchbaseLite 2.0.3 in the .NET project. How shall we get the list of attachment names from a document which is not synced so far. If the document has been synced, I could use the following code to get the attachments list. Please advise how to fetch the attachment names if the document was created and added attachments when the user was offline all the time.

var document = database.GetDocument(id).ToMutable();
var attachments = document.GetDictionary("_attachments");


#2

Couchbase Lite integrates attachments to be first class data citizens so they are no longer tracked separately like this. The answer to where they are is “wherever you put them”. They are online like everything else. If you need to know the keys they are stored under then that needs to be part of your data model.


#3

Thanks Jim for the quick response. I have written some code as given below
Will there be any performance issue? Please advise

var document = doc.ToMutable();
var blob = new Blob(contentType, fileStream);
var attachments = document.GetDictionary("_attachments");
if (attachments == null)
{
var obj = new MutableDictionaryObject();
obj.SetBlob(fileName, blob);
document.SetDictionary("_attachments", obj);
}
else
{
var mutableAttachments = attachments.ToMutable();
mutableAttachments.SetBlob(fileName, blob);
document.SetDictionary("_attachments", mutableAttachments);
}
database.Save(document);