Requirement: Get the data from the cache if NSMutableURLRequest
can not fetch data from the server.If internet available and i got the data from the server then function will return server data but if request fail to get the data from the server then function will return the NSCache
data according to cachePolicy
.
To resolve this issue i changed the cachePolicy
from NSURLRequestReloadIgnoringLocalCacheData
to NSURLRequestReturnCacheDataElseLoad
. Now I am getting the data from NSCache
.
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.timeoutIntervalForRequest=TimeOfRequestOut;
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad //HERE IS THE CHANGE
timeoutInterval:TimeOfRequestOut];
Now, I need to find out when this function returning NSCache
data and when it's returning data from live server and It will also helpful, if someone tell me is it right approach to get cache data?