2

I am creating a NSDocument package that contains potentially hundreds of large files, so I don't want to read it all in when opening the document.

I've spent some time searching, but I can't find a definitve answer. Most people seem to think that NSFileWrapper loads all of the data into memory, but some indicate that it doesn't load data until you invoke -regularFileContents on a wrapper. (See Does NSFileWrapper load everything into memory? and Objective-C / Cocoa: Uploading Images, Working Memory, And Storage for examples.)

The documentation isn't entirely clear, but options like NSFileWrapperReadingImmediate and NSFileWrapperReadingWithoutMapping seem to suggest that it doesn't always read everything in.

I gather that NSFileWrapper supports incremental saving, only writing out sub-wrappers that have been replaced. So it'd be nice if it supports incremental loading too.

Is there a definitive answer?

Community
  • 1
  • 1
Dejal
  • 813
  • 6
  • 18

1 Answers1

2

NSFileWrapper loads lazily by default, unless you specify the NSFileWrapperReadingImmediate option. It will avoid reading a file into memory until something actually requests it.

As a debugging aid only, you can see whether a file has been loaded yet, by examining:

[wrapper valueForKey:@"_contents"];

It gets filled in as NSData once the file is read from disk.

Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
  • 1
    Thank you, very helpful. Am I correct in thinking that if I specify `NSFileWrapperReadingWithoutMapping`, then `-regularFileContents` will read in the file without caching it? I want to be able to read in a bunch of plists without having them remain in memory. Even better would be to let it cache them, and have a way to unload/discard them when no longer needed, but I don't see any method for that. – Dejal Apr 09 '13 at 17:34
  • No, `NSFileWrapperReadingWithoutMapping` controls whether memory mapping should be used or not. Either way, an `NSData` object to cover the file will be held onto – Mike Abdullah Apr 09 '13 at 23:32
  • 1
    The only way to "unload" a file wrapper without deallocating it is to ask it to read from a URL again. Arguably this is a deliberate design so that under normal circumstances, `NSFileWrapper` behaves semi-immutably; once loaded, its properties don't revert backwards to an earlier/unknown state – Mike Abdullah Apr 09 '13 at 23:35
  • Okay, thanks. I'll try to minimize the loading of file contents, then (no doubt a good idea for performance, anyway). – Dejal Apr 10 '13 at 04:43