now request app image on multiple threads

This commit is contained in:
Diego Waxemberg
2015-01-08 21:36:29 -05:00
parent 7cf521272a
commit ccd8b3332e
3 changed files with 40 additions and 23 deletions

View File

@@ -9,6 +9,7 @@
#import <Foundation/Foundation.h>
#import "App.h"
#import "HttpManager.h"
#import "Host.h"
@protocol AppAssetCallback <NSObject>
@@ -18,6 +19,8 @@
@interface AppManager : NSObject
+ (void) retrieveAppAssets:(NSArray*)apps withManager:(HttpManager*)hMan andCallback:(id<AppAssetCallback>)callback;
- (id) initWithHost:(Host*)host andCallback:(id<AppAssetCallback>)callback;
- (void) retrieveAssets:(NSArray*)appList;
- (void) stopRetrieving;
@end

View File

@@ -7,39 +7,48 @@
//
#import "AppManager.h"
#import "CryptoManager.h"
#import "Utils.h"
@implementation AppManager {
App* _app;
HttpManager* _hMan;
NSOperationQueue* opQueue;
id<AppAssetCallback> _callback;
Host* _host;
NSString* _uniqueId;
NSData* _cert;
}
+ (void) retrieveAppAssets:(NSArray*)apps withManager:(HttpManager*)hMan andCallback:(id<AppAssetCallback>)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<AppAssetCallback>)callback {
- (id) initWithHost:(Host*)host andCallback:(id<AppAssetCallback>)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

View File

@@ -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];
});
}