Should I use NSFileWrappers in a UIManagedDocument?

I am trying to save a plist and several binaries (like images) as part of a UIManagedDocument. The name of the binaries is an attribute in Core Data, and I do not need to list them, just refer to what you need when showing the related entity.

The file structure I want to have is:

- <File yyyyMMdd-HHmmss>.extdoc - StoreContent - persistentStore - AdditionalContent - ListStatus.plist (used to store per document defaults) - Images - uuid1.png - uuid2.png - ... - uuidn.png 

So far, I have successfully followed the instructions in How to save additional content to my UIManagedDocument files? but when I try to add binaries there are some things that I don’t know how to do.

  • Should I handle the URL / the / path / File yyyyMMdd-HHmmss.extdoc / AdditionalContent (readAdditionalContentFromURL is used by default: error :) as NSFileWrapper? Are there any advantages / disadvantages, and not just the use of URLs? It’s harder for me to use the file wrapper, since the plist needs to be read using file covers and NSCoder (I think), and the files, I have to store the file wrapper for the Images directory, and then get the corresponding node with objectForKey (suppose). But Apple’s iOS application programming guide for custom formats instead of NSData or NSFileWrapper says, “Keep in mind that your code will have to duplicate what UIDocument does for you, and therefore you need to deal with more complexity and greater chance of error. " I do not understand this?
  • The default entries for documents are declared as properties: the setter modifies the NSDictionary, which displays a plist and marks the document as updated, and getter accesses the dictionary using the appropriate key. How to show the ability to read / write binary files? Should I add a method to my subclass of UIManagedDocument? - (void) writeImage: (NSString *) uuid; and - (UIImage *) readImage: (NSString *) uuid; And should you store this data in memory until the document is saved? How?
  • Assuming NSFileWrapper is the way to go, if I plan to use this document with iCloud , should file coordinators with a file shell be used? If so, how?

Any source code for each question is welcome. Thanks.

PS: I know that I can save some binary data inside Core Data, but I do not feel comfortable with this solution. Among other reasons, I prefer to store PNG data for image files, which are a serialized version of UIImage that will not be compatible with NSImage if I want to create a desktop application.

+4
source share
3 answers

I would like to say that in general I like UIManagedDocument more. It has several advantages over raw master data. For example, it automatically installs the entire kernel data stack. It also creates nested contexts for managed objects for you, so you get free background saving. None of this is particularly damaging to the Earth, but it does have a lot of functionality from a tiny amount of code.

I did not play with the retention of additional information ... but here are my thoughts.

Firstly, you do not need to handle the new URL as a file wrapper. You should simply perform regular file operations at the provided URL. Just make sure everything is implemented correctly in the AdditionalContentForURL: error :, writeAdditionalContent: toURL: originalContentsURL: error: and readAdditionalContentFromURL: error: file. Read and write operations must be symmetrical. And you should probably take a snapshot of the data in the AdditionalContentsForURL: error: file so that everything is saved in a known good condition (since the save operations are asynchronous).

As an alternative, have you considered using the Store in External Record File flag in your data model instead of saving it manually? This should force Core Data (depending on the size of the binary data) to automatically store it from the outside. I looked at the release notes and I didn’t see anything, saying that you cannot use this feature with iCloud. This may be the easiest solution.

+1
source

Attacking a side point at the moment (since I did not have ANY good experience with UIManagedDocument ).

You can save the binary code inside Core Data for the iOS 5.0+ application using an external file link. Then you can directly save PNG images to Core Data and not worry about UIManagedDocument or bloating sqlite file.

There is nothing to prevent you from saving PNG instead of UIImage.

0
source

One more thought. You may need to use NSFileCoordinator for read and write operations. Technically, any read or write operations in an iCloud container should use a file coordinator (to coordinate with the iCloud synchronization service - this prevents accidental damage to the file by reading it while another process is writing to it).

I know that UIDocument automatically processes most of its input and output methods. I assume these methods are wrapped in a similar way (as they give you the url to use). However, the documents are not very clear.

0
source

Source: https://habr.com/ru/post/1398931/


All Articles