A mobile app has the ability to leave a lasting impression on a user and whether that impression is good or bad depends on a lot of factors.One of the most straight-forward factor that give edge to user experience is to make things faster.Many mobile apps communicate with URL to populate data in the app using network request. What if, network performance is slow or network is unavailable. There’s a solution to this problem known as Network Cache.
What is Network Cache ?
A network cache is a repository for stored data that is used to expedite the process of retrieving data. Network caching reduces the number of requests that need to be made to the server, as that data does not have to be recomputed or fetched from its original location and improve the experience of using an application offline or under slow network conditions.
Network caching in iOS mobile app development.
The NSURLCache is a class that implements the caching of responses to URL load requests in iOS mobile app development programming language . It provides a composite in-memory and on-disk cache, and lets you manipulate the sizes of both the in-memory and on-disk portions.
To create cache, first thing to do is write below snippet in didFinishLaunchingWithOptions
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:2*1024*1024 diskCapacity:100*1024*1024 diskPath:nil]; [NSURLCache setSharedURLCache:sharedCache];
In above code, NSURLCache is declared with 2mb of memory and 100mb of disk space.
Setting the cache policy on NSURLRequest Objects.
You make an HTTP request using NSURLRequest class object.NSURLRequest has a cachePolicy property, which specifies the caching behaviour of the request according to the following constants:
- NSURLRequestUseProtocolCachePolicy: Caching logic defined in the protocol implementation is used for a particular URL load request. This is the default policy.
- NSURLRequestReloadIgnoringLocalCacheData: Data should be loaded from the originating source. No existing cache data should be used.
- NSURLRequestReloadIgnoringLocalAndRemoteCacheData: Not only should the local cache data be ignored, but proxies and other intermediates should be instructed to disregard their caches so far as the protocol allows.
- NSURLRequestReturnCacheDataElseLoad: Existing cached data should be used, regardless of its age or expiration date. If there is no existing data in the cache corresponding to the request, the data is loaded from the originating source.
- NSURLRequestReturnCacheDataDontLoad: Existing cache data should be used, regardless of its age or expiration date. If there is no existing data in the cache corresponding to the request, no attempt is made to load the data from the originating source, and the load is considered to have failed, (i.e. “offline” mode).
- NSURLRequestReloadRevalidatingCacheData: Existing cache data may be used provided the origin source confirms its validity, otherwise the URL is loaded from the origin source.
The following snippet shows you how you can define a cache policy and a timeout.Cache data is received depending on the above cache policy.
NSURL *url = [NSURL URLWithString:@"http://google.com"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:20.0];
How to save data as cache from NSURLRequest
NSCachedURLResponse *cachedURLResponse = [[NSCachedURLResponse alloc] initWithResponse:operation.response data:[NSKeyedArchiver archivedDataWithRootObject:responseObject] userInfo:nil storagePolicy:NSURLCacheStorageAllowed]; //store in cache [[NSURLCache sharedURLCache] storeCachedResponse:cachedURLResponse forRequest:request];
How to retrieve cache data from NSURLRequest
NSCachedURLResponse *cachedURLResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request]; NSData *responseData; //check if has cache if(cachedURLResponse && cachedURLResponse != (id)[NSNull null]){ responseData = [cachedURLResponse data]; NSError *error = nil; NSDictionary *responseDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:responseData]; responseBlock(responseDictionary, error); }
Conclusion
Cache is a powerful weapon for mobile app development that increase the speed of retrieving application contents resulting increase in overall speed of the mobile app and also allow offline usage by providing cache content.Hence making user app experience gratifying.