With FileManager on iOS, a scanning app saved each scan's absolute path in its database, and after an app update the scans seem gone: why, and what fixes it?
answer
- the files are probably still there
- an identifier inside the container path
- ask for the folder on every launch
- a base directory plus a relative part
- a one-time migration of old rows
basics
~20 sAn iOS app's container path is not guaranteed to survive an update, reinstall or restore, so stored absolute paths name a folder that no longer exists. Store a base folder plus a relative path and rebuild the URL from URL.documentsDirectory.
solid answer
~40 sThe scans are almost certainly still there. An iOS app's data container sits under a path that contains a system-assigned identifier, and Apple makes no promise that this path stays the same; after an update, a reinstall or a restore onto a new phone it can differ, so a stored `/var/mobile/Containers/Data/Application/<old id>/Documents/...` string points at nothing and `fileExists(atPath:)` returns false. The fix is to store which base folder a file lives in plus its path relative to that folder, such as `Scans/2026/receipt-0412.pdf`, and to rebuild the URL on use with `URL.documentsDirectory.appending(path:)`. For existing rows, a one-time migration strips everything up to `/Documents/` and rewrites them in the new form.
code
swift · 22 linesimport Foundation
struct StoredFileRef: Codable, Hashable {
enum Base: String, Codable { case documents, applicationSupport, caches }
var base: Base
var relativePath: String // "Scans/2026/receipt-0412.pdf"
var url: URL {
let root: URL = switch base {
case .documents: .documentsDirectory
case .applicationSupport: .applicationSupportDirectory
case .caches: .cachesDirectory
}
return root.appending(path: relativePath)
}
// one-time migration of an absolute path saved by an older release
static func migrating(absolutePath: String) -> StoredFileRef? {
guard let range = absolutePath.range(of: "/Documents/") else { return nil }
return StoredFileRef(base: .documents, relativePath: String(absolutePath[range.upperBound...]))
}
}go deeper
Recall that the container's absolute path can change and that the Documents folder is looked up fresh, never saved as a string.
Explain what to store instead, a base folder plus a relative path, and how appending(path:) rebuilds the URL on each use.
Diagnose the empty-library report as stale paths over intact files, and write a migration that rewrites old rows safely without deleting any it cannot map.
Make the stored-reference type the only way code may persist a file location, so the rule is enforced by the type rather than by review.
## Why the stored paths broke An iOS app's writable folders live in its **data container**, and the absolute path to that container includes an identifier the system assigns. Apple does not guarantee that path: it can be different after the app is updated, deleted and reinstalled, or restored from a backup onto another device. The system carries the container's **contents** across an update; it does not promise to keep its **address**. An app that saved `fileURL.path` or `fileURL.absoluteString` in a database therefore holds strings that name the old address. After the change: - `fileExists(atPath:)` returns false for every stored path. - `Data(contentsOf:)` throws `fileReadNoSuchFile`. - The UI shows an empty library, and users report that the update deleted their scans. The files are in `Documents` under the new container path the whole time. The same habit shows up during development: an app reinstalled from Xcode often gets a new container path, which is where the bug is usually first seen and then dismissed. ## What to store instead Store what does not change: **which base folder** the file belongs to and **the path inside it**. | Stored value | Survives a container path change | Notes | |---|---|---| | `url.path` or `url.absoluteString` | no | names the old container | | `url.lastPathComponent` alone | yes, if every file sits in one folder | breaks once files are nested | | base folder plus relative path | yes | rebuild with `URL.documentsDirectory.appending(path:)` | | `fileResourceIdentifier` resource value | no | documented as not persistent across system restarts | The base folder matters because an app usually stores files in more than one place: scans in `Documents`, the downloaded model in `Application Support`, thumbnails in `Caches`. A small enum for the base and a string for the relative part are enough. For files in an App Group container, the base is the URL returned by `containerURL(forSecurityApplicationGroupIdentifier:)`, which the documentation says is the only way to reach that folder on iOS. Rebuild the URL at the moment of use, and never persist the result of `URL.documentsDirectory` itself, for example in a settings value written at first launch. ## Migrating the rows you already have 1. On launch, look for rows still holding an absolute path, for example any string that starts with `/`. 2. Find the base folder's name in the string, such as `/Documents/`, and keep everything after it as the relative path. 3. Check that the rebuilt URL exists with `fileExists(atPath:)`, write the new form back, and record that the migration ran. 4. Rows that match no known base folder are logged rather than deleted, so a bug in the migration cannot destroy data. The migration is cheap string work and runs once. If the rows live in a persistent store, its schema change follows that store's migration rules, covered with that framework. ## Derived files follow the same rule Thumbnails in `Caches` and anything the app keys by a file's location should use the same relative reference. A thumbnail cache keyed by the scan's absolute path misses every entry after the container moves and silently regenerates the lot; keyed by `Scans/2026/receipt-0412.pdf`, it keeps working. The same applies to a store file's location passed to a persistence framework: build it from the directory lookup at launch rather than saving it. ## Files outside the sandbox A relative path works only for files inside the app's own folders. For a file outside the sandbox, such as one the user picked from another location, the tool is a **bookmark** created from the URL, which the FileManager documentation describes as a more sure way of locating an item later than a path; that flow belongs with the document-picker and iCloud material. ## Review checklist - No database column, preferences key or JSON file holds an absolute path or a `file://` string. - One type turns a stored reference into a `URL`, so the rule lives in one place. - Diagnostics print `URL.documentsDirectory` at launch, which makes a moved container obvious in a bug report. - Tests round-trip a reference through storage and rebuild it against a different base folder.
- Why not rebuild the URL with URL(filePath: relativePath) and no base?A relative file path with no base URL resolves against the process's current working directory, not against Documents, so the URL points somewhere unrelated. Always join the relative part to the base folder with `appending(path:)`, or pass the base explicitly with `URL(filePath:directoryHint:relativeTo:)`.
- How do you prove in a bug report that the container moved rather than the files being deleted?Log `URL.documentsDirectory` and a count of `contentsOfDirectory(at:includingPropertiesForKeys:options:)` at launch. If the folder holds the expected number of PDFs while every stored path fails `fileExists(atPath:)`, the stored paths are stale and the data is intact; the migration, not a restore, is the fix.
Writing down a scan's absolute path is like recording a book's location as a street address and shelf number; when the library moves to a new building, the address points at an empty lot. Recording only "third floor, shelf C" finds the book in whichever building the library is in.
- Saving url.path or url.absoluteString in the database as the file's identity
- Concluding that the app update deleted the user's files
- Persisting the Documents URL in a setting at first launch and reusing it
- Hardcoding a container identifier seen in the Simulator or a log
- Deleting database rows whose stored path no longer exists