iosswiftfirebasensdatansdocumentdirectory

Swift -How to know if a file was successfully saved in .cachesDirectory


I have a .mp4 video saved in Firebase and I want to save it to the .cachesDirectory. I want to make sure it is saved and tried to use a do-try block but it wouldn't accept it:

No calls to throwing functions occur within 'try' expression.

How else can I check to see if the file was successfully saved to the .cachesDirectory?

let fbStr = "https://firebasestorage.googleapis.com/v0/b/myApp.appspot.com/o/abcd%277920FHqFBkl7D6j%2F-MC65EFG_qT0KZbdtFhU%2F48127-8C29-4666-96C9-E95BE178B268.mp4?alt=media&token=bf85dcd1-8cee-428e-87bc-91800b7316de"
guard let url = URL(string: fbStr) else { return }
let videoData = NSData(contentsOf: url)
let cachesDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!

do {
    try videoData?.write(toFile: cachesDir.path, atomically: true)
} catch {
    print("video wasn't saved to cachesDirectory") 
}

Solution

  • Use the write(toFile:) method without automically parameter.

    do {
        try videoData?.write(toFile: cachesDir.path, options: .atomic)
    } catch {
        print("video wasn't saved to cachesDirectory")
    }