Thank you Jens. I have found the proper way to manage that.
When the user imports a new image, I quickly copy the image to a temporary
folder, then I add this latest file reference to the document¹s
NSFileWrapper.
I just create the file¹s fileWrapper using its URL, so I do not read its
content and create the fileWrapper by the NSData.
When the users saves the document, automatically the filesWrappers get
written to the disk, within the document filePackage.
And when I close the document, I remove the temp folder.
That¹s fine.

I had to scratch my head at managing the undo, but I found a solution here
too.
When the user undo the import of an image, if the images has been never
saved, so it is still within the temporary folder, I just remove the image
file reference from the fileWrapper. Instead, if the image has already been
saved, so it is within the file package, I copy its file to the temporary
folder and remove its reference from the fileWrapper. So when the user saves
the doc, the image file get removed from the doc filePackage. And if the
user invokes the undo, I add the temp image file to the fileWrapper.
It sounds complicated but it works well.

Why do I use a temporary folder? Because if the user imports an image file
then he tries to delete the original image file, the OS tells him that he
can¹t delete that file in use. That could be annoying.
Why don¹t I add the file¹s NSData to the fileWrapper but I add just its URL
reference? Because I don¹t really know whether the fileWrapper uses the disk
or keeps the NSData in memory. So in case of a 1GB file I do not engulf the
app.

I hope I have designed a good architecture.


Regards
-- Leonardo



Da: Jens Alfke <j...@mooseyard.com>
Data: Tue, 4 Feb 2014 11:47:35 -0800
A: Leonardo <mac.iphone....@gmail.com>
Cc: <cocoa-dev@lists.apple.com>
Oggetto: Re: Managing image loading


On Feb 4, 2014, at 9:03 AM, Leonardo <mac.iphone....@gmail.com> wrote:

> - When the user imports a new image, I add its NSData to the NSFileWrapper
>    [docFileWrapper addRegularFileWithContents:imgData
>        preferredFilename:@"image.png"];
> and retain the NSImage. So I can draw the NSImage at any time.

You don't need an NSFileWrapper for that. I would just use NSFileManager to
copy the file, and NSData methods to read/write the file. I have no idea
whether NSFileWrapper caches the file contents in memory or not.

‹Jens


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to