Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Chirone

macrumors 6502
Original poster
Mar 2, 2009
279
0
NZ
With Core Data I have entity1 and entity2

entity1 and entity2 have a relationship, from entity1 to entity2 it's a to-many-relationship, from entity2 to entity 1 it's just a one-to-one

when a property of entity1 changes then the set of entity2 objects related to entity1 needs to change (does that make sense to you guys?)

eg, a property of entity1 changes from "hello world" to "farewell cruel world" and the set of entity2 becomes different to what it was before

how can I do this?

the following are some of the ways i've tried...

in all cases the property that is causing the change has been declared in the header as
Code:
@property (nonatomic, retain) NSString* propertyOne;
the relationship has been declared as
Code:
@property (retain) NSSet* relationship;

in the implementation file i have:
Code:
@dynamic propertyOne;
@dynamic relationship;

now... what i've done is override the setPropertyOne method to do this:
- change the property
- for each part of property do
- - create an instance of entity2 and add it to a set
- make the relationship equal to that set

in code:
Code:
-(void)setPropertyOne: (NSString*)newPropertyOne {
// change the property
	[self willChangeValueForKey: @"propertyOne"];
	propertyOne = newPropertyOne;
	[self didChangeValueForKey: @"propertyOne"];

// create the set of entity2 objects
	NSManagedObjectContext* managedObjectContext = [self managedObjectContext];
	NSArray* propertyParts = [newPropertyOne componentsSeparatedByString: @" "];
	
	NSMutableSet* newSetOfPropertyOne = [[NSMutableSet alloc] init];
	for(int i = 0; i < [propertyParts count]; i++) {
		NSEntityDescription* entity = [NSEntityDescription insertNewObjectForEntityForName: @"EntityTwo" 
																inManagedObjectContext: managedObjectContext];
		EntityTwo* newEntityTwo = (EntityTwo*)entity;
		newEntityTwo.popertyOne = [propertyParts objectAtIndex: i];
		
		[newSetOfPropertyOne addObject: newEntityTwo];
		
	}

// finally update the set
[self willChangeValueForKey: @"containsWords"];
	self.containsWords = nil;
	self.containsWords = newSetOfWords;
	[self didChangeValueForKey: @"containsWords"];
	[newSetOfWords release];
}
now.. i know this works
because it works when entityOne first awakes from insert
i can undo and redo perfectly without crashing

however, once propertyOne is changed after the fact redo breaks and the program crashes...

what am i doing wrong?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.