简体   繁体   English

如何从磁盘获取PFFile?

[英]How to get PFFile from disk?

I want to get a PFFile from disk. 我想从磁盘获取PFFile。 I learned here that Parse caches all PFFiles as they are downloaded. 我在这里了解到,Parse在下载所有PFFile时会对其进行缓存。 I am wondering now how to retrieve that cache. 我现在想知道如何检索该缓存。 What is the correct path. 什么是正确的路径。 The path I use is suggested here 在这里建议使用的路径

NSString *path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0] stringByAppendingFormat:@"/Caches/Parse/PFFileCache/%@", pffile.name];

NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];

data is always nil. 数据始终为零。

Am I using the correct path? 我使用正确的路径吗? /Caches/Parse/PFFileCache/

From your description of the problem you should use a slightly different process. 根据问题的描述,您应该使用稍微不同的过程。 Use the PFFile interface initially, but then store the url s from the files for the case where you need to access the file data later. 首先使用PFFile接口,然后存储文件中的url ,以备以后需要访问文件数据时使用。 In this way you have a direct link to the file data and can use a normal connection or session to download. 这样,您就可以直接链接到文件数据,并且可以使用普通连接或会话进行下载。

If you don't actually need the PFFile for anything about the download then using the REST interface may be more suitable for you. 如果您实际上不需要PFFile进行下载,那么使用REST接口可能更适合您。

Another alternative is to make the same request which originally returned the file information to you, but instructing the API to use cached information only. 另一种选择是发出最初返回文件信息给您的相同请求,但指示API仅使用缓存的信息。 This is done by setting cachePolicy to CACHE_ONLY on your PFQuery . 这是通过设置cachePolicyCACHE_ONLYPFQuery

I struggled with this in Swift for some time, and found this thread which aided in the answer here: 我在Swift中为此苦苦挣扎了一段时间,并发现了这个线程,这有助于回答这里的问题:

let fileURL: String = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0] + ("/Caches/Parse/PFFileCache/\(checkedRemoteAsset.name)")

Thanks to you KKendall! 感谢您KKendall!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM