update image cache method

This commit is contained in:
Pan, Xinlei (Ryan) 2018-04-20 13:47:21 -04:00
parent 1d7e73768a
commit 2ca6c854d8
5 changed files with 42 additions and 14 deletions

View File

@ -43,3 +43,6 @@ extern NSString * const URLComponentKeepAlive;
extern NSString * const NotificationResponseLoaded;
extern NSString * const MVMCoreNotificationGoingToServer;
#pragma mark - Image Cache
extern NSTimeInterval const ImageTimeOut;

View File

@ -29,3 +29,6 @@ NSString * const URLComponentKeepAlive = @"isAlive.jsp";
NSString * const NotificationResponseLoaded = @"responseLoaded";
NSString * const MVMCoreNotificationGoingToServer = @"MVMCoreGoServer";
#pragma mark - Image Cache
NSTimeInterval const ImageTimeOut = 1800;

View File

@ -45,6 +45,7 @@ typedef void(^FreebeeLoadFinishedHandler)(MVMCoreOperation* _Nullable freebeeOpe
- (nullable NSString*)urlForidFromConfigDict:(nonnull NSString*)urlId;
- (nullable NSData*)freebee_dataWithContentsOfURL:(NSURL *_Nullable)url;
- (nullable NSCachedURLResponse*)freebee_dataWithImageURL:(NSURL *_Nullable)imageURL;
- (void)configureFreeBeeWithDict:(nullable NSDictionary*)configDict withSessionReset:(BOOL)isReset;
@end

View File

@ -254,6 +254,31 @@ typedef NS_ENUM(NSUInteger, FreeBeeCampaignState) {
return data;
}
- (nullable NSCachedURLResponse*)freebee_dataWithImageURL:(NSURL *)imageURL {
NSData* data = nil;
NSDictionary* proxyDict = [self proxyDictionaryforUrl:imageURL];
if ([self isFreeBeeEnabled] &&
[self isValidCampaign] && proxyDict &&
![self isExpired] && [self isFreeBeeEnabledForCurrentModule]) {
MVMCoreLog(@"Free Image Data Url, %@", imageURL);
NSURLResponse* response = nil;
NSError* error = nil;
data = [self sendSynchronousRequest:imageURL returningResponse:&response error:&error];
if (error) {
data = nil;
}
MVMCoreLog(@"freebee_dataWithImageURL:Free Image Data, %lu", (unsigned long)data.length);
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data];
return cachedResponse;
}
return nil;
}
#pragma mark - FreeBee Registration
- (void)responseJSONUpdated:(nonnull NSNotification *)notification {

View File

@ -13,6 +13,7 @@
#import "MFFreebeeHandler.h"
#import "MVMCoreGetterUtility.h"
#import "MVMCoreObject.h"
#import "MVMCoreConstants.h"
@interface MVMCoreCache ()
@ -532,29 +533,24 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration];
configure.URLCache = sharedCache;
NSURLSession *session = [NSURLSession sessionWithConfiguration:configure];
NSURLRequest *request = [NSURLRequest requestWithURL:s7URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1800];
NSCachedURLResponse *response = [sharedCache cachedResponseForRequest:request];
NSURLRequest *request = [NSURLRequest requestWithURL:s7URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:ImageTimeOut];
NSCachedURLResponse *cachedResponse = [sharedCache cachedResponseForRequest:request];
if (!cachedResponse.data) {
cachedResponse = [[MFFreebeeHandler sharedHandler] freebee_dataWithImageURL:s7URL];
if (cachedResponse) {
[sharedCache storeCachedResponse:cachedResponse forRequest:request];
}
}
NSURLSessionDownloadTask *downloadImageTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSData *data = [NSData dataWithContentsOfURL:location];
NSCachedURLResponse *cachedreponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data];
[sharedCache storeCachedResponse:cachedreponse forRequest:request];
NSLog(@"HEADDERRRSS %i for %@ %@", ((NSHTTPURLResponse *)response).statusCode,s7URL.absoluteString,((NSHTTPURLResponse *)response).allHeaderFields.description);
UIImage *image = [UIImage imageWithData:data scale:[UIScreen mainScreen].scale];
completionHandler(image, nil, isFallBackImage);
}];
[downloadImageTask resume];
return;
/*
// Check in memory cache
img = [self getCachedImageWithName:s7URL.absoluteString];
if (!img) {
NSData *imageData = [[MFFreebeeHandler sharedHandler] freebee_dataWithContentsOfURL:s7URL];
img = [[UIImage alloc] initWithData:imageData scale:[UIScreen mainScreen].scale];
if (img) {
[self addImageToCache:img withName:s7URL.absoluteString];
}
}*/
}
} else {