How to update an NSFileWrapper?
Asked Answered
T

0

2

I have an NSFileWrapper with some content. I want to save it (writeToURL...:), add new content to it and save it again.

I would like to avoid writing again and again the same files. So, if I understood clearly, it means the option NSFileWrapperWritingAtomic is not for me. Especially as I save the file wrapper in the method

- (BOOL)writeAdditionalContent:(id)content
                         toURL:(NSURL *)absoluteURL
           originalContentsURL:(NSURL *)absoluteOriginalContentsURL
                         error:(NSError *__autoreleasing *)error

(of an OSX analogue of UIManagedDocument)

and as Apple says "don't use this option in an override of -[NSDocument writeToURL:ofType:error:]".

So, I don't use this option, but updating the files from the file wrapper (I mean : saving again) fails (see below). Any idea/clue ?


Here is what I do :

1

I save it a first time with

[self.fileWrapper writeToURL:[absoluteURL URLByAppendingPathComponent:@"attached_files/"]
                     options:0
         originalContentsURL:[absoluteOriginalContentsURL URLByAppendingPathComponent:@"attached_files/"]
                       error:error] ;

It works well.

My file wrapper is

    /*
     Initializing the file wrapper
     */
    _fileWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil] ;
    [_fileWrapper setFilename:@"attached_files"] ;
    [_fileWrapper setPreferredFilename:@"attached_files"] ;

2

I save a second time with the same command. I get the error

 Error Domain=NSCocoaErrorDomain Code=516 "The file already exists". 

The given name is actually "attached_files".

3

If I use the option NSFileWrapperWritingWithNameUpdating, I still get the same error.

4

If I use the option NSFileWrapperWritingAtomic, I don't get errors.

Tantalous answered 10/3, 2014 at 12:5 Comment(3)
why not to directly update the file?Requital
I can't write directly in the document's file. Otherwise, the application will complain that the file has been modified by another application.Tantalous
The compiler that ships with Xcode 7 throws a warning with this code: Null passed to a callee that requires a non-null argument for -[NSFileWrapper initDirectoryWithFileWrappers:].Relapse

© 2022 - 2024 — McMap. All rights reserved.