Reduce usage of DataManager

This commit is contained in:
Cameron Gutman 2015-12-01 20:56:08 -08:00
parent bfa4856bbe
commit fe616d86c5
5 changed files with 12 additions and 61 deletions

View File

@ -11,7 +11,7 @@
@interface AppListResponse : NSObject <Response> @interface AppListResponse : NSObject <Response>
- (void)populateWithData:(NSData *)data; - (void)populateWithData:(NSData *)data;
- (NSArray*) getAppList; - (NSSet*) getAppList;
- (BOOL) isStatusOk; - (BOOL) isStatusOk;
@end @end

View File

@ -12,7 +12,7 @@
#import <libxml2/libxml/xmlreader.h> #import <libxml2/libxml/xmlreader.h>
@implementation AppListResponse { @implementation AppListResponse {
NSMutableArray* _appList; NSMutableSet* _appList;
} }
@synthesize data, statusCode, statusMessage; @synthesize data, statusCode, statusMessage;
@ -23,7 +23,7 @@ static const char* TAG_APP_IS_RUNNING = "IsRunning";
- (void)populateWithData:(NSData *)xml { - (void)populateWithData:(NSData *)xml {
self.data = xml; self.data = xml;
_appList = [[NSMutableArray alloc] init]; _appList = [[NSMutableSet alloc] init];
[self parseData]; [self parseData];
} }
@ -108,7 +108,7 @@ static const char* TAG_APP_IS_RUNNING = "IsRunning";
xmlFreeDoc(docPtr); xmlFreeDoc(docPtr);
} }
- (NSArray*) getAppList { - (NSSet*) getAppList {
return _appList; return _appList;
} }

View File

@ -33,16 +33,10 @@
_callback = callback; _callback = callback;
shouldDiscover = NO; shouldDiscover = NO;
_hostQueue = [NSMutableArray array]; _hostQueue = [NSMutableArray array];
DataManager* dataMan = [[DataManager alloc] init];
for (TemporaryHost* host in hosts) for (TemporaryHost* host in hosts)
{ {
if (![self addHostToDiscovery:host]) [self addHostToDiscovery:host];
{
// Remove the duplicate host from the database
[dataMan removeHost:host];
}
} }
[dataMan saveData];
[_callback updateAllHosts:_hostQueue]; [_callback updateAllHosts:_hostQueue];
_opQueue = [[NSOperationQueue alloc] init]; _opQueue = [[NSOperationQueue alloc] init];
@ -60,13 +54,11 @@
TemporaryHost* host = nil; TemporaryHost* host = nil;
if ([serverInfoResponse isStatusOk]) { if ([serverInfoResponse isStatusOk]) {
DataManager* dataMan = [[DataManager alloc] init]; host = [[TemporaryHost alloc] init];
host = [dataMan createHost];
host.activeAddress = host.address = hostAddress; host.activeAddress = host.address = hostAddress;
host.online = YES; host.online = YES;
[serverInfoResponse populateHost:host]; [serverInfoResponse populateHost:host];
if (![self addHostToDiscovery:host]) { if (![self addHostToDiscovery:host]) {
[dataMan removeHost:host];
callback(nil, @"Host already added"); callback(nil, @"Host already added");
} else { } else {
callback(host, nil); callback(host, nil);
@ -125,7 +117,6 @@
// Override from MDNSCallback // Override from MDNSCallback
- (void)updateHosts:(NSArray *)hosts { - (void)updateHosts:(NSArray *)hosts {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
DataManager* dataMan = [[DataManager alloc] init];
// Discover the hosts before adding to eliminate duplicates // Discover the hosts before adding to eliminate duplicates
for (TemporaryHost* host in hosts) { for (TemporaryHost* host in hosts) {
Log(LOG_I, @"Found host through MDNS: %@:", host.name); Log(LOG_I, @"Found host through MDNS: %@:", host.name);
@ -137,10 +128,8 @@
[_callback updateAllHosts:_hostQueue]; [_callback updateAllHosts:_hostQueue];
} else { } else {
Log(LOG_I, @"Not adding host to discovery: %@", host.name); Log(LOG_I, @"Not adding host to discovery: %@", host.name);
[dataMan removeHost:host];
} }
} }
[dataMan saveData];
}); });
} }

View File

@ -8,7 +8,6 @@
#import "MDNSManager.h" #import "MDNSManager.h"
#import "TemporaryHost.h" #import "TemporaryHost.h"
#import "DataManager.h"
@implementation MDNSManager { @implementation MDNSManager {
NSNetServiceBrowser* mDNSBrowser; NSNetServiceBrowser* mDNSBrowser;
@ -47,10 +46,9 @@ static NSString* NV_SERVICE_TYPE = @"_nvstream._tcp";
- (NSArray*) getFoundHosts { - (NSArray*) getFoundHosts {
NSMutableArray* hosts = [[NSMutableArray alloc] init]; NSMutableArray* hosts = [[NSMutableArray alloc] init];
DataManager* dataMan = [[DataManager alloc] init];
for (NSNetService* service in services) { for (NSNetService* service in services) {
if (service.hostName != nil) { if (service.hostName != nil) {
TemporaryHost* host = [dataMan createHost]; TemporaryHost* host = [[TemporaryHost alloc] init];
host.activeAddress = host.address = service.hostName; host.activeAddress = host.address = service.hostName;
host.name = host.address; host.name = host.address;
[hosts addObject:host]; [hosts addObject:host];

View File

@ -140,7 +140,7 @@ static NSMutableSet* hostList;
}); });
} else { } else {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[self mergeAppLists:[appListResp getAppList] forHost:host]; [self updateApplist:[appListResp getAppList] forHost:host];
if (host != _selectedHost) { if (host != _selectedHost) {
return; 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]; 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; host.appList = newList;
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);
[database updateHost:host];
[database saveData]; [database saveData];
} }
@ -346,8 +312,6 @@ static NSMutableSet* hostList;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[_discMan discoverHost:hostAddress withCallback:^(TemporaryHost* host, NSString* error){ [_discMan discoverHost:hostAddress withCallback:^(TemporaryHost* host, NSString* error){
if (host != nil) { if (host != nil) {
DataManager* dataMan = [[DataManager alloc] init];
[dataMan saveData];
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
@synchronized(hostList) { @synchronized(hostList) {
[hostList addObject:host]; [hostList addObject:host];