2017-02-20 8 views
0

私はデバイスにいくつかのファイルをダウンロードするために解析を使用していますが、すべてが良いですが、私はそのファイルを(/ライブラリ/キャッシュ/ 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. 

しかし、私は両方が存在することを確認しています。それは、ファイル名にエンコードされた%20を持たないPFFileの名前と同じように、名前と関係するかもしれません。

これは私のコードブロックです:

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"についてはわかりませんが、これはファインダーでは機能しないため、本当のパス "/ Users"を使用する必要があります。文字列の先頭??

また、私はファイル名のスペースに%20を追加する方法について幸運を捜してきました。明白なオプションのどれも動作していないようです。

答えて

1

スレッドの問題があります。あなたの "waitUntilFinished"の考え方は間違っています。メインスレッドをブロックすることはできません。そうすると警告が表示されます。その警告に耳を傾ける!

getFilePathInBackgroundに電話しないでください。ダウンロードが完了するまで待つ必要はありません。ブロックの中にgetFileInBackgroundWithBlock:と電話すると、コードの残りの部分、つまりファイルを移動することができます。

+0

私はこれを元にしようとしましたが、パスを戻していないので、waituntilfinished()を使用しました。あなたは正しいですが、病気をもう少し見てください。ドキュメントフォルダへの私の構築されたパスの問題は、ファイルでした://これを削除すると、ファイルを細かく移動します – WanderingScouse

+0

"しかし、パスを返さなかった" – matt

+0

私はもう一度それを試して、それをブロックに渡します。何らかの理由で私はそれが私の頭の中にあったのですか? – WanderingScouse

関連する問題