4
votes

I have a document based app (multiple documents each one of them with their own core data persistent store). I'm using UIManagedDocument to develop it.

Each document is a drawing and I want to be able to save a preview (UIImage) of each drawing.

As I want to show a scrollView with all the previews I guess I shouldn't be puting the preview inside the data base, so I'm using UIManagedDocument's additional content feature as described here.

I have a couple of questions:

  • What use has the parameter "absoluteURL" in additionalContentForURL:error: ? It's not getting used in the example I linked.

  • How do I retrieve the preview without opening the document? Currently I'm doing this:

.

NSString* docName = [[[DocumentStore sharedStore] documentsList] 
                                    objectAtIndex:indexPath.row];

NSString* dataDirectory = [FileUtils privateDataDirectory];
NSString *dataPath = [dataDirectory stringByAppendingPathComponent:docName];
NSString *imagePath = [dataPath 
             stringByAppendingPathComponent:@"AdditionalContent/thumb.png"];

UIImage * preview = [UIImage imageWithContentsOfFile:imagePath];

... but I'm not sure if this is the best way to do it.

1
I would really love to know why this was downvoted and still I don't have an answer. What am I missing? - Odrakir
What do you mean, "without opening the document"? At what point in your code is the document opened to retrieve the preview? - Greg
If you're opening the document, override readAdditionalContentFromURL:error: in your subclass to read data form the additional content container. developer.apple.com/library/ios/#documentation/uikit/reference/… - Greg
If you want to get data from the bundle without actually opening the document, you can get it directly like you are doing now, e.g. when you want to show a preview in a list. But when you actually open the document, you will want to override readAdditionalContentFromURL:error: to actually read the data when the document object is instantiated. - Greg
Check out this answer for more details on how to implement additional content for the document: stackoverflow.com/a/8901963/834998. Remember that this is for when the document is actually opened and loaded. If you want to just display previews for all user documents, you don't want to load and open each one just to display a preview, so what you are doing will work in that case. - Greg

1 Answers

0
votes
  1. The absoluteURL parameter in additionalContentForURL:error: gives you the absolute URL to which the additional content will be written.

    For most use cases, this is not a particularly necessary parameter, as you do not really need to know where this data will be written to, but may be useful as an identifier in some obscure use cases with a global content management system independent of the document (although that probably wouldn't be very structure)

  2. The method you are currently using to retrieve previews of documents should be fine, as long as you implement safeguards so your app won't crash or exhibit undefined behaviour if the preview resource does not exist at the path where you expect it to be, for whatever reason.

    The other method to get the previews would be to load each and every document, initialise it, implement readAdditionalContentFromURL:error: in the document class to read the preview and put it in a property, then retrieve the value of that property and use that as the preview. However, this would require loading every single document into memory just to retrieve the preview, so I would not recommend doing this as it would have severe performance implications.

For a general guide on how to handle additional content in UIManagedDocument, see this answer to this question.