Image stored in documents directory disappears on app relaunch/rebuild

Hello, I am relatively new to swift and completely new to FileManager, and I encountered some very strange behaviour of my app.

I have a "Day" struct that can save an image provided to it by the user:

struct Day: Codable, Comparable, Equatable, Identifiable {
      /*
     some vars and inits
     */

     private var dayImagePath: URL?

     var displayedDayImage: UIImage? {
        guard let dayImagePath = dayImagePath else { return nil }
        do {
            let data = try Data(contentsOf: dayImagePath)
            guard let image = UIImage(data: data) else { throw ImageProcessingError.noDayImage }
            return image
        } catch {
            print(error.localizedDescription)
            return UIImage(systemName: "questionmark.folder")
        }
    }


    mutating func addDayImage(_ image: UIImage) {

        if dayImagePath == nil {
            dayImagePath = FileManager.documentsDirectory.appendingPathComponent(id.toStringId().appending("DayImage.jpeg"))
        }

        if let data = image.jpegData(compressionQuality: 0.8) {
            do {
                try data.write(to: dayImagePath!, options: [.atomic, .completeFileProtection])
            } catch {
                print(error.localizedDescription)
            }
        }
    }

    /*
     some more code here
     */
}

The documents directory is found using this FileManager extension:

extension FileManager {
    static var documentsDirectory: URL {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return paths[0]
    }
}

Adding the image works well, the image is displayed and stored correctly. However, it gets lost on the next app relaunch/rebuild with the following error message: "The file “20220917DayImage.jpeg” couldn’t be opened because there is no such file." What am I doing wrong? I would be very grateful for any suggestions. Thank you!

Hi, every relaunch/rebuild reinstalls app with new generated guid in the intall path of the app, that's why app can not find the file in app documents directory.

As Vlad has mentioned, the app is completely rebuilt and is located in a different UUID folder so you cannot access files from the older build.

If you're expecting to be able to use files from a different installation - which is what it is - you're going to be disappointed. The app is sandboxed so you cannot access files from other apps.

If you were able to use files from a previous install, iPhones and Watches would fill up pretty quickly with files that cannot be deleted (the user deleted your app but you left some files hanging around that can only be accessed from a new version of your app which the user isn't guaranteed to install).

You need to rethink/remodel what you want to happen in the lifecycle of your app. Why do you need to refer to files from previous installs? Can't you just download the files you need for the current install?

Image stored in documents directory disappears on app relaunch/rebuild
 
 
Q