diff --git a/Limelight/Network/AppListResponse.h b/Limelight/Network/AppListResponse.h index 9769ea9..46ba193 100644 --- a/Limelight/Network/AppListResponse.h +++ b/Limelight/Network/AppListResponse.h @@ -11,7 +11,7 @@ @interface AppListResponse : NSObject - (void)populateWithData:(NSData *)data; -- (NSArray*) getAppList; +- (NSSet*) getAppList; - (BOOL) isStatusOk; @end diff --git a/Limelight/Network/AppListResponse.m b/Limelight/Network/AppListResponse.m index b4c222e..51e408d 100644 --- a/Limelight/Network/AppListResponse.m +++ b/Limelight/Network/AppListResponse.m @@ -12,7 +12,7 @@ #import @implementation AppListResponse { - NSMutableArray* _appList; + NSMutableSet* _appList; } @synthesize data, statusCode, statusMessage; @@ -23,7 +23,7 @@ static const char* TAG_APP_IS_RUNNING = "IsRunning"; - (void)populateWithData:(NSData *)xml { self.data = xml; - _appList = [[NSMutableArray alloc] init]; + _appList = [[NSMutableSet alloc] init]; [self parseData]; } @@ -108,7 +108,7 @@ static const char* TAG_APP_IS_RUNNING = "IsRunning"; xmlFreeDoc(docPtr); } -- (NSArray*) getAppList { +- (NSSet*) getAppList { return _appList; } diff --git a/Limelight/Network/DiscoveryManager.m b/Limelight/Network/DiscoveryManager.m index 45041d5..7302930 100644 --- a/Limelight/Network/DiscoveryManager.m +++ b/Limelight/Network/DiscoveryManager.m @@ -33,16 +33,10 @@ _callback = callback; shouldDiscover = NO; _hostQueue = [NSMutableArray array]; - DataManager* dataMan = [[DataManager alloc] init]; for (TemporaryHost* host in hosts) { - if (![self addHostToDiscovery:host]) - { - // Remove the duplicate host from the database - [dataMan removeHost:host]; - } + [self addHostToDiscovery:host]; } - [dataMan saveData]; [_callback updateAllHosts:_hostQueue]; _opQueue = [[NSOperationQueue alloc] init]; @@ -60,13 +54,11 @@ TemporaryHost* host = nil; if ([serverInfoResponse isStatusOk]) { - DataManager* dataMan = [[DataManager alloc] init]; - host = [dataMan createHost]; + host = [[TemporaryHost alloc] init]; host.activeAddress = host.address = hostAddress; host.online = YES; [serverInfoResponse populateHost:host]; if (![self addHostToDiscovery:host]) { - [dataMan removeHost:host]; callback(nil, @"Host already added"); } else { callback(host, nil); @@ -125,7 +117,6 @@ // Override from MDNSCallback - (void)updateHosts:(NSArray *)hosts { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - DataManager* dataMan = [[DataManager alloc] init]; // Discover the hosts before adding to eliminate duplicates for (TemporaryHost* host in hosts) { Log(LOG_I, @"Found host through MDNS: %@:", host.name); @@ -137,10 +128,8 @@ [_callback updateAllHosts:_hostQueue]; } else { Log(LOG_I, @"Not adding host to discovery: %@", host.name); - [dataMan removeHost:host]; } } - [dataMan saveData]; }); } diff --git a/Limelight/Network/MDNSManager.m b/Limelight/Network/MDNSManager.m index dfd521a..c35a197 100644 --- a/Limelight/Network/MDNSManager.m +++ b/Limelight/Network/MDNSManager.m @@ -8,7 +8,6 @@ #import "MDNSManager.h" #import "TemporaryHost.h" -#import "DataManager.h" @implementation MDNSManager { NSNetServiceBrowser* mDNSBrowser; @@ -47,10 +46,9 @@ static NSString* NV_SERVICE_TYPE = @"_nvstream._tcp"; - (NSArray*) getFoundHosts { NSMutableArray* hosts = [[NSMutableArray alloc] init]; - DataManager* dataMan = [[DataManager alloc] init]; for (NSNetService* service in services) { if (service.hostName != nil) { - TemporaryHost* host = [dataMan createHost]; + TemporaryHost* host = [[TemporaryHost alloc] init]; host.activeAddress = host.address = service.hostName; host.name = host.address; [hosts addObject:host]; diff --git a/Limelight/ViewControllers/MainFrameViewController.m b/Limelight/ViewControllers/MainFrameViewController.m index 303a1be..6deee0b 100644 --- a/Limelight/ViewControllers/MainFrameViewController.m +++ b/Limelight/ViewControllers/MainFrameViewController.m @@ -140,7 +140,7 @@ static NSMutableSet* hostList; }); } else { dispatch_async(dispatch_get_main_queue(), ^{ - [self mergeAppLists:[appListResp getAppList] forHost:host]; + [self updateApplist:[appListResp getAppList] forHost:host]; if (host != _selectedHost) { return; @@ -158,47 +158,13 @@ static NSMutableSet* hostList; }); } -- (void) mergeAppLists:(NSArray*) newList forHost:(TemporaryHost*)host { +- (void) updateApplist:(NSSet*) newList forHost:(TemporaryHost*)host { DataManager* database = [[DataManager alloc] init]; - for (TemporaryApp* app in newList) { - BOOL appAlreadyInList = NO; - for (TemporaryApp* savedApp in host.appList) { - if ([app.id isEqualToString:savedApp.id]) { - savedApp.name = app.name; - savedApp.isRunning = app.isRunning; - appAlreadyInList = YES; - break; - } - } - if (!appAlreadyInList) { - app.host = host; - [database addAppFromTemporaryApp:app]; - } - } - BOOL appWasRemoved; - do { - appWasRemoved = NO; - - for (TemporaryApp* app in host.appList) { - appWasRemoved = YES; - for (TemporaryApp* mergedApp in newList) { - if ([mergedApp.id isEqualToString:app.id]) { - appWasRemoved = NO; - break; - } - } - if (appWasRemoved) { - // Removing the app mutates the list we're iterating (which isn't legal). - // We need to jump out of this loop and restart enumeration. - [database removeAppFromHost:app]; - break; - } - } - - // Keep looping until the list is no longer being mutated - } while (appWasRemoved); + host.appList = newList; + + [database updateHost:host]; [database saveData]; } @@ -346,8 +312,6 @@ static NSMutableSet* hostList; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ [_discMan discoverHost:hostAddress withCallback:^(TemporaryHost* host, NSString* error){ if (host != nil) { - DataManager* dataMan = [[DataManager alloc] init]; - [dataMan saveData]; dispatch_async(dispatch_get_main_queue(), ^{ @synchronized(hostList) { [hostList addObject:host];