We can't hold onto managed objects in our temporary objects because they can disappear from under us, so do the lookup when we propagate values

This commit is contained in:
Cameron Gutman
2016-01-21 11:29:17 -05:00
parent 3076e29335
commit f8e2ca8500
6 changed files with 63 additions and 38 deletions

View File

@@ -25,4 +25,7 @@
- (void) updateUniqueId:(NSString*)uniqueId;
- (NSString*) getUniqueId;
// Don't call this outside of database code
- (App*) getAppForTemporaryApp:(TemporaryApp*)tempApp;
@end

View File

@@ -61,13 +61,14 @@
- (void) updateHost:(TemporaryHost *)host {
[_managedObjectContext performBlockAndWait:^{
// Add a new persistent managed object if one doesn't exist
if (host.parent == nil) {
Host* parent = [self getHostForTemporaryHost:host];
if (parent == nil) {
NSEntityDescription* entity = [NSEntityDescription entityForName:@"Host" inManagedObjectContext:_managedObjectContext];
host.parent = [[Host alloc] initWithEntity:entity insertIntoManagedObjectContext:_managedObjectContext];
parent = [[Host alloc] initWithEntity:entity insertIntoManagedObjectContext:_managedObjectContext];
}
// Push changes from the temp host to the persistent one
[host propagateChangesToParent];
[host propagateChangesToParent:parent withDm:self];
[self saveData];
}];
@@ -98,14 +99,12 @@
}
- (void) removeHost:(TemporaryHost*)host {
if (host.parent == nil) {
// Not inserted into the DB
return;
}
[_managedObjectContext performBlockAndWait:^{
[_managedObjectContext deleteObject:host.parent];
[self saveData];
Host* managedHost = [self getHostForTemporaryHost:host];
if (managedHost != nil) {
[_managedObjectContext deleteObject:managedHost];
[self saveData];
}
}];
}
@@ -132,6 +131,33 @@
return tempHosts;
}
// Only call from within performBlockAndWait!!!
- (Host*) getHostForTemporaryHost:(TemporaryHost*)tempHost {
NSArray *hosts = [self fetchRecords:@"Host"];
for (Host* host in hosts) {
if ([tempHost.uuid isEqualToString:host.uuid]) {
return host;
}
}
return nil;
}
// Only call from within performBlockAndWait!!!
- (App*) getAppForTemporaryApp:(TemporaryApp*)tempApp {
NSArray *apps = [self fetchRecords:@"App"];
for (App* app in apps) {
if ([app.id isEqualToString:tempApp.id] &&
[app.host.uuid isEqualToString:app.host.uuid]) {
return app;
}
}
return nil;
}
- (NSArray*) fetchRecords:(NSString*)entityName {
NSArray* fetchedRecords;

View File

@@ -18,15 +18,13 @@
@property (nonatomic) BOOL isRunning;
@property (nullable, nonatomic, retain) TemporaryHost *host;
@property (nullable, nonatomic) App* parent;
NS_ASSUME_NONNULL_BEGIN
- (id) initFromApp:(App*)app withTempHost:(TemporaryHost*)tempHost;
- (NSComparisonResult)compareName:(TemporaryApp *)other;
- (void) propagateChangesToParent;
- (void) propagateChangesToParent:(App*)parent withHost:(Host*)host;
NS_ASSUME_NONNULL_END

View File

@@ -14,8 +14,6 @@
- (id) initFromApp:(App*)app withTempHost:(TemporaryHost*)tempHost {
self = [self init];
self.parent = app;
self.id = app.id;
self.image = app.image;
self.name = app.name;
@@ -24,11 +22,11 @@
return self;
}
- (void) propagateChangesToParent {
self.parent.id = self.id;
self.parent.image = self.image;
self.parent.name = self.name;
self.parent.host = self.host.parent;
- (void) propagateChangesToParent:(App*)parent withHost:(Host*)host {
parent.id = self.id;
parent.image = self.image;
parent.name = self.name;
parent.host = host;
}
- (NSComparisonResult)compareName:(TemporaryApp *)other {

View File

@@ -10,14 +10,14 @@
#import "Utils.h"
#import "Host.h"
@class DataManager;
@interface TemporaryHost : NSObject
@property (nonatomic) BOOL online;
@property (nonatomic) PairState pairState;
@property (nonatomic, nullable) NSString * activeAddress;
@property (nullable, nonatomic) Host* parent;
NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, retain) NSString *address;
@@ -32,7 +32,7 @@ NS_ASSUME_NONNULL_BEGIN
- (NSComparisonResult)compareName:(TemporaryHost *)other;
- (void) propagateChangesToParent;
- (void) propagateChangesToParent:(Host*)host withDm:(DataManager*)dm;
NS_ASSUME_NONNULL_END

View File

@@ -6,6 +6,7 @@
// Copyright © 2015 Moonlight Stream. All rights reserved.
//
#import "DataManager.h"
#import "TemporaryHost.h"
#import "Host.h"
#import "TemporaryApp.h"
@@ -16,8 +17,6 @@
- (id) initFromHost:(Host*)host {
self = [self init];
self.parent = host;
self.address = host.address;
self.externalAddress = host.externalAddress;
self.localAddress = host.localAddress;
@@ -37,28 +36,29 @@
return self;
}
- (void) propagateChangesToParent {
self.parent.address = self.address;
self.parent.externalAddress = self.externalAddress;
self.parent.localAddress = self.localAddress;
self.parent.mac = self.mac;
self.parent.name = self.name;
self.parent.uuid = self.uuid;
- (void) propagateChangesToParent:(Host*)parentHost withDm:(DataManager*)dm {
parentHost.address = self.address;
parentHost.externalAddress = self.externalAddress;
parentHost.localAddress = self.localAddress;
parentHost.mac = self.mac;
parentHost.name = self.name;
parentHost.uuid = self.uuid;
NSMutableSet *applist = [[NSMutableSet alloc] init];
for (TemporaryApp* app in self.appList) {
// Add a new persistent managed object if one doesn't exist
if (app.parent == nil) {
NSEntityDescription* entity = [NSEntityDescription entityForName:@"App" inManagedObjectContext:self.parent.managedObjectContext];
app.parent = [[App alloc] initWithEntity:entity insertIntoManagedObjectContext:self.parent.managedObjectContext];
App* parentApp = [dm getAppForTemporaryApp:app];
if (parentApp == nil) {
NSEntityDescription* entity = [NSEntityDescription entityForName:@"App" inManagedObjectContext:parentHost.managedObjectContext];
parentApp = [[App alloc] initWithEntity:entity insertIntoManagedObjectContext:parentHost.managedObjectContext];
}
[app propagateChangesToParent];
[app propagateChangesToParent:parentApp withHost:parentHost];
[applist addObject:app.parent];
[applist addObject:parentApp];
}
self.parent.appList = applist;
parentHost.appList = applist;
}
- (NSComparisonResult)compareName:(TemporaryHost *)other {