-
Notifications
You must be signed in to change notification settings - Fork 66
More efficient CouchModel attachment handling #19
Comments
@snej & @lenkawell +1 for this :) |
Any chance this might be improved any time soon? If not, can you suggest a work-around I could use in the meantime? |
Probably not a 1.0 feature, unfortunately. You can PUT directly to an attachment's URL (call PUT: on a CouchAttachment) without having to do any base64 expansion; that might help. |
For the suggested work-around, if I directly alloc, init and PUT a CouchAttachment, what is the correct way to add it to the CouchModel? I didn't see a CouchModel API to add a CouchAttachment. Should I directly modify the _attachments property and add the CouchAttachment resource ID? |
If you add the attachment to the model's document, it will show up in the model, because the model tracks changes to its document. So just get the attachment object from the document and PUT the data to it. |
I'm actually running into some serious memory problems trying to use CouchModel and multiple large attachments (e..g, photos). The problem is that each time I add an attachment to an open CouchModel subclass, an NSData with the base-64-encoded attachment is linked to the CouchModel subclass instance. If I add a bunch of attachments, it retains them all. The result is that, if a user of my app tries to add a group of photos to a CouchModel subclass, iOS kills the app for using too much memory.
I'd like to suggest adding some kind of a "deferred" interface to CouchModel attachments that wouldn't load the attachments until they were actually being sent to TouchDB. Even better might be some kind of by-reference (URL?) mechanism so the file was actually never copied into memory, but simply file-system copied to a .blob file?
From Jens in an email:
Yes, that would be a lot better. The most flexible interface would be to allow the value of an attachment to be an NSInputStream, which would then be read and copied to the HTTP body during the POST. Then for convenience, a wrapper that allows the client to specify a file path/URL. I already have code in TouchDB's replicator that supports uploading attachments from a stream (TDMultipartWriter).
(And of course with TouchDB there's really no need even to stream; the file path could somehow be passed directly from CouchCocoa to TouchDB which would then copy or move it into its attachment store directly. That's the kind of integration I'm hoping to get to in the Syncpoint project.)
The text was updated successfully, but these errors were encountered: