From ccd8b3332e2987dc5511663723d9d32c684fc22a Mon Sep 17 00:00:00 2001 From: Diego Waxemberg Date: Thu, 8 Jan 2015 21:36:29 -0500 Subject: [PATCH] now request app image on multiple threads --- Limelight/Network/AppManager.h | 5 +- Limelight/Network/AppManager.m | 51 +++++++++++-------- .../ViewControllers/MainFrameViewController.m | 7 ++- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/Limelight/Network/AppManager.h b/Limelight/Network/AppManager.h index d70b103..262bdb8 100644 --- a/Limelight/Network/AppManager.h +++ b/Limelight/Network/AppManager.h @@ -9,6 +9,7 @@ #import #import "App.h" #import "HttpManager.h" +#import "Host.h" @protocol AppAssetCallback @@ -18,6 +19,8 @@ @interface AppManager : NSObject -+ (void) retrieveAppAssets:(NSArray*)apps withManager:(HttpManager*)hMan andCallback:(id)callback; +- (id) initWithHost:(Host*)host andCallback:(id)callback; +- (void) retrieveAssets:(NSArray*)appList; +- (void) stopRetrieving; @end diff --git a/Limelight/Network/AppManager.m b/Limelight/Network/AppManager.m index df0c4b2..8188821 100644 --- a/Limelight/Network/AppManager.m +++ b/Limelight/Network/AppManager.m @@ -7,39 +7,48 @@ // #import "AppManager.h" +#import "CryptoManager.h" +#import "Utils.h" @implementation AppManager { - App* _app; - HttpManager* _hMan; + NSOperationQueue* opQueue; id _callback; + Host* _host; + NSString* _uniqueId; + NSData* _cert; } -+ (void) retrieveAppAssets:(NSArray*)apps withManager:(HttpManager*)hMan andCallback:(id)callback { - for (App* app in apps) { - AppManager* manager = [[AppManager alloc] initWithApp:app httpManager:hMan andCallback:callback]; - [manager retrieveAsset]; - } - -} - -- (id) initWithApp:(App*)app httpManager:(HttpManager*)hMan andCallback:(id)callback { +- (id) initWithHost:(Host*)host andCallback:(id)callback { self = [super init]; - _app = app; - _hMan = hMan; _callback = callback; + _host = host; + opQueue = [[NSOperationQueue alloc] init]; return self; } -- (void) retrieveAsset { - NSData* appAsset = [_hMan executeRequestSynchronously:[_hMan newAppAssetRequestWithAppId:_app.appId]]; - UIImage* appImage = [UIImage imageWithData:appAsset]; - _app.appImage = appImage; - NSLog(@"App Name: %@ id:%@ image: %@", _app.appName, _app.appId, _app.appImage); - [self performSelectorOnMainThread:@selector(sendCallBack) withObject:self waitUntilDone:NO]; +- (void) retrieveAssets:(NSArray*)appList { + for (App* app in appList) { + [opQueue addOperationWithBlock:^{ + [self retrieveAssetForApp:app]; + }]; + } } -- (void) sendCallBack { - [_callback receivedAssetForApp:_app]; +- (void) stopRetrieving { + [opQueue cancelAllOperations]; +} + +- (void) retrieveAssetForApp:(App*)app { + HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.address uniqueId:_uniqueId deviceName:deviceName cert:_cert]; + NSData* appAsset = [hMan executeRequestSynchronously:[hMan newAppAssetRequestWithAppId:app.appId]]; + UIImage* appImage = [UIImage imageWithData:appAsset]; + app.appImage = appImage; + NSLog(@"App Name: %@ id:%@ image: %@", app.appName, app.appId, app.appImage); + [self performSelectorOnMainThread:@selector(sendCallBackForApp:) withObject:app waitUntilDone:NO]; +} + +- (void) sendCallBackForApp:(App*)app { + [_callback receivedAssetForApp:app]; } @end diff --git a/Limelight/ViewControllers/MainFrameViewController.m b/Limelight/ViewControllers/MainFrameViewController.m index 61de06b..e38cf81 100644 --- a/Limelight/ViewControllers/MainFrameViewController.m +++ b/Limelight/ViewControllers/MainFrameViewController.m @@ -27,6 +27,7 @@ NSData* _cert; NSString* _currentGame; DiscoveryManager* _discMan; + AppManager* _appManager; UIAlertView* _pairAlert; UIScrollView* hostScrollView; int currentPosition; @@ -77,7 +78,11 @@ static StreamConfiguration* streamConfig; dispatch_async(dispatch_get_main_queue(), ^{ [self updateApps]; }); - [AppManager retrieveAppAssets:appList withManager:hMan andCallback:self]; + if (_appManager != nil) { + [_appManager stopRetrieving]; + } + _appManager = [[AppManager alloc] initWithHost:_selectedHost andCallback:self]; + [_appManager retrieveAssets:appList]; }); }