Couchbase Lite 2.0 - Multiple blobs


Is it possible to include multiple blobs? We want to save a document with several images.

We’re using Xamarin and CBL 2.0.

Best regards,

Yes, you can include multiple blobs in the document. For example,

document.set(blob1, “image1”)
document.set(blob2, “image2”)
document.set(blob3, “image3”)

1 Like

Correction on the typo in Pasin’s response … Its SetBlob
It would be

 var data1 = new MutableDocument()
                .SetBlob("blob1",new Blob("image/jpeg",byteArr1))
               .SetBlob("blob2", new Blob("image/png", byteArr2)

Oh, I didn’t realize you could add more than one… Thank you, both, for clarifying this!

how would you than add undefined number of blobs (in some case 1, in other 3 or 10) for same type of document? Also how would you select them back from database?

Not sure I understand your question. You will set the Blob the same way you set any other property in a document

  • get the Mutable copy of the document
  • Set the Blob and chain together the setBlob as many times as you need. So if it’s 1, do it once and if it’s 3 do it thrice.

You will use getBlob to select the blob by specified key

For adding I’m doing this now
for (index, element) in attachments.enumerated(){
if let imageData = UIImageJPEGRepresentation(element, 1) {
let blob = Blob(contentType: “image/jpeg”, data: imageData)
doc.setBlob(blob, forKey: “att(index)”)
When I’m doing select, I cannot select blob by key, since I don’t know how many are they.

And just to clarify I’m working in iOS…

The simplest option for you is to include a separate property that tracks number of blobs in the document and use that to construct the key name associated with the blob that you want to fetch.

What is the problem you’re trying to solve? There are query functions for iterating over collections, for example.

I have an array of objects with one of the property array of UIImage that I’m trying to save and then later on select. Saving I solved as I said before.
When I’m doing select, I’m already iterating over rows and I get all other properties. Issue that I’m having is only with multiple blobs.
Can you point me to an example of how to iterate over multiple blobs?

You have not explained why my proposed workaround doesn’t work for you ? If you track the number of blobs in each document as a property within your document, why can’t you use that to derive the key (which in your case was"att(index)" to fetch the blob

Why not just use the query API to get the IDs of the documents you need, then get the Document objects and retrieve the blobs from there?