Recommended way to query for attachments?

24 views
Skip to first unread message

Martin Rybak

unread,
May 23, 2018, 3:03:16 PM5/23/18
to Couchbase Mobile
What is the recommended way to query for attachments across multiple documents? For example, image thumbnails for display in a list view or grid view. It's not clear how best to do this in either 1.x or 2.0.

In 1.x, it looks like the file path can be constructed from the database, document, and attachment name, which means that attachments can effectively be indexed using map/reduce and queried.

In 2.0, the new Blob api exposes content and contentStream properties. The latter sounds promising, but how to access Blobs from a query? Also, is there a way to access the file path directly? I understand that accessing attachments directly as files is discouraged. However you also point out that there are APIs in iOS/OS X that only take paths as input, not NSData or NSStreams.

Curious to get some official guidance on this, thanks.

Martin Rybak

unread,
May 31, 2018, 9:17:39 AM5/31/18
to Couchbase Mobile
Anyone from Couchbase have some advice on this? Thanks.

Priya Rajagopal

unread,
May 31, 2018, 12:00:41 PM5/31/18
to Couchbase Mobile
In CBL 2.0, the Blob type will be associated with a property name. So you would query for that just like you would query for any named property. Check out this tutorial which discusses an example of using Blob type (https://developer.couchbase.com/documentation/mobile/2.0/userprofile_basic.html) . That should give you some indication on how to apply it to your case.

Martin Rybak

unread,
May 31, 2018, 3:52:09 PM5/31/18
to Couchbase Mobile
Thanks Priya, but the example you provided only shows how to access the content property of a Blob. If I get a bunch of image thumbnail Blobs from a query, and want to display them efficiently in a large list view, it seems that I would need to access the underlying filepath and use something like SDWebImage. How do you suggest I handle this use case?

Brendan Duddridge

unread,
Jun 1, 2018, 3:58:29 PM6/1/18
to Couchbase Mobile
I would recommend storing thumbnails separately in the Caches folder and keep the originals in the database as blobs. Don't store the thumbnails in the database because they can always be regenerated from the originals. Just my suggestion.

Martin Rybak

unread,
Jun 11, 2018, 12:04:26 PM6/11/18
to Couchbase Mobile
In my particular case, the thumbnail is the original. My question still remains -- how can I access the underlying filepath for attachments in a query in CBL 2.0 to support this use case?

Jens Alfke

unread,
Jun 11, 2018, 1:05:23 PM6/11/18
to mobile-c...@googlegroups.com


On Jun 11, 2018, at 9:04 AM, Martin Rybak <martin...@gmail.com> wrote:

In my particular case, the thumbnail is the original. My question still remains -- how can I access the underlying filepath for attachments in a query in CBL 2.0 to support this use case?

You can’t; the actual filesystem storage of attachments is an implementation detail.

If I get a bunch of image thumbnail Blobs from a query, and want to display them efficiently in a large list view,

You would do that by returning the documents from the query, getting the Blob object from each document, and accessing its data, probably as an input stream. Then you can use a platform API to create an image object from that.

—Jens

Jens Alfke — Mobile Architect — Couchbase, Inc.

Reply all
Reply to author
Forward
0 new messages