简体   繁体   English

下载后移动文件

[英]Move File After Downloading

Im using parse to download some files to the device and all is good but i want to then move the file from the default cache directory which is where parse stores them (/Library/Caches/PFFileCache/) to somewhere more stable say the users documents directory. 我使用解析将一些文件下载到设备上,一切都很好,但是我想将文件从解析存储它们的默认缓存目录(/ Library / Caches / PFFileCache /)移动到更稳定的位置,说用户文档目录。

The localised error i am getting is: 我得到的本地化错误是:

Error: “7b54d8a0f1a64b710058d4408ca4d696_The%20Name%20of%20the%20Wind%2029-92.mp3” couldn’t be moved to “Documents” because either the former doesn't exist, or the folder containing the latter doesn't exist.

But I'm sure both exist. 但是我确信两者都存在。 It may be something to do with the name as when i ge the name from the PFFile it doesn't have the encoded %20 in the file name. 这可能与名称有关,因为当我从PFFile命名名称时,文件名中没有编码的%20。

This is my code block: 这是我的代码块:

    let cachedPFFile = object.object(forKey: "partAudio") as! PFFile
        print(cachedPFFile)
    let getCachedFilePath = cachedPFFile.getPathInBackground()
        getCachedFilePath.waitUntilFinished()
    let cachedFilePath = getCachedFilePath.result as! String
        print(cachedFilePath)

    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentsDirectory = String(describing: paths[0])

    let saveDirectory = documentsDirectory.appending(cachedPFFile.name)

        print(documentsDirectory)
        print(saveDirectory)

    let fileManager = FileManager.default

    if fileManager.fileExists(atPath: saveDirectory) == false {

        do {
            try fileManager.moveItem(atPath: cachedFilePath, toPath: saveDirectory)
            print("Move successful")
        } catch let error {
            print("Error: \(error.localizedDescription)")
        }

    }

This is the entire log: 这是整个日志:

<PFFile: 0x608000458f00>
2017-02-20 18:42:35.430 ParseStarterProject-Swift[2260:55934] Warning: A long-running operation is being executed on the main thread. 
 Break on warnBlockingOperationOnMainThread() to debug.
/Users/Genie/Library/Developer/CoreSimulator/Devices/A2FB00CE-B018-4FDF-9635-35FD6678DF8D/data/Containers/Data/Application/BA7C112C-BECB-4734-8C67-A9CB84F0E1F3/Library/Caches/Parse/PFFileCache/7b54d8a0f1a64b710058d4408ca4d696_The%20Name%20of%20the%20Wind%2029-92.mp3
file:///Users/Genie/Library/Developer/CoreSimulator/Devices/A2FB00CE-B018-4FDF-9635-35FD6678DF8D/data/Containers/Data/Application/BA7C112C-BECB-4734-8C67-A9CB84F0E1F3/Documents/
file:///Users/Genie/Library/Developer/CoreSimulator/Devices/A2FB00CE-B018-4FDF-9635-35FD6678DF8D/data/Containers/Data/Application/BA7C112C-BECB-4734-8C67-A9CB84F0E1F3/Documents/7b54d8a0f1a64b710058d4408ca4d696_The Name of the Wind 29-92.mp3
Error: “7b54d8a0f1a64b710058d4408ca4d696_The%20Name%20of%20the%20Wind%2029-92.mp3” couldn’t be moved to “Documents” because either the former doesn't exist, or the folder containing the latter doesn't exist.

I am confused by this because both the file and the new directory exist?? 我对此感到困惑,因为文件和新目录都存在?

though I'm not sure about the "file:///Users" as this doesn't work in finder, have to use the true path "/Users" how can i remove "file://" from the start of the string?? 尽管我不确定“ file:/// Users”,因为它在finder中不起作用,但必须使用真实路径“ / Users”,我如何才能从“开始”中删除“ file://”串??

Also I've been searching SO without luck on how to add the %20 in the spaces of the file name none of the obvious options seem to work. 另外,我一直在搜索SO,但没有运气如何在文件名的空格中添加%20,似乎没有一个显而易见的选项起作用。

You have threading issues. 您有线程问题。 Your entire idea of "waitUntilFinished" is wrong. 您对“ waitUntilFinished”的整个想法是错误的。 You cannot block the main thread — and you are getting a warning telling you so. 您不能阻塞主线程,并且会收到警告告知您。 Listen to that warning! 听那个警告!

Do not call getFilePathInBackground , and do not wait for the download to finish. 不要调用getFilePathInBackground ,也不要等待下载完成。 Instead, call getFileInBackgroundWithBlock: , and inside the block proceed with the rest of your code, ie move the file. 而是调用getFileInBackgroundWithBlock:然后在块内继续执行其余代码,即移动文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM