Re: Managing image loading
Re: Managing image loading
- Subject: Re: Managing image loading
- From: Kyle Sluder <email@hidden>
- Date: Sat, 08 Feb 2014 10:06:04 -0800
On Feb 8, 2014, at 3:58 AM, Leonardo <email@hidden> wrote:
>
> 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.
This will break.
>
> 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.
Except the file *is* in use—a reference to it lives on your undo stack. You seem not to care about data corruption.
But it turns out this doesn’t matter, because your NSData will have an open file descriptor on the image file, so deleting it via Finder won’t actually delete the file from disk until the NSData instance is reallocated or your app quits.
The only thing that would be problematic would be ejecting the volume the file lives on—the OS should warn about that.
So you need to handle the case that the file gets deleted while its data is still on the undo stack. You won't be able to swap the file back into place. See below for how to address that:
> 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.
1. Look up “memory mapping.” You can pass arguments to NSData that control whether it maps the image file.
2. The wonderful thing about virtual memory is that it gets paged out to disk.
3. 64-bit apps have oodles of address space (256TB under the current scheme); dedicating 1GB of that to a memory-mapped image file is not going to be a problem.
--Kyle Sluder
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden