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

Boneoh

macrumors 6502
Original poster
Feb 27, 2009
318
2
So. Cal.
Hi, all. I am struggling to understand how all Core Data and Managed Object Model actually work. I am a noob as far as objective C and Cocoa go, so it's pretty obvious that I am hitting the learning curve. I've been reading the Core Data docs and the Hilegass book, but there are definitely some holes that I need to fill in. Here goes:

1. What is the difference in my xib Controller in specifying mode = Entity vs. Class? When should I use one or the other?

2. My application will only have a single instance of the model entity, called Network, per document. I am using an ObjectController as opposed to an ArrayController. This seems to be working fine, just want to be convinced that I am on the right path.

3. This question relates to the code snippet below. When I open my application, an new blank document of the Network entity is created. The initWithType: gets invoked, where I set the self.network property. This works fine. When I open an existing Network document, and click a button in my app window, the buildNetwork: gets invoked. I need to set self.network there as well. The [self setNetwork: ...] does not seem to do what I need to do. I've hit my head against the wall on this for a while, and I need to punt.

Code:
- (IBAction)buildNetwork:(id)sender {
	
	NetBuilder *netBuilder;
	
	@try 
	{
		[self setNetwork:[[NSDocumentController sharedDocumentController] currentDocument]];
		
		netBuilder = [[[NetBuilder alloc ] init ] retain];
		[netBuilder setNetwork:self.network];
		
		[netBuilder buildNetwork];
	}
	
	@catch ( NSException *e ) 
	{
		NSLog(@"MyDocument error in buildNetwork: %@", [e reason]);
	}
	
	[netBuilder release];
	
}

- (id)initWithType:(NSString *)type error:(NSError **)error {
    self = [super initWithType:type error:error];
    if (self != nil) {
        NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
        [[managedObjectContext undoManager] disableUndoRegistration];
        self.network = [NSEntityDescription insertNewObjectForEntityForName:@"Network"
													 inManagedObjectContext:managedObjectContext];
        [managedObjectContext processPendingChanges];
        [[managedObjectContext undoManager] enableUndoRegistration];
		
    }
    return self;
}

4. My managed object model has the Network class as the root. There are one to many relationships defined between the Network and two other classes called Node and Path. The Network class has this to contain the Node children

Code:
@property (retain)  NSSet* nodes;

The Node class has this to contain the Path children

Code:
@property (retain) NSSet* outputPaths;
@property (retain) NSSet* inputPaths;

This seems to me to be correct, but if I'm wrong please let me know. My application will be creating all of the child Nodes and Paths as needed. Will Core Data correctly save all of the child Nodes and Paths when I save? Or will my ObjectController need to get switched to a TreeController? I'm not sure if it makes a difference, but the Nodes and Paths will never be displayed on a window, edited by the user, etc.

Thanks in advance :)
 
Hi, all. I am struggling to understand how all Core Data and Managed Object Model actually work. I am a noob as far as objective C and Cocoa go, so it's pretty obvious that I am hitting the learning curve. I've been reading the Core Data docs and the Hilegass book, but there are definitely some holes that I need to fill in. Here goes:

1. What is the difference in my xib Controller in specifying mode = Entity vs. Class? When should I use one or the other?

You have a xcdatamodel, in which you created the different types of managedobjects. If you create a class for such an object, you have a class. Otherwise it's an entity. Unless for very simple structures, you probable converted the entities to classes. That gives you freedom to add your custom code/behaviour/logic to that managedobject.

2. My application will only have a single instance of the model entity, called Network, per document. I am using an ObjectController as opposed to an ArrayController. This seems to be working fine, just want to be convinced that I am on the right path.

If it works, it works.

3. This question relates to the code snippet below. When I open my application, an new blank document of the Network entity is created. The initWithType: gets invoked, where I set the self.network property. This works fine. When I open an existing Network document, and click a button in my app window, the buildNetwork: gets invoked. I need to set self.network there as well. The [self setNetwork: ...] does not seem to do what I need to do. I've hit my head against the wall on this for a while, and I need to punt.

Code:
- (IBAction)buildNetwork:(id)sender {
	
	NetBuilder *netBuilder;
	
		[self setNetwork:[[NSDocumentController sharedDocumentController] currentDocument]];
		
->		netBuilder = [[[NetBuilder alloc ] init ] retain];
		[netBuilder setNetwork:self.network];
		
		[netBuilder buildNetwork];
	}
	
->	[netBuilder release];	
}

Why do you have a retain and a release for netBuilder? If you want to keep netBuilder around, just do the alloc, and release it in the dealloc function.
initWithType is not called for existing documents. You need
Code:
initWithContentsOfURL:ofType: error:


4. My managed object model has the Network class as the root. There are one to many relationships defined between the Network and two other classes called Node and Path. The Network class has this to contain the Node children

Code:
@property (retain)  NSSet* nodes;

The Node class has this to contain the Path children

Code:
@property (retain) NSSet* outputPaths;
@property (retain) NSSet* inputPaths;

This seems to me to be correct, but if I'm wrong please let me know. My application will be creating all of the child Nodes and Paths as needed. Will Core Data correctly save all of the child Nodes and Paths when I save? Or will my ObjectController need to get switched to a TreeController? I'm not sure if it makes a difference, but the Nodes and Paths will never be displayed on a window, edited by the user, etc.

Thanks in advance :)

As long as you maintain the correct (graph)state of your data, cocoa will not care if you use a NSTreeController. It's there to help you, not to force you to use it. Anyway, until Leopard, NSTreeController was annoying to work with, to say the least.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.