1

I'm using Parse to download my PFFiles for my videos and images on my backend. However after i get the file url from the PFFile, it takes forever to load. Why is this? Does using a file url mean you are essentially downloading the data? Like the PFFile was just a network pointer to the actual image/videos?

NSURL *fileUrl = [NSURL URLWithString:scene.file.url];
AVAsset *asset1 = [AVAsset assetWithURL:fileUrl];
AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:asset1];
[QPlayer insertItem:item afterItem:nil];

If this is true, Would a NSURLCache allow me to save THAT DATA locally without having to say go to this server via this url to get THAT DATA?

Essentially I'm asking am I downloading this twice - the PFFILE then the data its contents point to? And if so would an NSCache allow me to only do the second download once?

Thanks! I'm trying to really get the conceptual understanding of networking.

ian
  • 1,002
  • 2
  • 12
  • 29

1 Answers1

1

The OP code as presented does just one synchronous download. But if it got the scene object from parse just before, then there are two network operations, both necessary, as you point out.

You can cache whatever you wish in an NSCache.

The file download is probably slow because the file is large. And it must feel very slow because the synchronous operation -- if done on the main -- blocks the UI.

danh
  • 62,181
  • 10
  • 95
  • 136
  • Its actually in an async block dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{}); sorry i didnt post that – ian Dec 22 '15 at 17:04
  • So for the second request, the one using the file url, could I use an NSURLCache and speed that up for every time after the first time its called? Or is that an NSCache? – ian Dec 22 '15 at 17:07
  • Yes. But to use it, you'll need to make the http request yourself, not via assetWithURL. – danh Dec 22 '15 at 17:12
  • Okay i'll implement an NSURLRequest with the file url. However when it comes to using that cached response object, can i not use that same URL for assetWithURL? I thought with the cachePolicy it checks locally for that url and then checks the origin(parse backend in this case)? – ian Dec 22 '15 at 17:17
  • I think because the target is an AVAsset, you'll need to operate at a lower level, creating a cached file, and using an NSCache to cache its data. – danh Dec 22 '15 at 17:27
  • I can only create an AVAsset with a url though. Will I be able to use another url pointing to my cached file? – ian Dec 22 '15 at 17:29
  • Right, so operating at a lower level, you'll to the http request that receives NSData, write that data to the NSCaches directory (generating a local URL) and initialize the asset with the local URL. You can cache in memory as well, using NSCache. – danh Dec 22 '15 at 17:35
  • Wait I did some digging and I cant find what you mean by using the NSCachesDirectory vs caching in the memory with NSCache. Could you point me to some literature explaining the difference or a simple answer if you can? – ian Dec 22 '15 at 17:45
  • Are you referring to the /documents vs /temp directories? – ian Dec 22 '15 at 17:47
  • Please see here (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/#//apple_ref/c/func/NSSearchPathForDirectoriesInDomains) and then NSCachesDirectory in this enum for the first parameter (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/index.html#//apple_ref/doc/c_ref/NSSearchPathDirectory) – danh Dec 22 '15 at 18:07
  • Here's somebody doing nearly exactly what you're looking for, I think...http://stackoverflow.com/questions/14898994/ios-how-do-i-asynchronously-download-and-cache-images-and-videos-for-use-in-my-a – danh Dec 22 '15 at 18:08
  • yeah I'm actually using that code! haha I understand what and why theyre doing it now though – ian Dec 22 '15 at 18:09
  • You might consider starting without the NSCache. The file system should be pretty quick.... if the file exists, use it, otherwise create it with the nsurl request, then use it. – danh Dec 22 '15 at 18:17
  • Alright. that method is working well. But now my video is freezing up – ian Dec 22 '15 at 18:20