Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

More efficient CouchModel attachment handling #19

Open
lenkawell opened this issue Apr 6, 2012 · 5 comments
Open

More efficient CouchModel attachment handling #19

lenkawell opened this issue Apr 6, 2012 · 5 comments

Comments

@lenkawell
Copy link

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.)

@pulkitsinghal
Copy link

@snej & @lenkawell +1 for this :)

@lenkawell
Copy link
Author

Any chance this might be improved any time soon? If not, can you suggest a work-around I could use in the meantime?

@snej
Copy link
Contributor

snej commented Jun 13, 2012

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.

@lenkawell
Copy link
Author

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?

@snej
Copy link
Contributor

snej commented Jul 7, 2012

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants