0

I am attempting to save an NSPersistentHistoryToken. Before the attempt to save, I check if the file exists, following Apple's documentation, Keep Track of Your Place in the History, here. However, NSFileManager.fileExists(atPath:) always returns false.

I copied Apple's implementation, which is provided in the referenced documentation, and contained it within a method:

func tokenURL() -> URL {
  let url = NSPersistentContainer.defaultDirectoryURL().appendingPathComponent(
    "MyProject",
    isDirectory: true
  )
  if !FileManager.default.fileExists(atPath: url.path()) {
    print("==> FILE DOES NOT EXIST AT PATH (\(String(describing: url.path())))")
    print("==>       CREATING FILE AT PATH (\(String(describing: url.path())))")
    do {
      try FileManager.default.createDirectory(
        at: url,
        withIntermediateDirectories: true,
        attributes: nil
      )
    } catch {
      let message = "Could not create persistent container URL"
      print("###\(#function): \(message): \(error)")
    }
  }
  return url.appendingPathComponent("token.data", isDirectory: false)
}

I checked other questions about this issue, and it seems that others were using url.absoluteString, but I am using url.path(), which is what was recommended in the related answers.

I checked:

The output I see in the debug console follows:

==> FILE DOES NOT EXIST AT PATH (/var/mobile/Containers/Data/Application/EDD6A72F-C22F-4643-8366-01391DDC8EAB/Library/Application%20Support/MyProject/)
==>       CREATING FILE AT PATH (/var/mobile/Containers/Data/Application/EDD6A72F-C22F-4643-8366-01391DDC8EAB/Library/Application%20Support/MyProject/)
==> FILE DOES NOT EXIST AT PATH (/var/mobile/Containers/Data/Application/EDD6A72F-C22F-4643-8366-01391DDC8EAB/Library/Application%20Support/MyProject/)
==>       CREATING FILE AT PATH (/var/mobile/Containers/Data/Application/EDD6A72F-C22F-4643-8366-01391DDC8EAB/Library/Application%20Support/MyProject/)
==> FILE DOES NOT EXIST AT PATH (/var/mobile/Containers/Data/Application/EDD6A72F-C22F-4643-8366-01391DDC8EAB/Library/Application%20Support/MyProject/)
==>       CREATING FILE AT PATH (/var/mobile/Containers/Data/Application/EDD6A72F-C22F-4643-8366-01391DDC8EAB/Library/Application%20Support/MyProject/)
==> FILE DOES NOT EXIST AT PATH (/var/mobile/Containers/Data/Application/EDD6A72F-C22F-4643-8366-01391DDC8EAB/Library/Application%20Support/MyProject/)
==>       CREATING FILE AT PATH (/var/mobile/Containers/Data/Application/EDD6A72F-C22F-4643-8366-01391DDC8EAB/Library/Application%20Support/MyProject/)

Based on the output, I expect the file to exist and for the file to not be recreated upon each access.

What am I doing incorrectly?

Nick Kohrn
  • 5,779
  • 3
  • 29
  • 49

0 Answers0