I have a document properties downloaded from a view, so I have the actual JSON from the doc, including the ID and Rev ID. I don't have the actual CouchDocument, though.
The document has an attachment, and I know the name of it. I'm trying to download the attachment into a CouchAttachment object, but I can't find a way of doing it without having to redownload the document, which is slow. Here's what I'm doing:
-(CouchAttachment *)getAttachmentFor:(NSObject *)doc named:(NSString *)fileName {
if ([[doc valueForKey:@"_attachments"] valueForKey:fileName]==nil)
return nil;
CouchDocument * document = [[[App shared] database] documentWithID:[doc valueForKey:@"_id"]];
CouchRevision * revision = [document revisionWithID:[doc valueForKey:@"_rev"]];
return [revision attachmentNamed:fileName];
}
Is there any way to get the Attachment directly, wihout having to first get the doc and revision?
The CouchCocoa framework does not seem to provide a way to create a
CouchAttachmentobject directly. However, you can get the attachment directly with a GET operation, provided you know the URL of the attachment.Let's say you have some document in some database with an attachment named someAttachment.txt. The URL format for that attachment would be:
You have your revision ID and document ID from your
docdictionary. If you can pass your server URL and/or your database URL, you can do a GET operation to get the attachment like so.Source: http://wiki.apache.org/couchdb/HTTP_Document_API#Attachments
Alternatively you can create a
CouchAttachmentobject usingRESTResource'sinitWithURL:method but that does not constructCouchAttachmentspecific properties like the document and database etc.