unable to delete file because it's never found because.

my mind is shot. core data hasn't been my cup of tea.

    print("saveTrigger called with trigger: \(trigger)")
    let encoder = JSONEncoder()
    let fm = FileManager.default
    let documentsDirectory = fm.urls(for: .documentDirectory, in: .userDomainMask).first!
    let reflectionsURL = documentsDirectory.appendingPathComponent("reflections.json")
    let triggersDirectory = documentsDirectory.appendingPathComponent("Triggers")
    
    // Create a new reflections file if it doesn't exist
    if !fm.fileExists(atPath: reflectionsURL.path) {
        let emptyData = Data()
        fm.createFile(atPath: reflectionsURL.path, contents: emptyData, attributes: nil)
    }
    
    // Write the trigger to the reflections file
    do {
        let data = try encoder.encode(trigger)
        try data.write(to: reflectionsURL)
        print("Trigger saved to device: \(trigger)")
        print("Reflections file URL: \(reflectionsURL)")
    } catch {
        print("Failed to save trigger: \(error)")
    }
    
    // Find the trigger file with the UUID inside the file
    let triggerFileURL = triggersDirectory.appendingPathComponent("\(trigger.id).json")
    if fm.fileExists(atPath: triggerFileURL.path) {
        do {
            let data = try Data(contentsOf: triggerFileURL)
            let uuid = try JSONDecoder().decode(UUID.self, from: data)
            let uuidString = uuid.uuidString
            let matchingTriggerFileURL = triggersDirectory.appendingPathComponent("\(uuidString).json")
            if fm.fileExists(atPath: matchingTriggerFileURL.path) {
                try fm.removeItem(at: matchingTriggerFileURL)
                print("Trigger file deleted: \(matchingTriggerFileURL.lastPathComponent)")
            } else {
                print("Trigger file not found")
            }
        } catch {
            print("Error deleting trigger file: \(error.localizedDescription)")
        }
    } else {
        print("Trigger file not found")
    }
}

core data hasn't been my cup of tea.

Core Data is certainly an acquired taste, but I’m not sure how it fits into this question. Are you saying that you’re using this code because you don’t want to use Core Data?

If so, that’s fine. Core Data is great, but it’s certainly not the right option for all your persistence needs.

Beyond that, it’s not clear what you’re asking about in this post. The subject is “unable to delete file because it's never found because” and you’ve posted a bunch of code, but you then don’t go into an details about how that code is failing. If you step through the code, where do things go wrong?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Apologies you are correct I was confusing CoreData with FileSystem, nor did I post the error logs.

Trigger file name: 8B2E883D-999D-4597-9563-D78A4C10833C.json
saveTrigger called with trigger: Trigger(id: DE3354C2-BA2A-4254-9266-A481D06C2B4E,///content)
Trigger saved to device: Trigger(id: DE3354C2-BA2A-4254-9266-A481D06C2B4E,///content)
Reflections file URL: file:///Users/yuta/Library/Developer/CoreSimulator/Devices/B482E416-FCE1-46D3-9CB6-03309F617081/data/Containers/Data/Application/DE2A1CC7-F6E6-4B90-991E-545C1EC831AF/Documents/reflections.json
deleteTrigger called with title: Why
Error deleting trigger file: The folder “Triggers” doesn’t exist.

I believe it's renaming the file therefore cannot ultimately delete the file later, I've tried a few solutions such as using the UUID line in each json file and having it look through the directory for a file with that matching string however that fails. I'm wondering now if I need to just throw the deletion task back to the list view and handle it there after it creates the new file.

Consider this snippet from your log:

Reflections file URL: …
deleteTrigger called with title: Why
Error deleting trigger file: The folder “Triggers” doesn’t exist.

Where does that middle line come from? It’s not list in the snippet you posted, and I don’t see your code calling out to some other function that might print that.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

since im unable to post more then 500 characters in a response.

Loaded reflections from device: [Triggers.Trigger(id: A3625B83-0B81-44A8-8D19-2D010FA07652, title: "Eo", description: "", date: 2023-10-05 02:01:08 +0000, preventible: false, alternateHandling: "New", alternatePerspective: "", reflectionDate: 2023-10-05 02:01:20 +0000)]
Index: 0
Number of reflections: 1
Reflection file does not exist: file:///Users/macos/Library/Developer/CoreSimulator/Devices/B482E416-FCE1-46D3-9CB6-03309F617081/data/Containers/Data/Application/22FB33CF-F740-4350-807A-C302B4D0F7BF/Documents/reflections/A3625B83-0B81-44A8-8D19-2D010FA07652.json

also I am able to successfully delete it with this code in the main list view

    func deleteTrigger(at index: Int) {
        let fm = FileManager.default
        let documentsDirectory = fm.urls(for: .documentDirectory, in: .userDomainMask).first!
        let triggersDirectory = documentsDirectory.appendingPathComponent("triggers")
        let trigger = triggers[index]
        let triggerFile = triggersDirectory.appendingPathComponent("\(trigger.id).json")
        if fm.fileExists(atPath: triggerFile.path) {
            do {
                try fm.removeItem(at: triggerFile)
                
                // Reload triggers from the file system to update the array on the main thread
                DispatchQueue.main.async {
                    self.triggers = self.loadTriggers()
                }
            } catch {
                print("Failed to delete trigger: \(error.localizedDescription)")
            }
        } else {
            print("Trigger file does not exist.")
        }
    }

At which line do you write a file to the triggersDirectory folder? 'triggersDirectory' appears three times, but I don't see that. You do write a file to the reflectionsURL folder.

unable to delete file because it's never found because.
 
 
Q