Resolving iOS URL bookmark data eventually starts failing

886 Views Asked by At

I have an iOS app where I'm attempting to persist and reload bookmark data using UserDefaults. Seems to work OK, but I'm having an issue where sometimes the conversion from the bookmark Data to a URL is failing. It seems to work fine for a period of time (even across the app terminating and relaunching, etc.), but eventually the bookmark data will fail (maybe after a few hours?).

So when resolving the bookmark data like so:

let url = try URL(resolvingBookmarkData: data, bookmarkDataIsStale: &isStale)

A caught exception is thrown with a description: Error Domain=NSCocoaErrorDomain Code=4 "The file doesn’t exist."

I'm storing the bookmark as Data in UserDefaults. I create the bookmark data simply with the following:

let bookmarkData = try url.bookmarkData(options: .minimalBookmark)

Interestingly, if I manually choose the same file again from the document picker, then the original bookmark stored in UserDefaults becomes accessible again.

I've reviewed open source code in GitHub and don't see anything substantially different about my implementations versus others. Looking for any tips or thoughts on what might be causing this issue.

1

There are 1 best solutions below

0
On

Turns out I was the issue was because I wasn't storing a bookmark to the original file, but a copy stored in a tmp directory. I had just recently refactored something and accidentally started using UIDocumentPickerMode.import instead of UIDocumentPickerMode.open. This explains why this was working for an arbitrary period of time, until the tmp directory got purged.