4

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.

Community
  • 1
  • 1
Odrakir
  • 4,254
  • 1
  • 20
  • 52
  • I would really love to know why this was downvoted and still I don't have an answer. What am I missing? – Odrakir Feb 25 '13 at 10:15
  • What do you mean, *"without opening the document"*? At what point in your code is the document opened to retrieve the preview? – Greg May 03 '13 at 11:26
  • In the code posted it's not opened. But what I'm doing doesn't seem completely right (accessing the folder inside the doc's path). I'm not sure if that's the way that is supposed to be done. – Odrakir May 06 '13 at 08:50
  • Accessing files inside the document's path is absolutely fine. The document itself isn't loaded into memory at this point, so there's nothing wrong with doing that, you just need to be sure that you appropriately handle cases where that file doesn't exist inside the document, for whatever reason. – Greg May 07 '13 at 09:43
  • Ok, thanks. It just seems weird to me that UIManagedDocument has a mechanism to save additional data but not to retrieve it. But if you say it's ok, I'll just have to believe you :) – Odrakir May 08 '13 at 07:55
  • 1
    If you're opening the document, override `readAdditionalContentFromURL:error:` in your subclass to read data form the additional content container. http://developer.apple.com/library/ios/#documentation/uikit/reference/UIManagedDocument_Class/Reference/Reference.html – Greg May 09 '13 at 01:46
  • 1
    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 May 09 '13 at 01:47
  • 1
    Check out this answer for more details on how to implement additional content for the document: http://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 May 09 '13 at 01:49

1 Answers1

0
  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.

Community
  • 1
  • 1
Greg
  • 9,068
  • 6
  • 49
  • 91