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>
- (void)populateWithData:(NSData *)data;
- (NSArray*) getAppList;
- (NSSet*) getAppList;
- (BOOL) isStatusOk;
@end

View File

@ -12,7 +12,7 @@
#import <libxml2/libxml/xmlreader.h>
@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;
}

View File

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

View File

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

View File

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