CBL 2.0 for Swift: get attachments as blob

In CBL android version, there is a method getBlob() on Database that fetches attachments like getBlob("_attachements.something"). On Swift side however I don’t find this method on Database. There is a blob(forKey:) method on Document class, but when I tried it on “_attachments” or “_attachments.something”, both returned nil. value(forKey:) on the same document returns both as DictionaryObject.

The attachments were created on the server side via Sync Gateway api

So my question is, how to get attachments sync’ed down from server side other than getting it from sync gateway as it suggested in this doc: https://developer.couchbase.com/documentation/mobile/2.0/couchbase-lite/swift.html


Version: DB21
Client OS: iOS 11
Server: SG 1.5

In CBL android version, there is a method getBlob() on Database

You mean Document, not Database, right? (Actually it’s on Dictionary, inherited by Document.)

blob(forKey:) is the equivalent method in Swift.

when I tried it on “_attachments” or “_attachments.something”, both returned nil.

The _attachments property of a document isn’t a blob; it’s a dictionary containing blobs.

_attachments.something is not a path to the something property of the _attachments dictionary; it refers to a property named literally _attachments.something. You need to get one property and then the other.

I tried both blob(forKey: "_attachments.something" and dictionary(forKey: "_attachments")!.blob(forKey: "something"). Both returned nil .

dictionary(forKey: "_attachments")!.dictionary(forKey: "something") does return a DictionaryObject but I just can’t get the blob.

Can you try this and see what you have inside the _attachments dictionary?

NSlog("\(dictionary(forKey: “_attachments”)!).toDictionary())")

["something1": {
    "content_type" = "text/somescript";
    digest = "sha1-HwiFX2djWv/NSIYHH8Ez9nY0DnM=";
    length = 359492;
    revpos = 7;
    stub = 1;
}, "something2": {
    "content_type" = "text/somescript";
    digest = "sha1-e9ynzq+SSlHK/4K4m+8JK64xrfE=";
    length = 359503;
    revpos = 5;
    stub = 1;
}, "something3": {
    "content_type" = "text/somescript";
    digest = "sha1-R+MkOM6BR0pm0OR+mXlp75zPJ8U=";
    length = 8597163;
    revpos = 3;
    stub = 1;
}, "something4": {
    "content_type" = "text/somescript";
    digest = "sha1-L5c6Bq7mIAagE1gzLE4oebSb/Z8=";
    length = 7554258;
    revpos = 4;
    stub = 1;
}, "something5": {
    "content_type" = "text/somescript";
    digest = "sha1-m8l4k47Zo6S/dtDkf+iv/2uh0yU=";
    length = 359514;
    revpos = 2;
    stub = 1;
}, "something6": {
    "content_type" = "text/somescript";
    digest = "sha1-gcKuME5x+P7hSGMzXqyEnUOfzgA=";
    length = 8640780;
    revpos = 6;
    stub = 1;

I expected the content of “somethingN” to be Blob instead of dictionary (not sure if it’s log printing thing or not). I just tested this yesterday and I didn’t see the issue.

Does this work on CBL Android 2.0? Are you using the master branch build of CBL iOS? If it works on Android but not iOS, would it be possible to pack a zip of database folder and post it somewhere for me to download and test?

From the sample code above, I noticed that the blob key is “something” instead of “something1”. Can you make sure that the key is correct too.

something is just what I masked when posting here, and I did use the correct key name when doing the query.

I’m working on DB21 as this environment’s SG is on 1.5.

DB21 may not have had the logic to recognize children of _attachments as blobs. That fix went in on 1/31, commit 6cc6e94.

I just did a test with DB22 and it works fine.
The _attachments show up as

 "_attachments" =     {
        imagefile = "CBLBlob[application/json; 681 KB]"

whereimagefile is the name of the attachment

Verified that attachments work fine in DB23. Thanks!

1 Like