now cache app images and reuse them when connecting to the same host

This commit is contained in:
Diego Waxemberg
2015-01-08 22:18:26 -05:00
parent ccd8b3332e
commit ec39c27498
3 changed files with 42 additions and 21 deletions
+2 -2
View File
@@ -19,8 +19,8 @@
@interface AppManager : NSObject @interface AppManager : NSObject
- (id) initWithHost:(Host*)host andCallback:(id<AppAssetCallback>)callback; - (id) initWithCallback:(id<AppAssetCallback>)callback;
- (void) retrieveAssets:(NSArray*)appList; - (void) retrieveAssets:(NSArray*)appList fromHost:(Host*)host;
- (void) stopRetrieving; - (void) stopRetrieving;
@end @end
+35 -14
View File
@@ -11,39 +11,60 @@
#import "Utils.h" #import "Utils.h"
@implementation AppManager { @implementation AppManager {
NSOperationQueue* opQueue; NSOperationQueue* _opQueue;
id<AppAssetCallback> _callback; id<AppAssetCallback> _callback;
Host* _host; Host* _host;
NSString* _uniqueId; NSString* _uniqueId;
NSData* _cert; NSData* _cert;
NSMutableDictionary* _imageCache;
} }
- (id) initWithHost:(Host*)host andCallback:(id<AppAssetCallback>)callback { - (id) initWithCallback:(id<AppAssetCallback>)callback {
self = [super init]; self = [super init];
_callback = callback; _callback = callback;
_host = host; _opQueue = [[NSOperationQueue alloc] init];
opQueue = [[NSOperationQueue alloc] init]; _imageCache = [[NSMutableDictionary alloc] init];
_uniqueId = [CryptoManager getUniqueID];
return self; return self;
} }
- (void) retrieveAssets:(NSArray*)appList { - (void) retrieveAssets:(NSArray*)appList fromHost:(Host*)host {
Host* oldHost = _host;
_host = host;
BOOL useCache = [oldHost.uuid isEqualToString:_host.uuid];
NSLog(@"Using cached app images: %d", useCache);
if (!useCache) {
[_imageCache removeAllObjects];
}
for (App* app in appList) { for (App* app in appList) {
[opQueue addOperationWithBlock:^{ [_opQueue addOperationWithBlock:^{
[self retrieveAssetForApp:app]; [self retrieveAssetForApp:app useCache:useCache];
}]; }];
} }
} }
- (void) stopRetrieving { - (void) stopRetrieving {
[opQueue cancelAllOperations]; [_opQueue cancelAllOperations];
} }
- (void) retrieveAssetForApp:(App*)app { - (void) retrieveAssetForApp:(App*)app useCache:(BOOL)useCache {
HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.address uniqueId:_uniqueId deviceName:deviceName cert:_cert]; UIImage* appImage = nil;
NSData* appAsset = [hMan executeRequestSynchronously:[hMan newAppAssetRequestWithAppId:app.appId]]; if (useCache) {
UIImage* appImage = [UIImage imageWithData:appAsset]; UIImage* cachedImage = [_imageCache objectForKey:app.appId];
app.appImage = appImage; if (cachedImage != nil) {
NSLog(@"App Name: %@ id:%@ image: %@", app.appName, app.appId, app.appImage); appImage = cachedImage;
app.appImage = appImage;
}
}
if (appImage == nil) {
HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.address uniqueId:_uniqueId deviceName:deviceName cert:_cert];
NSData* appAsset = [hMan executeRequestSynchronously:[hMan newAppAssetRequestWithAppId:app.appId]];
appImage = [UIImage imageWithData:appAsset];
app.appImage = appImage;
if (appImage != nil) {
[_imageCache setObject:appImage forKey:app.appId];
}
}
[self performSelectorOnMainThread:@selector(sendCallBackForApp:) withObject:app waitUntilDone:NO]; [self performSelectorOnMainThread:@selector(sendCallBackForApp:) withObject:app waitUntilDone:NO];
} }
@@ -78,11 +78,9 @@ static StreamConfiguration* streamConfig;
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[self updateApps]; [self updateApps];
}); });
if (_appManager != nil) {
[_appManager stopRetrieving]; [_appManager stopRetrieving];
} [_appManager retrieveAssets:appList fromHost:_selectedHost];
_appManager = [[AppManager alloc] initWithHost:_selectedHost andCallback:self];
[_appManager retrieveAssets:appList];
}); });
} }
@@ -299,6 +297,8 @@ static StreamConfiguration* streamConfig;
_uniqueId = [CryptoManager getUniqueID]; _uniqueId = [CryptoManager getUniqueID];
_cert = [CryptoManager readCertFromFile]; _cert = [CryptoManager readCertFromFile];
_appManager = [[AppManager alloc] initWithCallback:self];
// Only initialize the host picker list once // Only initialize the host picker list once
if (hostList == nil) { if (hostList == nil) {
hostList = [[NSMutableSet alloc] init]; hostList = [[NSMutableSet alloc] init];