Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is NSData + (id)dataWithContentsOfURL:(NSURL *)aURL options:(NSDataReadingOptions)mask error:(NSError **)errorPtr: cached automatically?

When I read the section on

NSDataReadingOptions
Options for methods used to read NSData objects.

enum {
   NSDataReadingMappedIfSafe = 1UL << 0,
   NSDataReadingUncached = 1UL << 1,
   NSDataReadingMappedAlways = 1UL << 3,
};
typedef NSUInteger NSDataReadingOptions;

It says that

NSDataReadingUncached A hint indicating the file should not be stored in the file-system caches. For data being read once and discarded, this option can improve performance. Available in OS X v10.6 and later. Declared in NSData.h.

So I am assuming that by default these URL requests are cached and there is no need to implement NSURLRequest to cache data if I want to use shared global cache ? Is this understanding correct ?

like image 286
DrBug Avatar asked Dec 20 '22 02:12

DrBug


2 Answers

Let me start off by saying that dataWithContentsOfURL:options:error: and its ilk are probably the worst APIs for getting something from network. They are very alluring to developers because they can get a resource from network in a single line of code, but they come with some very pernicious side-effects:

First, they block the thread on which they are called. This means that if you execute this on the main thread (the only thread on which your UI can be updated), then your application will appear frozen to the user. This is a really big 'no no' from a user experience perspective.

Second, you cannot cancel these requests, so even if you put this request on a background thread, it will continue to download even though the data may no longer be useful. For example, if your user arrives on a view controller and you execute this request and the user subsequently decides to hit a back button, that data will continue to download, even though it is no longer relevant.

Bottom line: DO NOT USE THESE APIs.

Please use async networking like NSURLConnection or AFNetworking. These classes were designed to get data efficiently and in a way that doesn't impact the user experience. What's even better is that they handle the specific use case you originally asked: how do I stop it from caching on disk?.

like image 198
Wayne Hartman Avatar answered Dec 24 '22 11:12

Wayne Hartman


There is no answer to your specific question which refer to the caches managed by the URL loading system.

The reading options in method dataWithContentsOfFile:options:error: refer solely to reading from the file system (please consult to the official documentation of NSDataReadingOptions).

Unfortunately, the documentation gives no hint about the behavior when reading from remote resources. Whether the response is cached in the URL cache is unspecified - that is, we don't know, and the internal behavior and implementation can change with each new OS version.

IMHO, we should generally avoid to use the "convenient methods" to read from remote resources. It appears, these methods work only by "accident" when accessing remote resources. So, I strongly agree with @Wayne Hartman ;)

like image 41
CouchDeveloper Avatar answered Dec 24 '22 11:12

CouchDeveloper