> Le 16 mai 2018 à 09:26, Rick Mann <rm...@latencyzero.com> a écrit :
> 
> I'm working on a little NSDocument-based app. The documents are packages (a 
> directory containing multiple files). One of the operations is to import a 
> music file into the document, which should copy the music file into the 
> package, and set it as the track for the document.
> 
> Undoing this operation is somewhat complex (where do I store the previous 
> track if any?). So for now, I want to ignore undo.
> 
> But I'm not sure how to copy the track file into the package. For example, 
> what if it's an untitled document, and therefore has no package on disk? The 
> action should result in a dirty document that isn't committed to disk until 
> the next save (be it automatic or manual).
> 
> Can this even be done without undo?
> 
> Along those lines, how do I mark a document as dirty after some change, 
> without making the change undoable?

You have a choice, for « unsaved » documents:
- either you keep them only in memory, or
- you record them in a « untitled » file package, perhaps in /tmp.

An element of choice is how you want to process the import:
- either you copy the data immediately,
- or you copy the data only when the document is saved.

In the later case, you and the user will have a problem if the imported file is 
deleted before the unsaved document is saved.

If you copy the data immediately, when the unsaved document is only kept in 
memory, then it may be difficult to load big files in memory, notably if bigger 
than the actual RAM available: the system will start to swap the memory to the 
swap file, and this will be very slow, and may even crash if there’s not enough 
free space on the disk where the swap files are stored.  But one good news is 
that nowadays everybody has 64-bit systems, so it should be possible to load in 
memory even files bigger than 4GB.

I guess you see where I’m drifting to: it’s best to keep your document saved on 
disk all the time, even before it’s saved explicitely by the user.  The bonus 
feature, is that if your application crashes, or the system is powered off, you 
can restore the unsaved document (if you don’t put it in /tmp, but eg. in 
~/Documents/Unsaved\ Documents/).  Then you can copy the imported data to a 
file as soon as the import function is activated, so even if the imported file 
is deleted, the imported data will be there.  And then you don’t have to 
implement in-memory data structure for unsaved documents, since by definition 
all documents are backed by files the same way (only a different path), whether 
they’re « saved » nor not « saved ».  Much simplier.  When you launch the 
application, scan the unsaved documents folder for unsaved document,  and 
re-open them, to let the user choose whether to close (and delete them), or to 
save them.

-- 
__Pascal J. Bourguignon__




_______________________________________________

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