Search code examples
iosfinderfile-manager

Why is there a mismatch between FileManager and Finder?


If I print the document directory when the simulator starts, I get this:

let simulatorPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
print(simulatorPath)


//prints -> /Users/name/Library/Developer/CoreSimulator/Devices/[device-id]/data/Containers/Data/Application/[application-id]/Documents

If I open this directory with Finder, there are a few other folders there: /Library, /SystemData, and /tmp. Specifically, /Library/Caches/[domain.app]/[several cache files].

However, in my app, I'm creating a sqlite database using the file manager. And if I do the following after creating the database:

var path = FileManager.default.currentDirectoryPath + "Library/Caches"

let enumerator = FileManager.default.enumerator(atPath: path)
while let obj = enumerator?.nextObject() as? String {
  print(obj)
}

This is printed:

ColorSync
ColorSync/com.apple.colorsync.devices
Desktop Pictures
Desktop Pictures/78DJQ81B-3D2C-46C7-A268-3CE1903213FE
Desktop Pictures/78DJQ81B-3D2C-46C7-A268-3CE1903213FE/lockscreen.png
domain.app
domain.app/SQLite
com.apple.cloudkit
com.apple.cloudkit/com.apple.cloudkit.launchservices.hostnames.plist
com.apple.iconservices.store
app
app/SQLite
app/SQLite/cache.db

But the contents of the /Library/Caches/[domain.app] folders are different when printed with the enumerator, vs visited in Finder. Am I wrong in assuming that

NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

and

FileManager.default.currentDirectoryPath + "/Documents"

point to the same location?


Solution

  • After stumbling around Finder for a while, I discovered the SQLite db file I was creating was showing up in /Library/Caches, but in my hard drive's root directory. According to docs for FileManager.default.currentDirectoryPath:

    When an app is launched, this property is initially set to the app’s current working directory. If the current working directory is not accessible for any reason, the value of this property is nil.

    Given this, I figured when I printed that value, and / came out, it was relatively referring to the simulated app's current directory. If anyone knows why this is the case, comments welcome.