Question

I have a NSFileWrapper directory in which I would like to update a certain FileWrapper. I was wondering what the best way is to do so?

So far I've used this code:

[self.fileWrapper addRegularFileWithContents:photoData 
                           preferredFilename:@"photo.data"];

However, whenever the FileWraper already exists, I get duplicates in my FileWrapper which look so:

"1__#$!@%!#__photo.data" = "<NSFileWrapper: 0x6bb0260>";
"2__#$!@%!#__photo.data" = "<NSFileWrapper: 0x6b89b80>";
"3__#$!@%!#__photo.data" = "<NSFileWrapper: 0x6ba1700>";
"4__#$!@%!#__photo.data" = "<NSFileWrapper: 0x6bc8480>";
"photo.data" = "<NSFileWrapper: 0x6bcfc50>";

How can I prevent this and simply replace the FileWrapper - in this case photo.data? I did not find any method to replace FileWrappers in the NSFileWrapper Class Reference.

Was it helpful?

Solution

I think this may be the solution:

NSFileWrapper *oldFileWrapper = [self.fileWrapper.fileWrappers objectForKey:fileName];
if (oldFileWrapper) [self.fileWrapper removeFileWrapper:oldFileWrapper];

[self.fileWrapper addRegularFileWithContents:[self encodeObject:object] 
                           preferredFilename:fileName];

OTHER TIPS

I just stumbled upon this issue and for me it was because I had a folder named "Resources" and one named "resources". This created an iCloud issue because on Mac OS X the file system isn't case sensitive, but it is on iOS.

It looks like the underlying problem in your case might also be upper/lowercase related. Of course you're fixing this by replacing the file wrapper, which effectively deletes and recreates the file. This might be an acceptable solution for small files, but can be very inefficient for larger or many files in a directory (because the file would be synced even if it didn't change).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top