通过文件路径初始化AGSGeodatabase提示‘File not found’问题求助
Let’s walk through why you’re hitting this error when restarting your app, and how to fix it reliably.
Root Cause
The key problem here is that you’re storing the full absolute sandbox path to your geodatabase (like file:///var/mobile/Containers/Data/Application/[UUID]/Documents/...). iOS app sandbox container UUIDs can change in scenarios like app reinstalls, updates, or even system-level adjustments—but even if it doesn’t shift on restart, hardcoding/storing the full path is inherently unreliable. Additionally, using URL(string:) to create a local file URL can lead to encoding issues with special characters in your filename or path.
Fixes & Recommendations
1. Store Only the Geodatabase Filename, Not the Full Path
Instead of saving the entire absolute path in SettingsManager.geoDatabasePath, just store the filename (e.g., fix the possible typo in 2018-05-134T18:29:01+0530.geodatabase—134 likely should be 13!). Then dynamically build the full path at app startup using FileManager to locate the Documents directory:
// Get the app's Documents directory URL guard let documentsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { print("Could not access Documents directory") return } // Build full URL using stored filename let geoDatabaseFilename = SettingsManager.geoDatabaseFilename // Store just the filename here let geoDatabaseURL = documentsDir.appendingPathComponent("GeoDatabase").appendingPathComponent(geoDatabaseFilename) // Initialize the geodatabase self.generatedGeodatabase = AGSGeodatabase(fileURL: geoDatabaseURL) self.displayLayersFromGeodatabase()
2. Use URL(fileURLWithPath:) for Local Files
If you must work with a path string, use URL(fileURLWithPath:) instead of URL(string:)—it’s purpose-built for local file paths and handles special characters/encoding correctly:
if let geoDatabasePathString = SettingsManager.geoDatabasePath, let geoDatabaseURL = URL(fileURLWithPath: geoDatabasePathString) { self.generatedGeodatabase = AGSGeodatabase(fileURL: geoDatabaseURL) self.displayLayersFromGeodatabase() }
3. Verify File Existence Before Loading
Add a check to confirm the file exists at the target URL before attempting to load the geodatabase. This catches missing files early and provides clearer debugging context:
if FileManager.default.fileExists(atPath: geoDatabaseURL.path) { self.generatedGeodatabase.load(completion: { [weak self] error in if let error = error { print("Error loading geodatabase: \(error.localizedDescription)") } else { self?.displayLayersFromGeodatabase() } }) } else { print("Geodatabase file does not exist at path: \(geoDatabaseURL.path)") }
4. Double-Check Your Filename
Your example URL includes 2018-05-134T18:29:01+0530.geodatabase—the 134 looks like a typo. A malformed filename could prevent the system from locating the file, so confirm the actual filename matches what you’re trying to access.
Debugging Tip
To confirm the geodatabase’s actual location after restart, print the current Documents directory path and compare it to the stored path:
print("Current Documents directory: \(documentsDir.path)")
This will show you the active sandbox container ID, so you can verify if it aligns with the path you saved earlier.
内容的提问来源于stack exchange,提问作者Parv Bhasker

