2017-02-20 97 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