2017-02-20 81 views
0

即時通訊使用解析下載一些文件到設備,一切都很好,但我想然後將文件從默認緩存目錄,這是解析存儲他們(/庫/ 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沒有任何明顯的選項似乎工作。

回答

1

您有線程問題。你的「waitUntilFinished」的想法是錯誤的。你不能阻止主線程 - 而且你會收到警告。聽聽那個警告!

請勿撥打getFilePathInBackground,也不要等待下載完成。相反,在塊內部撥打getFileInBackgroundWithBlock:,繼續執行其餘代碼,即移動文件。

+0

我最初嘗試過,但它沒有返回一個路徑,所以去了waituntilfinished()。你是對的,但是再看看它。與我構建的文檔文件夾路徑的問題是文件:/ /如果我刪除它移動文件罰款 – WanderingScouse

+0

「但它沒有返回路徑」它將路徑_int到block_。 – matt

+0

我只是試了一遍,它確實把它交給了這個塊。由於某種原因,我在我的腦海中它沒有? – WanderingScouse