From 2217fe07702bcfc0f0438baa118f4bc031ea403b Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Thu, 15 Mar 2018 13:48:41 -0400 Subject: [PATCH 01/17] change image cache method --- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index e0c5884..43c28c1 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -528,7 +528,19 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; // Gets the full path NSURL *s7URL = [self handleS7Path:pathOrName useWidth:useWidth widthForS7:widthForS7 useHeight:useHeight heightForS7:heightForS7 format:format finalRect:finalRect flipImage:flip]; if (s7URL) { - + NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:200*1024*1024 diskCapacity:200*1024*1024 diskPath:nil]; + NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration]; + configure.URLCache = sharedCache; + NSURLSession *session = [NSURLSession sessionWithConfiguration:configure]; + NSURLRequest *request = [NSURLRequest requestWithURL:s7URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1800]; + NSURLSessionDownloadTask *downloadImageTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { + NSLog(@"HEADDERRRSS %i for %@ %@", ((NSHTTPURLResponse *)response).statusCode,s7URL.absoluteString,((NSHTTPURLResponse *)response).allHeaderFields.description); + UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:location] scale:[UIScreen mainScreen].scale]; + completionHandler(image, nil, isFallBackImage); + }]; + [downloadImageTask resume]; + return; + /* // Check in memory cache img = [self getCachedImageWithName:s7URL.absoluteString]; @@ -538,7 +550,7 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; if (img) { [self addImageToCache:img withName:s7URL.absoluteString]; } - } + }*/ } } else { From 80d379609fa6631774634ab59cdf3f38f4a37e52 Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Tue, 10 Apr 2018 16:57:25 -0400 Subject: [PATCH 02/17] update url cache method --- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 25 ++++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index 43c28c1..6cde35a 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -528,18 +528,29 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; // Gets the full path NSURL *s7URL = [self handleS7Path:pathOrName useWidth:useWidth widthForS7:widthForS7 useHeight:useHeight heightForS7:heightForS7 format:format finalRect:finalRect flipImage:flip]; if (s7URL) { - NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:200*1024*1024 diskCapacity:200*1024*1024 diskPath:nil]; + NSURLCache *sharedCache = [NSURLCache sharedURLCache]; + [sharedCache setMemoryCapacity:200*1024*1024]; NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration]; configure.URLCache = sharedCache; NSURLSession *session = [NSURLSession sessionWithConfiguration:configure]; NSURLRequest *request = [NSURLRequest requestWithURL:s7URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1800]; - NSURLSessionDownloadTask *downloadImageTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { - NSLog(@"HEADDERRRSS %i for %@ %@", ((NSHTTPURLResponse *)response).statusCode,s7URL.absoluteString,((NSHTTPURLResponse *)response).allHeaderFields.description); - UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:location] scale:[UIScreen mainScreen].scale]; + NSCachedURLResponse *response = [sharedCache cachedResponseForRequest:request]; + if (response != nil) { + UIImage *image = [UIImage imageWithData:response.data scale:[UIScreen mainScreen].scale]; completionHandler(image, nil, isFallBackImage); - }]; - [downloadImageTask resume]; - return; + return; + } else { + 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]; From b319de5d1613d7187bd191e712e8572b6f3e56ca Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Wed, 11 Apr 2018 12:31:23 -0400 Subject: [PATCH 03/17] remove cachedResponseForRequest method --- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 24 ++++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index 6cde35a..c54398e 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -535,22 +535,16 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; NSURLSession *session = [NSURLSession sessionWithConfiguration:configure]; NSURLRequest *request = [NSURLRequest requestWithURL:s7URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1800]; NSCachedURLResponse *response = [sharedCache cachedResponseForRequest:request]; - if (response != nil) { - UIImage *image = [UIImage imageWithData:response.data scale:[UIScreen mainScreen].scale]; + 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); - return; - } else { - 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; - } + }]; + [downloadImageTask resume]; + return; /* // Check in memory cache img = [self getCachedImageWithName:s7URL.absoluteString]; From 1d7e73768a155f62a9f42a049208da39ff92f731 Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Fri, 20 Apr 2018 13:06:30 -0400 Subject: [PATCH 04/17] move memory capacity into mvm rc --- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 1 - 1 file changed, 1 deletion(-) diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index c54398e..426a196 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -529,7 +529,6 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; NSURL *s7URL = [self handleS7Path:pathOrName useWidth:useWidth widthForS7:widthForS7 useHeight:useHeight heightForS7:heightForS7 format:format finalRect:finalRect flipImage:flip]; if (s7URL) { NSURLCache *sharedCache = [NSURLCache sharedURLCache]; - [sharedCache setMemoryCapacity:200*1024*1024]; NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration]; configure.URLCache = sharedCache; NSURLSession *session = [NSURLSession sessionWithConfiguration:configure]; From 2ca6c854d8bce9ba8a27cdb5c95b7560bc37ac78 Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Fri, 20 Apr 2018 13:47:21 -0400 Subject: [PATCH 05/17] update image cache method --- MVMCore/MVMCore/Constants/MVMCoreConstants.h | 3 +++ MVMCore/MVMCore/Constants/MVMCoreConstants.m | 3 +++ .../LoadHandling/FreeBee/MFFreebeeHandler.h | 1 + .../LoadHandling/FreeBee/MFFreebeeHandler.m | 25 +++++++++++++++++++ MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 24 ++++++++---------- 5 files changed, 42 insertions(+), 14 deletions(-) diff --git a/MVMCore/MVMCore/Constants/MVMCoreConstants.h b/MVMCore/MVMCore/Constants/MVMCoreConstants.h index 9774912..cf74414 100644 --- a/MVMCore/MVMCore/Constants/MVMCoreConstants.h +++ b/MVMCore/MVMCore/Constants/MVMCoreConstants.h @@ -43,3 +43,6 @@ extern NSString * const URLComponentKeepAlive; extern NSString * const NotificationResponseLoaded; extern NSString * const MVMCoreNotificationGoingToServer; + +#pragma mark - Image Cache +extern NSTimeInterval const ImageTimeOut; diff --git a/MVMCore/MVMCore/Constants/MVMCoreConstants.m b/MVMCore/MVMCore/Constants/MVMCoreConstants.m index b0b9d17..76fbe3e 100644 --- a/MVMCore/MVMCore/Constants/MVMCoreConstants.m +++ b/MVMCore/MVMCore/Constants/MVMCoreConstants.m @@ -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; diff --git a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h index dc0daee..9ab7393 100644 --- a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h +++ b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h @@ -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 diff --git a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m index 2a8d2cb..698043b 100644 --- a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m +++ b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m @@ -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 { diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index 426a196..8fee473 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -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 { From 6700d97b81fc95c8dea61e64dd977affef4dad02 Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Fri, 20 Apr 2018 14:59:56 -0400 Subject: [PATCH 06/17] check fall back image for s7 image --- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 43 ++++++++++++-------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index 8fee473..3e84a33 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -519,7 +519,6 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; - (void)getImage:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 format:(nullable NSString *)format finalRect:(CGRect)finalRect flipImage:(BOOL)flip localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - BOOL isFallBackImage = NO; UIImage *img = nil; if (pathOrName.length > 0) { @@ -539,15 +538,22 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; if (!cachedResponse.data) { cachedResponse = [[MFFreebeeHandler sharedHandler] freebee_dataWithImageURL:s7URL]; if (cachedResponse) { + //system stores cache based datatask and check cache based on response, + //need to manually store cache for response [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]; + //system stores cache based datatask and check cache based on response, + //need to manually store cache for response [sharedCache storeCachedResponse:cachedreponse forRequest:request]; UIImage *image = [UIImage imageWithData:data scale:[UIScreen mainScreen].scale]; - completionHandler(image, nil, isFallBackImage); + + //check fallback image + [self checkImage:image fallbackImage:fallbackImageName completionHandler:completionHandler]; + }]; [downloadImageTask resume]; return; @@ -566,24 +572,29 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; } } } + [self checkImage:img fallbackImage:fallbackImageName completionHandler:completionHandler]; - // Set to the fallback image. - if (!img && fallbackImageName) { - isFallBackImage = YES; - // Grab fallback from cache - img = [self getCachedImageWithName:fallbackImageName]; - if (!img) { + }); +} + +- (void)checkImage:(UIImage *)img fallbackImage:(NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler{ + // Set to the fallback image. + BOOL isFallBackImage = NO; + if (!img && fallbackImageName) { + isFallBackImage = YES; + // Grab fallback from cache + img = [self getCachedImageWithName:fallbackImageName]; + if (!img) { + + img = [UIImage imageNamed:fallbackImageName inBundle:[self bundleToUseForImages] compatibleWithTraitCollection:nil]; + if (img) { - img = [UIImage imageNamed:fallbackImageName inBundle:[self bundleToUseForImages] compatibleWithTraitCollection:nil]; - if (img) { - - // Cache the fallback image if not already cached. - [self addImageToCache:img withName:fallbackImageName]; - } + // Cache the fallback image if not already cached. + [self addImageToCache:img withName:fallbackImageName]; } } - completionHandler(img, nil, isFallBackImage); - }); + } + completionHandler(img, nil, isFallBackImage); } - (nullable UIImage *)getCachedImageWithName:(nonnull NSString *)imageName { From 7904d4e8fe16ec3315834ccc2a6cac55cc066276 Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Fri, 20 Apr 2018 15:25:33 -0400 Subject: [PATCH 07/17] add cache method to gif image --- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 112 +++++++------------ 1 file changed, 41 insertions(+), 71 deletions(-) diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index 3e84a33..28e362e 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -457,31 +457,17 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; - (void)getGif:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 format:(nullable NSString *)format localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - BOOL isFallBackImage = NO; NSData *imageData = nil; if (pathOrName.length > 0) { - - if([pathOrName containsString:@"http"]) { - + if(pathOrName.length >=4 && [[pathOrName substringToIndex:4] isEqualToString:@"http"]) { // Gets the full path NSURL *s7URL = [self handleS7Path:pathOrName useWidth:useWidth widthForS7:widthForS7 useHeight:useHeight heightForS7:heightForS7 format:format finalRect:CGRectNull flipImage:NO]; if (s7URL) { - - // Check in memory cache - imageData = [self getCachedDataWithName:s7URL.absoluteString]; - - if (!imageData) { - imageData = [[MFFreebeeHandler sharedHandler] freebee_dataWithContentsOfURL:s7URL]; - if (imageData) { - [self addDataToCache:imageData withName:s7URL.absoluteString]; - } - } + [self downloadImage:s7URL isGif:YES fallbackImageName:fallbackImageName completionHandler:completionHandler]; } } else { - // Try Local. Check in memory cache imageData = [self getCachedDataWithName:pathOrName]; - if (!imageData) { imageData = [NSData dataWithContentsOfFile:[[self bundleToUseForImages] pathForResource:pathOrName ofType:@"gif"]]; if (imageData) { @@ -490,25 +476,7 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; } } } - - // Set to the fallback image. - UIImage *fallbackImage = nil; - if (!imageData && fallbackImageName) { - isFallBackImage = YES; - // Grab fallback from cache - fallbackImage = [self getCachedImageWithName:fallbackImageName]; - if (!fallbackImage) { - - fallbackImage = [UIImage imageNamed:fallbackImageName inBundle:[self bundleToUseForImages] compatibleWithTraitCollection:nil]; - if (fallbackImage) { - - // Cache the fallback image if not already cached. - [self addImageToCache:fallbackImage withName:fallbackImageName]; - } - } - } - - completionHandler(fallbackImage, imageData, isFallBackImage); + [self checkImage:nil imageData:imageData fallbackImage:fallbackImageName completionHandler:completionHandler]; }); } @@ -521,48 +489,17 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ UIImage *img = nil; if (pathOrName.length > 0) { - // try the url if it's a url. if (pathOrName.length >=4 && [[pathOrName substringToIndex:4] isEqualToString:@"http"]) { - // Gets the full path NSURL *s7URL = [self handleS7Path:pathOrName useWidth:useWidth widthForS7:widthForS7 useHeight:useHeight heightForS7:heightForS7 format:format finalRect:finalRect flipImage:flip]; if (s7URL) { - NSURLCache *sharedCache = [NSURLCache sharedURLCache]; - NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration]; - configure.URLCache = sharedCache; - NSURLSession *session = [NSURLSession sessionWithConfiguration:configure]; - 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) { - //system stores cache based datatask and check cache based on response, - //need to manually store cache for response - [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]; - //system stores cache based datatask and check cache based on response, - //need to manually store cache for response - [sharedCache storeCachedResponse:cachedreponse forRequest:request]; - UIImage *image = [UIImage imageWithData:data scale:[UIScreen mainScreen].scale]; - - //check fallback image - [self checkImage:image fallbackImage:fallbackImageName completionHandler:completionHandler]; - - }]; - [downloadImageTask resume]; + [self downloadImage:s7URL isGif:NO fallbackImageName:fallbackImageName completionHandler:completionHandler]; return; } } else { - // Try native image. Check in memory cache img = [self getCachedImageWithName:pathOrName]; - if (!img) { img = [UIImage imageNamed:pathOrName inBundle:[self bundleToUseForImages] compatibleWithTraitCollection:nil]; @@ -572,15 +509,48 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; } } } - [self checkImage:img fallbackImage:fallbackImageName completionHandler:completionHandler]; - + [self checkImage:img imageData:nil fallbackImage:fallbackImageName completionHandler:completionHandler]; }); } -- (void)checkImage:(UIImage *)img fallbackImage:(NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler{ +- (void)downloadImage:(NSURL *)s7URL isGif:(BOOL)isGif fallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler{ + NSURLCache *sharedCache = [NSURLCache sharedURLCache]; + NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration]; + configure.URLCache = sharedCache; + NSURLSession *session = [NSURLSession sessionWithConfiguration:configure]; + 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) { + //system stores cache based datatask and check cache based on response, + //need to manually store cache for response + [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]; + //system stores cache based datatask and check cache based on response, + //need to manually store cache for response + [sharedCache storeCachedResponse:cachedreponse forRequest:request]; + if (isGif) { + [self checkImage:nil imageData:data fallbackImage:fallbackImageName completionHandler:completionHandler]; + } else { + UIImage *image = [UIImage imageWithData:data scale:[UIScreen mainScreen].scale]; + //check fallback image + [self checkImage:image imageData:nil fallbackImage:fallbackImageName completionHandler:completionHandler]; + } + + }]; + [downloadImageTask resume]; +} + +- (void)checkImage:(UIImage *)img imageData:(NSData *)imageData fallbackImage:(NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler{ // Set to the fallback image. BOOL isFallBackImage = NO; - if (!img && fallbackImageName) { + if (!img && !imageData && fallbackImageName) { isFallBackImage = YES; // Grab fallback from cache img = [self getCachedImageWithName:fallbackImageName]; From 9d101abbf84b1f388d5762c1db5bb3b04ebb500c Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Tue, 24 Apr 2018 12:05:11 -0400 Subject: [PATCH 08/17] fix typo --- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index 28e362e..d2c298c 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -564,7 +564,7 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; } } } - completionHandler(img, nil, isFallBackImage); + completionHandler(img, imageData, isFallBackImage); } - (nullable UIImage *)getCachedImageWithName:(nonnull NSString *)imageName { From 374ae5973a127bf0f4bd0ae357f2b116083990e2 Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Tue, 1 May 2018 15:47:19 -0400 Subject: [PATCH 09/17] fix typo add comments --- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index d2c298c..45e9603 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -513,13 +513,14 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; }); } +//download image - (void)downloadImage:(NSURL *)s7URL isGif:(BOOL)isGif fallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler{ NSURLCache *sharedCache = [NSURLCache sharedURLCache]; NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration]; configure.URLCache = sharedCache; NSURLSession *session = [NSURLSession sessionWithConfiguration:configure]; NSURLRequest *request = [NSURLRequest requestWithURL:s7URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:ImageTimeOut]; - + //use Freebee to download image first, then store image to cache NSCachedURLResponse *cachedResponse = [sharedCache cachedResponseForRequest:request]; if (!cachedResponse.data) { cachedResponse = [[MFFreebeeHandler sharedHandler] freebee_dataWithImageURL:s7URL]; @@ -529,6 +530,7 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; [sharedCache storeCachedResponse:cachedResponse forRequest:request]; } } + //call nsurlsession again, if image is cached by Freebee, the second session will grab image data from cache instead of making another server call 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]; @@ -547,6 +549,7 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; [downloadImageTask resume]; } +//check whehter get image or imageData, if not, return fallbackImage - (void)checkImage:(UIImage *)img imageData:(NSData *)imageData fallbackImage:(NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler{ // Set to the fallback image. BOOL isFallBackImage = NO; From 1e7fb38f17bdae9e1c39925af80a489ce1c3e002 Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Thu, 10 May 2018 11:32:08 -0400 Subject: [PATCH 10/17] update freebee method --- .../LoadHandling/FreeBee/MFFreebeeHandler.h | 3 +- .../LoadHandling/FreeBee/MFFreebeeHandler.m | 46 +++++-------------- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 12 ++--- 3 files changed, 19 insertions(+), 42 deletions(-) diff --git a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h index 9ab7393..fb148d5 100644 --- a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h +++ b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h @@ -44,8 +44,7 @@ typedef void(^FreebeeLoadFinishedHandler)(MVMCoreOperation* _Nullable freebeeOpe - (BOOL)isFreeBeeAuthorizedValidUrl:(nullable NSURL*)url; - (nullable NSString*)urlForidFromConfigDict:(nonnull NSString*)urlId; -- (nullable NSData*)freebee_dataWithContentsOfURL:(NSURL *_Nullable)url; -- (nullable NSCachedURLResponse*)freebee_dataWithImageURL:(NSURL *_Nullable)imageURL; +- (nullable NSCachedURLResponse*)freebee_dataWithContentsOfURL:(NSURL *_Nullable)url; - (void)configureFreeBeeWithDict:(nullable NSDictionary*)configDict withSessionReset:(BOOL)isReset; @end diff --git a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m index 698043b..e52684e 100644 --- a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m +++ b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m @@ -230,53 +230,31 @@ typedef NS_ENUM(NSUInteger, FreeBeeCampaignState) { } #pragma mark FreeBee Helper for NSData -- (nullable NSData*)freebee_dataWithContentsOfURL:(NSURL *)url { +- (nullable NSCachedURLResponse*)freebee_dataWithContentsOfURL:(NSURL *)url { NSData* data = nil; NSDictionary* proxyDict = [self proxyDictionaryforUrl:url]; + NSURLResponse* response = nil; + NSError* error = nil; if ([self isFreeBeeEnabled] && [self isValidCampaign] && proxyDict && ![self isExpired] && [self isFreeBeeEnabledForCurrentModule]) { - MVMCoreLog(@"Free Data Url, %@", url); - NSURLResponse* response = nil; - NSError* error = nil; - data = [self sendSynchronousRequest:url returningResponse:&response error:&error]; - - if (error) - data = nil; - - MVMCoreLog(@"freebee_dataWithContentsOfURL:Free Data, %lu", (unsigned long)data.length); - } else { - data = [NSData dataWithContentsOfURL:url]; - } - 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]; - + MVMCoreLog(@"Free Data Url, %@", url); + data = [self sendSynchronousRequest:url returningResponse:&response error:&error]; if (error) { data = nil; } + MVMCoreLog(@"freebee_dataWithContentsOfURL:Free Data, %lu", (unsigned long)data.length); - MVMCoreLog(@"freebee_dataWithImageURL:Free Image Data, %lu", (unsigned long)data.length); - NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data]; - return cachedResponse; + } else { + //create response for identify + response = [[NSURLResponse alloc] initWithURL:url MIMEType:nil expectedContentLength:100 textEncodingName:nil]; + data = [NSData dataWithContentsOfURL:url]; } - return nil; + NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data]; + return cachedResponse; } #pragma mark - FreeBee Registration diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index 45e9603..aea1f46 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -459,11 +459,12 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *imageData = nil; if (pathOrName.length > 0) { - if(pathOrName.length >=4 && [[pathOrName substringToIndex:4] isEqualToString:@"http"]) { + if (pathOrName.length >=4 && [[pathOrName substringToIndex:4] isEqualToString:@"http"]) { // Gets the full path NSURL *s7URL = [self handleS7Path:pathOrName useWidth:useWidth widthForS7:widthForS7 useHeight:useHeight heightForS7:heightForS7 format:format finalRect:CGRectNull flipImage:NO]; if (s7URL) { [self downloadImage:s7URL isGif:YES fallbackImageName:fallbackImageName completionHandler:completionHandler]; + return; } } else { // Try Local. Check in memory cache @@ -514,16 +515,15 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; } //download image -- (void)downloadImage:(NSURL *)s7URL isGif:(BOOL)isGif fallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler{ +- (void)downloadImage:(NSURL *)s7URL isGif:(BOOL)isGif fallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler { + NSURLCache *sharedCache = [NSURLCache sharedURLCache]; - NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration]; - configure.URLCache = sharedCache; - NSURLSession *session = [NSURLSession sessionWithConfiguration:configure]; + NSURLSession *session = [MVMCoreSessionObject sharedGlobal].session; NSURLRequest *request = [NSURLRequest requestWithURL:s7URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:ImageTimeOut]; //use Freebee to download image first, then store image to cache NSCachedURLResponse *cachedResponse = [sharedCache cachedResponseForRequest:request]; if (!cachedResponse.data) { - cachedResponse = [[MFFreebeeHandler sharedHandler] freebee_dataWithImageURL:s7URL]; + cachedResponse = [[MFFreebeeHandler sharedHandler] freebee_dataWithContentsOfURL:s7URL]; if (cachedResponse) { //system stores cache based datatask and check cache based on response, //need to manually store cache for response From ebb5303711c8899b78e5dea8d5570f4823f84988 Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Thu, 10 May 2018 11:39:09 -0400 Subject: [PATCH 11/17] fix typo --- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index aea1f46..68f2f00 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -533,10 +533,10 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; //call nsurlsession again, if image is cached by Freebee, the second session will grab image data from cache instead of making another server call 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]; + NSCachedURLResponse *cachedReponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data]; //system stores cache based datatask and check cache based on response, //need to manually store cache for response - [sharedCache storeCachedResponse:cachedreponse forRequest:request]; + [sharedCache storeCachedResponse:cachedReponse forRequest:request]; if (isGif) { [self checkImage:nil imageData:data fallbackImage:fallbackImageName completionHandler:completionHandler]; } else { @@ -544,12 +544,11 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; //check fallback image [self checkImage:image imageData:nil fallbackImage:fallbackImageName completionHandler:completionHandler]; } - }]; [downloadImageTask resume]; } -//check whehter get image or imageData, if not, return fallbackImage +//check whether get image or imageData, if not, return fallbackImage - (void)checkImage:(UIImage *)img imageData:(NSData *)imageData fallbackImage:(NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler{ // Set to the fallback image. BOOL isFallBackImage = NO; From 995b3119ba42613483b685bb6b82471a82a038eb Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Thu, 10 May 2018 11:46:48 -0400 Subject: [PATCH 12/17] revert freebee changes --- .../LoadHandling/FreeBee/MFFreebeeHandler.h | 3 +- .../LoadHandling/FreeBee/MFFreebeeHandler.m | 44 ++++++++++++++----- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h index fb148d5..9ab7393 100644 --- a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h +++ b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h @@ -44,7 +44,8 @@ typedef void(^FreebeeLoadFinishedHandler)(MVMCoreOperation* _Nullable freebeeOpe - (BOOL)isFreeBeeAuthorizedValidUrl:(nullable NSURL*)url; - (nullable NSString*)urlForidFromConfigDict:(nonnull NSString*)urlId; -- (nullable NSCachedURLResponse*)freebee_dataWithContentsOfURL:(NSURL *_Nullable)url; +- (nullable NSData*)freebee_dataWithContentsOfURL:(NSURL *_Nullable)url; +- (nullable NSCachedURLResponse*)freebee_dataWithImageURL:(NSURL *_Nullable)imageURL; - (void)configureFreeBeeWithDict:(nullable NSDictionary*)configDict withSessionReset:(BOOL)isReset; @end diff --git a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m index e52684e..0b6c153 100644 --- a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m +++ b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m @@ -230,31 +230,53 @@ typedef NS_ENUM(NSUInteger, FreeBeeCampaignState) { } #pragma mark FreeBee Helper for NSData -- (nullable NSCachedURLResponse*)freebee_dataWithContentsOfURL:(NSURL *)url { +- (nullable NSData*)freebee_dataWithContentsOfURL:(NSURL *)url { NSData* data = nil; NSDictionary* proxyDict = [self proxyDictionaryforUrl:url]; - NSURLResponse* response = nil; - NSError* error = nil; if ([self isFreeBeeEnabled] && [self isValidCampaign] && proxyDict && ![self isExpired] && [self isFreeBeeEnabledForCurrentModule]) { - + MVMCoreLog(@"Free Data Url, %@", url); + NSURLResponse* response = nil; + NSError* error = nil; data = [self sendSynchronousRequest:url returningResponse:&response error:&error]; + + if (error) + data = nil; + + MVMCoreLog(@"freebee_dataWithContentsOfURL:Free Data, %lu", (unsigned long)data.length); + } else { + data = [NSData dataWithContentsOfURL:url]; + } + 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_dataWithContentsOfURL:Free Data, %lu", (unsigned long)data.length); - } else { - //create response for identify - response = [[NSURLResponse alloc] initWithURL:url MIMEType:nil expectedContentLength:100 textEncodingName:nil]; - data = [NSData dataWithContentsOfURL:url]; + MVMCoreLog(@"freebee_dataWithImageURL:Free Image Data, %lu", (unsigned long)data.length); + NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data]; + return cachedResponse; } - NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data]; - return cachedResponse; + return nil; } #pragma mark - FreeBee Registration From 5e8d96a96ce86aa654f3a0ac82be013499472100 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Thu, 10 May 2018 11:57:39 -0400 Subject: [PATCH 13/17] Consolidation --- .../LoadHandling/FreeBee/MFFreebeeHandler.h | 1 + .../LoadHandling/FreeBee/MFFreebeeHandler.m | 52 ++++++++----------- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h index 9ab7393..a737d45 100644 --- a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h +++ b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h @@ -44,6 +44,7 @@ typedef void(^FreebeeLoadFinishedHandler)(MVMCoreOperation* _Nullable freebeeOpe - (BOOL)isFreeBeeAuthorizedValidUrl:(nullable NSURL*)url; - (nullable NSString*)urlForidFromConfigDict:(nonnull NSString*)urlId; +// Tries to get the data using freebee. If freebee is not enabled, gets the data without freebee. - (nullable NSData*)freebee_dataWithContentsOfURL:(NSURL *_Nullable)url; - (nullable NSCachedURLResponse*)freebee_dataWithImageURL:(NSURL *_Nullable)imageURL; diff --git a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m index 698043b..ef3a0fa 100644 --- a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m +++ b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m @@ -230,25 +230,29 @@ typedef NS_ENUM(NSUInteger, FreeBeeCampaignState) { } #pragma mark FreeBee Helper for NSData -- (nullable NSData*)freebee_dataWithContentsOfURL:(NSURL *)url { - - NSData* data = nil; - NSDictionary* proxyDict = [self proxyDictionaryforUrl:url]; + +- (nullable NSData *)freebeeDataWithURL:(NSURL *)url response:(NSURLResponse **)response { + NSData *data = nil; + NSDictionary *proxyDict = [self proxyDictionaryforUrl:url]; if ([self isFreeBeeEnabled] && [self isValidCampaign] && proxyDict && ![self isExpired] && [self isFreeBeeEnabledForCurrentModule]) { + + MVMCoreLog(@"Free Data Url, %@", url); + NSError *error = nil; + data = [self sendSynchronousRequest:url returningResponse:response error:&error]; + if (error) { + data = nil; + } + MVMCoreLog(@"freebee_dataWithContentsOfURL:Free Data, %lu", (unsigned long)data.length); + } + return data; +} - MVMCoreLog(@"Free Data Url, %@", url); - NSURLResponse* response = nil; - NSError* error = nil; - data = [self sendSynchronousRequest:url returningResponse:&response error:&error]; - - if (error) - data = nil; - - MVMCoreLog(@"freebee_dataWithContentsOfURL:Free Data, %lu", (unsigned long)data.length); - } else { +- (nullable NSData*)freebee_dataWithContentsOfURL:(NSURL *)url { + NSData *data = [self freebeeDataWithURL:url response:nil]; + if (!data) { data = [NSData dataWithContentsOfURL:url]; } return data; @@ -256,23 +260,9 @@ typedef NS_ENUM(NSUInteger, FreeBeeCampaignState) { - (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); + NSURLResponse *response = nil; + NSData *data = [self freebeeDataWithURL:imageURL response:&response]; + if (data) { NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data]; return cachedResponse; } From 6edf0ab99c5c61bee810ddaca6cd83ed26978621 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Thu, 10 May 2018 12:50:46 -0400 Subject: [PATCH 14/17] variable name change. cache tied from session --- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 37 ++++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index 68f2f00..da02bb1 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -488,7 +488,7 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; - (void)getImage:(nonnull NSString *)pathOrName useWidth:(BOOL)useWidth widthForS7:(NSInteger)widthForS7 useHeight:(BOOL)useHeight heightForS7:(NSInteger)heightForS7 format:(nullable NSString *)format finalRect:(CGRect)finalRect flipImage:(BOOL)flip localFallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - UIImage *img = nil; + UIImage *image = nil; if (pathOrName.length > 0) { // try the url if it's a url. if (pathOrName.length >=4 && [[pathOrName substringToIndex:4] isEqualToString:@"http"]) { @@ -500,27 +500,27 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; } } else { // Try native image. Check in memory cache - img = [self getCachedImageWithName:pathOrName]; - if (!img) { - img = [UIImage imageNamed:pathOrName inBundle:[self bundleToUseForImages] compatibleWithTraitCollection:nil]; - - if (img) { - [self addImageToCache:img withName:pathOrName]; + image = [self getCachedImageWithName:pathOrName]; + if (!image) { + image = [UIImage imageNamed:pathOrName inBundle:[self bundleToUseForImages] compatibleWithTraitCollection:nil]; + if (image) { + [self addImageToCache:image withName:pathOrName]; } } } } - [self checkImage:img imageData:nil fallbackImage:fallbackImageName completionHandler:completionHandler]; + [self checkImage:image imageData:nil fallbackImage:fallbackImageName completionHandler:completionHandler]; }); } //download image - (void)downloadImage:(NSURL *)s7URL isGif:(BOOL)isGif fallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler { - NSURLCache *sharedCache = [NSURLCache sharedURLCache]; NSURLSession *session = [MVMCoreSessionObject sharedGlobal].session; + NSURLCache *sharedCache = session.configuration.URLCache; NSURLRequest *request = [NSURLRequest requestWithURL:s7URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:ImageTimeOut]; - //use Freebee to download image first, then store image to cache + + //use Freebee to download image first if it is not already in the cache, then store image to cache NSCachedURLResponse *cachedResponse = [sharedCache cachedResponseForRequest:request]; if (!cachedResponse.data) { cachedResponse = [[MFFreebeeHandler sharedHandler] freebee_dataWithContentsOfURL:s7URL]; @@ -541,7 +541,6 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; [self checkImage:nil imageData:data fallbackImage:fallbackImageName completionHandler:completionHandler]; } else { UIImage *image = [UIImage imageWithData:data scale:[UIScreen mainScreen].scale]; - //check fallback image [self checkImage:image imageData:nil fallbackImage:fallbackImageName completionHandler:completionHandler]; } }]; @@ -549,24 +548,24 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; } //check whether get image or imageData, if not, return fallbackImage -- (void)checkImage:(UIImage *)img imageData:(NSData *)imageData fallbackImage:(NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler{ +- (void)checkImage:(UIImage *)image imageData:(NSData *)imageData fallbackImage:(NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler { // Set to the fallback image. BOOL isFallBackImage = NO; - if (!img && !imageData && fallbackImageName) { + if (!image && !imageData && fallbackImageName) { isFallBackImage = YES; // Grab fallback from cache - img = [self getCachedImageWithName:fallbackImageName]; - if (!img) { + image = [self getCachedImageWithName:fallbackImageName]; + if (!image) { - img = [UIImage imageNamed:fallbackImageName inBundle:[self bundleToUseForImages] compatibleWithTraitCollection:nil]; - if (img) { + image = [UIImage imageNamed:fallbackImageName inBundle:[self bundleToUseForImages] compatibleWithTraitCollection:nil]; + if (image) { // Cache the fallback image if not already cached. - [self addImageToCache:img withName:fallbackImageName]; + [self addImageToCache:image withName:fallbackImageName]; } } } - completionHandler(img, imageData, isFallBackImage); + completionHandler(image, imageData, isFallBackImage); } - (nullable UIImage *)getCachedImageWithName:(nonnull NSString *)imageName { From 937daba018d2e1f1da5d57d2c0a0d213b117ca2a Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Thu, 10 May 2018 12:52:02 -0400 Subject: [PATCH 15/17] image function use --- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index da02bb1..de4a78e 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -523,7 +523,7 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; //use Freebee to download image first if it is not already in the cache, then store image to cache NSCachedURLResponse *cachedResponse = [sharedCache cachedResponseForRequest:request]; if (!cachedResponse.data) { - cachedResponse = [[MFFreebeeHandler sharedHandler] freebee_dataWithContentsOfURL:s7URL]; + cachedResponse = [[MFFreebeeHandler sharedHandler] freebee_dataWithImageURL:s7URL]; if (cachedResponse) { //system stores cache based datatask and check cache based on response, //need to manually store cache for response From 38c7687acd8d17c68b888cbeec05f64063207074 Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Thu, 10 May 2018 14:32:52 -0400 Subject: [PATCH 16/17] change timeout to 60 --- MVMCore/MVMCore/Constants/MVMCoreConstants.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MVMCore/MVMCore/Constants/MVMCoreConstants.m b/MVMCore/MVMCore/Constants/MVMCoreConstants.m index 76fbe3e..c579b3b 100644 --- a/MVMCore/MVMCore/Constants/MVMCoreConstants.m +++ b/MVMCore/MVMCore/Constants/MVMCoreConstants.m @@ -31,4 +31,4 @@ NSString * const NotificationResponseLoaded = @"responseLoaded"; NSString * const MVMCoreNotificationGoingToServer = @"MVMCoreGoServer"; #pragma mark - Image Cache -NSTimeInterval const ImageTimeOut = 1800; +NSTimeInterval const ImageTimeOut = 60; From eb25f78b26cfc24f65071416509303f788249252 Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Thu, 10 May 2018 15:42:09 -0400 Subject: [PATCH 17/17] add data and response valid for image download --- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index de4a78e..bb97f89 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -533,10 +533,13 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; //call nsurlsession again, if image is cached by Freebee, the second session will grab image data from cache instead of making another server call 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]; - //system stores cache based datatask and check cache based on response, - //need to manually store cache for response - [sharedCache storeCachedResponse:cachedReponse forRequest:request]; + //check data and response first, since they are nullable + if (data && response) { + NSCachedURLResponse *cachedReponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data]; + //system stores cache based datatask and check cache based on response, + //need to manually store cache for response + [sharedCache storeCachedResponse:cachedReponse forRequest:request]; + } if (isGif) { [self checkImage:nil imageData:data fallbackImage:fallbackImageName completionHandler:completionHandler]; } else {