How can I avoid using NSFileWrapper in memory when writing a file

I have an application that uses NSFileWrapper

user data to back up. This backup file contains text and multimedia files (compression is not relevant here). Sometimes these backup files get quite large, over 200MB in size. When I call NSFileWrapper -writeToURL...

, it loads all content into memory as part of the write process. On older devices, this causes my app to be interrupted by the system due to memory constraints.

Is there an easy way to avoid NSFileWrapper

loading everything into memory? I read every NSFileWrapper

question I could find. Any suggestions on how to handle this?

Here is the current file structure of the backup file:

BackupContents.backupxyz user.txt - folder1 - audio files asdf.caf asdf2.caf - folder2 - audio files asdf3.caf

Again, please don't tell me to compress my audio files. It will only be striped for a faulty design.

It seems I could just move / copy all files to a directory with NSFileManager

and then make that directory a package. Should I follow this path?

+3


source to share


1 answer


When the tree is being NSFileWrapper

written to disk, it will try to hard link the source file to the new location, but only if you specify an option for originalContentsURL

.

It looks like you are creating a file wrapper at random (for a backup script), so your files are probably scattered all over the filesystem. This means that when you are writeToURL

, you are not originalContentsURL

. This means the hard link logic will be skipped and the file will be loaded so that it can be overwritten.



So, if you want hard-link behavior, you need to find a way to provide originalContentsURL

. This is most easily accomplished by providing the appropriate URL to the initial call writeToURL

.

Alternatively, you could try subclassing NSFileWrapper

for regular files and giving them NSURL

which they internally hang with. You need to override writeToURL

to pass this new url before super

, but that url should be enough to trigger the hard link code. Then you will want to use this subclass NSFileWrapper

for large files that you want to hard-link install.

+3


source







All Articles