开发者

Core Data Document-based application: How do I model and control a entity that is instantiated only once per document?

I've got a few questions I've been trying to answer for myself (by hunting through the documentation) but I have a feeling I'm missing something.

Any hints (and/or pointers to appropriate documentation) would be 开发者_如何学Cmuch appreciated.

I'm building a Core Data document-based application. There are essentially two entities:

There is a single "Comparison" record associated with each document. There are potentially many "Node" records associated with each document.

My first question is whether I'm thinking about this correctly. Since there is only a single Comparison object for each document, the attributes of the Comparison are essentially attributes of the Document itself. What (if any) is the preferred way of modeling that?

If a Comparison entity is in fact the right way to go, my next question is how and when to actually instantiate the (single) Comparison object. The user should not have to explicitly "add" the Comparison since there's going to be only one of them associated with the Document. Instead, a single Comparison object should be instantiated and inserted into the managedObjectContext. I've got something like this working already, with code in MyDocument.m that looks like this:

  • (void)windowControllerDidLoadNib:(NSWindowController *)windowController { [super windowControllerDidLoadNib:windowController]; [NSEntityDescription insertNewObjectForEntityForName:@"Comparison" inManagedObjectContext:managedObjectContext]; }

However -- if the user creates a new document but then never does any work with it -- for example if he immediately clicks the close button -- then he should not be asked to "Save" the document. He should be asked to save his work only if he's actually entered any information. Is there a preferred way to implement this behavior?


I found this thread while struggling with the exact same issue. I have a table of Entity_A working in my document based Core Data app, but I need to figure out how to handle a required single-instance per document of Entity_B.

I've found something that seems to work. There's probably a better way, but this is getting me past this hurdle for now.

When the document's xib is loaded I simply check to see if an Entity_B has been created. if not, I create one and initialize its attributes.

    - (void)windowControllerDidLoadNib:(NSWindowController *)aController
    {
        [super windowControllerDidLoadNib:aController];

        //has an Entity_B been created?  if not, create one.
        NSError *theError = nil;
        NSUInteger count = [[self managedObjectContext] countForFetchRequest:[NSFetchRequest fetchRequestWithEntityName:@"Entity_B"] error:&theError];

        if( count == 0 )
        {
            NSManagedObject *newEntity_B = [NSEntityDescription insertNewObjectForEntityForName:@"Entity_B" inManagedObjectContext:[self managedObjectContext]];

            [newEntity_B setValue:[NSNumber numberWithBool:YES] forKey:@"boolAttribute"];
            [newEntity_B setValue:[NSNumber numberWithInt:2] forKey:@"intAttribute"];
        }
    }


I didn't insert that code snippet into the original post correctly. Trying again:

-(void)windowControllerDidLoadNib:(NSWindowController *)windowController {
   [super windowControllerDidLoadNib:windowController];
   [NSEntityDescription insertNewObjectForEntityForName:@"Comparison" inManagedObjectContext:managedObjectContext];
}


Your question about modelling is not very clear, can you please elaborate on what your "Comparison" entity is supposed to do and what sort of attributes you are assigning to it? It would be handy to see your "Document" entity structure so we can provide some useful input.

With regards to your second question, you could check if your NSManagedObject has been updated before deciding on whether to prompt the user to save their document or not:

if ([documentObject isUpdated]) {
...
}

More details in the documentation here http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/Reference/NSManagedObject.html#//apple_ref/occ/cl/NSManagedObject

Cheers, Rog


There isn't really a "Document" entity, I was simply using that term to refer to the overall document that is saved when the user invokes the Save menu item. Perhaps there is a better way to refer to this concept? NSPersistentDocument?

Backing up a bit... the central idea of the application is to compare two hierarchical directory structures (a visual recursive "diff").

For now the "Comparison" entity has two string attributes, pathA and pathB, which are the names of the two directories to be compared. Each "Node" entity represents the name of a file down in the directory trees that are being compared. The Node entity contains at least one attribute ("relativePath") which is the path relative to the starting point specified in the Comparison.

My first question was simply whether it makes sense for there to be a "Comparison" entity since there is going to be only one of them instantiated (at some point after the user invokes the "New" menu item).

The second question is really at what point should the single "Comparison" object be instantiated and inserted into the managedObjectContext, i.e. what method is most appropriate to make this happen?

Finally if a "Comparison" object is automatically instantiated (at awakeFromNib time, perhaps?) but the user decides not to proceed, and simply clicks the close button, he should not be prompted to save (right?) What would be the appropriate way to accomplish this? The documentObject will appear to have been updated, because an "empty" Comparison object has in fact already been inserted automatically at startup, but the user has not modified it.

Hope that's clear... thanks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜