【问题标题】:Move File After Downloading下载后移动文件
【发布时间】:2017-02-20 08:53:49
【问题描述】:

我正在使用 parse 将一些文件下载到设备上,一切都很好,但我想将文件从 parse 存储它们的默认缓存目录 (/Library/Caches/PFFileCache/) 移动到更稳定的地方用户文档目录。

我得到的本地化错误是:

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.

但我确信两者都存在。这可能与名称有关,因为当我从 PFFile 中获取名称时,它的文件名中没有编码的 %20。

这是我的代码块:

    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)")
        }

    }

这是整个日志:

<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.

我对此感到困惑,因为文件和新目录都存在??

虽然我不确定“file:///Users”,因为这在 finder 中不起作用,但必须使用真正的路径“/Users”,如何从字符串的开头??

另外,我一直在寻找如何在文件名的空格中添加 %20 的问题,但没有一个明显的选项似乎有效。

【问题讨论】:

    标签: swift string url nsfilemanager


    【解决方案1】:

    您有线程问题。您对“waitUntilFinished”的整个想法是错误的。你不能阻塞主线程——你会得到一个警告告诉你。听那个警告!

    不要拨打getFilePathInBackground,也不要等待下载完成。相反,请调用 getFileInBackgroundWithBlock:,然后在块内继续执行其余代码,即移动文件。

    【讨论】:

    • 我最初尝试过,但它没有返回路径,所以使用 waituntilfinished()。不过你说得对,还是多看几眼吧。我构建的文档文件夹路径的问题是文件://如果我删除它,它会很好地移动文件
    • “但它没有返回路径”它将路径放入块中
    • 我又试了一次,它确实把它交给了块。出于某种原因,我的脑海里有它没有??
    • 对。因此,现在将 all 其余代码移入块中,一切都会好起来的,因为您将不再阻塞主线程。永远不要再这样做了! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多