help needed understanding the apps file system

How often/ under what circumstances are users sandbox directory URL's changed?

So I tried to save a URL to a document in @AppStorage

@AppStorage(DefaultsKey.userActiveBook.rawValue) var activeBook : URL = Bundle.main.url(forResource: "BookPlaceHolder", withExtension: "pdf")!

When user selects a new document activeBook is updated with the new URL.

Issue is when the app relaunches from xCode the path to whatever is stored in activeBook changes. My work around so far is to just access the users Documents Directory via FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!. Then appending the last path component of activeBook.

sidebar: obviously this doesn't work if the saved URL in activeBook is still the default from Bundle.main.url. I haven't written that workaround as yet.

Am I only seeing issues in development due to a fresh relaunch via xCode?

If the user only ever terminates the app on their device are the .documentDirectory URL's changing on each launch or do the remain constant?

I don't want to write a workaround to ensure the correct URL for whats stored in @AppStorage, if the changing URL's only occurs due to launching from xCode which the end user wont ever encounter that edge case.

Accepted Reply

How often / under what circumstances are users sandbox directory URL's changed?

That’s not documented but it shouldn’t matter because your app should not be saving references to items in your own container via an absolute path. If you need to store a long-term reference to an item in the file system, save a bookmark.

When you resolve the bookmark, make sure you pay attention to the returned ‘is stale’ value and, if the bookmark is stale, create a new one and replace the stale bookmark with that.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Replies

How often / under what circumstances are users sandbox directory URL's changed?

That’s not documented but it shouldn’t matter because your app should not be saving references to items in your own container via an absolute path. If you need to store a long-term reference to an item in the file system, save a bookmark.

When you resolve the bookmark, make sure you pay attention to the returned ‘is stale’ value and, if the bookmark is stale, create a new one and replace the stale bookmark with that.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"