[英]Move File After Downloading
我使用解析將一些文件下載到設備上,一切都很好,但是我想將文件從解析存儲它們的默認緩存目錄(/ 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”,我如何才能從“開始”中刪除“ file://”串??
另外,我一直在搜索SO,但沒有運氣如何在文件名的空格中添加%20,似乎沒有一個顯而易見的選項起作用。
您有線程問題。 您對“ waitUntilFinished”的整個想法是錯誤的。 您不能阻塞主線程,並且會收到警告告知您。 聽那個警告!
不要調用getFilePathInBackground
,也不要等待下載完成。 而是調用getFileInBackgroundWithBlock:
然后在塊內繼續執行其余代碼,即移動文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.