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
+3
View File
@@ -25,4 +25,7 @@
- (void) updateUniqueId:(NSString*)uniqueId; - (void) updateUniqueId:(NSString*)uniqueId;
- (NSString*) getUniqueId; - (NSString*) getUniqueId;
// Don't call this outside of database code
- (App*) getAppForTemporaryApp:(TemporaryApp*)tempApp;
@end @end
+36 -10
View File
@@ -61,13 +61,14 @@
- (void) updateHost:(TemporaryHost *)host { - (void) updateHost:(TemporaryHost *)host {
[_managedObjectContext performBlockAndWait:^{ [_managedObjectContext performBlockAndWait:^{
// Add a new persistent managed object if one doesn't exist // 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]; 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 // Push changes from the temp host to the persistent one
[host propagateChangesToParent]; [host propagateChangesToParent:parent withDm:self];
[self saveData]; [self saveData];
}]; }];
@@ -98,14 +99,12 @@
} }
- (void) removeHost:(TemporaryHost*)host { - (void) removeHost:(TemporaryHost*)host {
if (host.parent == nil) {
// Not inserted into the DB
return;
}
[_managedObjectContext performBlockAndWait:^{ [_managedObjectContext performBlockAndWait:^{
[_managedObjectContext deleteObject:host.parent]; Host* managedHost = [self getHostForTemporaryHost:host];
[self saveData]; if (managedHost != nil) {
[_managedObjectContext deleteObject:managedHost];
[self saveData];
}
}]; }];
} }
@@ -132,6 +131,33 @@
return tempHosts; 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*) fetchRecords:(NSString*)entityName {
NSArray* fetchedRecords; NSArray* fetchedRecords;
+1 -3
View File
@@ -18,15 +18,13 @@
@property (nonatomic) BOOL isRunning; @property (nonatomic) BOOL isRunning;
@property (nullable, nonatomic, retain) TemporaryHost *host; @property (nullable, nonatomic, retain) TemporaryHost *host;
@property (nullable, nonatomic) App* parent;
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
- (id) initFromApp:(App*)app withTempHost:(TemporaryHost*)tempHost; - (id) initFromApp:(App*)app withTempHost:(TemporaryHost*)tempHost;
- (NSComparisonResult)compareName:(TemporaryApp *)other; - (NSComparisonResult)compareName:(TemporaryApp *)other;
- (void) propagateChangesToParent; - (void) propagateChangesToParent:(App*)parent withHost:(Host*)host;
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END
+5 -7
View File
@@ -14,8 +14,6 @@
- (id) initFromApp:(App*)app withTempHost:(TemporaryHost*)tempHost { - (id) initFromApp:(App*)app withTempHost:(TemporaryHost*)tempHost {
self = [self init]; self = [self init];
self.parent = app;
self.id = app.id; self.id = app.id;
self.image = app.image; self.image = app.image;
self.name = app.name; self.name = app.name;
@@ -24,11 +22,11 @@
return self; return self;
} }
- (void) propagateChangesToParent { - (void) propagateChangesToParent:(App*)parent withHost:(Host*)host {
self.parent.id = self.id; parent.id = self.id;
self.parent.image = self.image; parent.image = self.image;
self.parent.name = self.name; parent.name = self.name;
self.parent.host = self.host.parent; parent.host = host;
} }
- (NSComparisonResult)compareName:(TemporaryApp *)other { - (NSComparisonResult)compareName:(TemporaryApp *)other {
+3 -3
View File
@@ -10,14 +10,14 @@
#import "Utils.h" #import "Utils.h"
#import "Host.h" #import "Host.h"
@class DataManager;
@interface TemporaryHost : NSObject @interface TemporaryHost : NSObject
@property (nonatomic) BOOL online; @property (nonatomic) BOOL online;
@property (nonatomic) PairState pairState; @property (nonatomic) PairState pairState;
@property (nonatomic, nullable) NSString * activeAddress; @property (nonatomic, nullable) NSString * activeAddress;
@property (nullable, nonatomic) Host* parent;
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, retain) NSString *address; @property (nonatomic, retain) NSString *address;
@@ -32,7 +32,7 @@ NS_ASSUME_NONNULL_BEGIN
- (NSComparisonResult)compareName:(TemporaryHost *)other; - (NSComparisonResult)compareName:(TemporaryHost *)other;
- (void) propagateChangesToParent; - (void) propagateChangesToParent:(Host*)host withDm:(DataManager*)dm;
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END
+15 -15
View File
@@ -6,6 +6,7 @@
// Copyright © 2015 Moonlight Stream. All rights reserved. // Copyright © 2015 Moonlight Stream. All rights reserved.
// //
#import "DataManager.h"
#import "TemporaryHost.h" #import "TemporaryHost.h"
#import "Host.h" #import "Host.h"
#import "TemporaryApp.h" #import "TemporaryApp.h"
@@ -16,8 +17,6 @@
- (id) initFromHost:(Host*)host { - (id) initFromHost:(Host*)host {
self = [self init]; self = [self init];
self.parent = host;
self.address = host.address; self.address = host.address;
self.externalAddress = host.externalAddress; self.externalAddress = host.externalAddress;
self.localAddress = host.localAddress; self.localAddress = host.localAddress;
@@ -37,28 +36,29 @@
return self; return self;
} }
- (void) propagateChangesToParent { - (void) propagateChangesToParent:(Host*)parentHost withDm:(DataManager*)dm {
self.parent.address = self.address; parentHost.address = self.address;
self.parent.externalAddress = self.externalAddress; parentHost.externalAddress = self.externalAddress;
self.parent.localAddress = self.localAddress; parentHost.localAddress = self.localAddress;
self.parent.mac = self.mac; parentHost.mac = self.mac;
self.parent.name = self.name; parentHost.name = self.name;
self.parent.uuid = self.uuid; parentHost.uuid = self.uuid;
NSMutableSet *applist = [[NSMutableSet alloc] init]; NSMutableSet *applist = [[NSMutableSet alloc] init];
for (TemporaryApp* app in self.appList) { for (TemporaryApp* app in self.appList) {
// Add a new persistent managed object if one doesn't exist // Add a new persistent managed object if one doesn't exist
if (app.parent == nil) { App* parentApp = [dm getAppForTemporaryApp:app];
NSEntityDescription* entity = [NSEntityDescription entityForName:@"App" inManagedObjectContext:self.parent.managedObjectContext]; if (parentApp == nil) {
app.parent = [[App alloc] initWithEntity:entity insertIntoManagedObjectContext:self.parent.managedObjectContext]; 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 { - (NSComparisonResult)compareName:(TemporaryHost *)other {