0

I have a UIDocument-based app which shows a list of documents stored in iCloud. I'm getting their filenames (for displaying) from the NSMetadataItem with metadataItem.value(forAttribute: NSMetadataItemDisplayNameKey). The documents contain a string and I've overridden the localizedName property to produce a better name containing this string. Ideally, I'd like to show the localisedName property of the UIDocument before it's been downloaded. Is there a way to do this?

I'm storing my UIDocument as a FileWrapper containing FileWrappers for each part of the document. In my case, this is just a UIImage and a String.

I'm managing to get the thumbnail without downloading the document so I was wondering if there's an equivalent thing for this string.

I'm using Swift 4 but I don't think that makes any difference here. Many thanks.

Josh Paradroid
  • 1,172
  • 18
  • 45

1 Answers1

0

Josh,

fileURL.deletingPathExtension().lastPathComponent

will give you the equivalent of the un-overridden localizedName.

I can infer from your post that you know your metadataquery.results will give you a list of URLs. You can use this on any URL.

FYI, the un-overridden localizedName will not even work after your UIDocument is instantiated, you have to open it first, which seems silly.

If you need to make a name more fancy than that and not based upon the fileURL then you will need to open the file first, as far as I can tell.

I know you understand this but for others, overriding localizedName just lets you modify it to a string you want it to be, probably based upon the location of the user, smilely face.

Using fileWrappers will allow you to open and get any stored variable and not have to open the "Big Object". Thumbnails used to be loaded this way but have been upgraded to being native to cloud since they added a file browser.

Are you time pressured to get these open to list in a TableView, CollectionView? The reason I ask is because UIDocument is not ARC compliant while a file is open, the Deamon that connects it to the cloud will keep a hard reference, slowing down the Deamon, i assume, and not allowing the UIDocument to deinit if the TableViewCell is reused before the file is closed, i.e. the pointer to the document is lost.

Sojourner9
  • 158
  • 11
  • Thanks, but I was wondering if I could get my custom, overridden document name without having to open the document, like the way I'm getting thumbnail. It sounds like that is not possible: When I get the notification that a new file exists, I will have to show a temporary name (possibly the filename) until enough data is downloaded for me to generate the custom title. – Josh Paradroid Nov 06 '17 at 17:26
  • You did not say how you wanted to customize the names, maybe if you gave an example. – Sojourner9 Nov 07 '17 at 22:13
  • I mean that the name would be customised based on something which might require the entire document. So on the device that has or created (and has) that content, it would save out the name as some kind of metadata, so the new device could download just that name string before downloading the entire document. – Josh Paradroid Nov 08 '17 at 10:57
  • You can use NSFileVersion.currentVersionOfItem(at: fileURL) to see if a file is cached locally, if it is nil then it is not cached, TMBABW. Any time you load your app on a new device or for any files not already on your device, i.e. created by another device, they are not cached. They will likely have private in the URL. Opening and closing a file to cache it, TMBABW. Stall at the beginning of your app to not let the app run until all the files are cached. If this does not cause problems for you, then use read(fileURL) to read the cached contents which is really quick. – Sojourner9 Nov 16 '17 at 22:05