Download And Save File From URL in Swift : <p>I'm trying to...</p>

<ul>
<li><p>download a file (an MLModel) from a server</p></li>
<li><p>save that file to the device </p></li>
<li><p>re-launch the app</p></li>
<li><p>use the URL (path) that the file was saved to, to access the downloaded file.. so I don't have to re-download it every time the app launches</p></li>
</ul>

<p>let download = URLSession.shared.downloadTask(with: url) { (urlOrNil, response, error) in</p>

<pre><code>    if let error = error {
                print("❌ There was an error in \(#function) \(error)")
                completion(nil)
                return
            }


// the location the mlModel is saved at
            guard let fileURL = urlOrNil else {print("🔥❇️&gt;&gt;&gt;\(#file) \(#line)"); return  }
            do {
                //compiles the model
                let compiledUrl = try MLModel.compileModel(at: fileURL)

            //turns the model into an MLModel
            let model = try MLModel(contentsOf: compiledUrl)
            print("newModel Complete 🍪")


            // find the app support directory
            let fileManager = FileManager.default
            // Finds a place to save the MLModel
            let appSupportDirectory = try! fileManager.url(for: .applicationSupportDirectory,
                                                           in: .userDomainMask, appropriateFor: compiledUrl, create: true)

            // create a permanent URL in the app support directory
            let permanentUrl = appSupportDirectory.appendingPathComponent(compiledUrl.lastPathComponent)



            print("🎨\(permanentUrl)")


            // if the file exists, replace it. Otherwise, copy the file to the destination.
            if fileManager.fileExists(atPath: permanentUrl.absoluteString) {
                _ = try fileManager.replaceItemAt(permanentUrl, withItemAt: compiledUrl)
            } else {
                try fileManager.copyItem(at: compiledUrl, to: permanentUrl)
            }

            let newModel = try MLModel(contentsOf: permanentUrl)
            print("🎟", newModel)



            completion(newModel)
        }catch{
            print("❌ There was an error in \(#function) \(error) : \(error.localizedDescription)")
        }
    }.resume()
</code></pre>

<p>From what I understand, this is saving my MLModel to the "permanentUrl". So I restart the app, I don't run downloadTask again (because the MLModel should be saved?), and I try to access the MLModel that I copied to the permanentURL with this code </p>

<pre><code>let permanentUrl = URL(string:"file:///var/mobile/Containers/Data/Application/659B2FEE-4384-4BBE-97D1-B1575BF1EB3B/Library/Application%20Support/CFNetworkDownload_v3EpLD.mlmodelc")

    do {
        let model = try MLModel(contentsOf: permanentUrl!)

        print(model)
    }catch{
        print("❌ There was an error in \(#function) \(error) : \(error.localizedDescription)")
    }
</code></pre>

<p>I get this Error</p>

<blockquote>
  <p>file:///var/mobile/Containers/Data/Application/659B2FEE-4384-4BBE-97D1-B1575BF1EB3B/Library/Application                 귑ీƢupport/CFNetworkDownload_v3EpLD.mlmodelc does not exist" UserInfo={NSLocalizedDescription=file:///var/mobile/Containers/Data/Application/659B2FEE-4384-4BBE-97D1-B1575BF1EB3B/Library/Application                 귑ీƢupport/CFNetworkDownload_v3EpLD.mlmodelc does not exist} : file:///var/mobile/Containers/Data/Application/659B2FEE-4384-4BBE-97D1-B1575BF1EB3B/Library/Application                 귑ీƢupport/CFNetworkDownload_v3EpLD.mlmodelc does not exist</p>
</blockquote>

<p>So I guess the MLModel wasn't saved? What am I missing here? I assume I saved the MLModel with downloadTask and I'm just not retrieving it the right way. How do I access the file that I had saved?</p>
