Need help storing Core Data entities!! Eeek!

Discussion in 'Mac Programming' started by Littleodie914, Oct 4, 2006.

  1. Littleodie914 macrumors 68000


    Jun 9, 2004
    Rochester, NY
    Hey guys, as some of you may know, I'm writing a program that keeps track of homework assignments and the "steps" that make up the assignment. Ex. A physics paper may require 3 steps, a first draft, a proofread, and a final draft. I have a button that will "archive" the assignment, and store it in a separate NSArrayController. I also added functionality to restore the task once it has been archived. The problem is, I'm having trouble storing the steps associated with the assignment. I'm using Core Data, and it uses two entities, Assignment and Steps. I have no problem archiving the selected assignment object in the NSTableView, but how would I also store the steps associated with it? For steps that haven't yet been archived, I'm using a simple NSTableView that is bound to the Core Data model.

    And on a side-note, when a new assignment is added to the table, is there a way to make it automatically in "edit-mode"? I'd like to make it so you don't have to double-click on the title to change it right after you've created the new one. Thanks! :D
  2. Krevnik macrumors 68040


    Sep 8, 2003
    When you create an instance the of Step entity, it is already placed in the store, although depending on the level you are implementing (Are you using a NSPersistentDocument-based model?), you might need to assign new objects to the particular store. Considering you seem to have no problems with the Assignment object, I will assume you know this already.

    However, it does not setup relationships for you. If you haven't done this already, you need a one-to-many relationship to the Step entity from the Assignment entity, and then an inverse relationship to the Assignment entity in your data model. Once the data model knows about the relationship, you can then in your code, add the new entity as a 'child' of the Assignment entity. Once this is done and your data saved to the store, the relationships are also stored for you, and reconstructed when you open it back up.

    As for the second question, there is a method that might be worth looking at: [NSTableView editColumn:row:withEvent:select:]. The description says that it is normally invoked by the UI, and shouldn't normally be invoked by the developer, but it seems to be exactly what you want. You just need to know the Column/Row of the new element, which might be tricky, but I don't know the complexity of your application.

    Edit: As an aside, the way it seems you are using Core Data isn't really going to benefit you with this application and is more complex than you need. Are your assignments being dumped out to file on their own? If so, you should probably be using an NSPersistentDocument model if you want to continue using Core Data and not overly complicate your code. If you are trying to create something where you see all your assignments in a single window, then I would consider using a single data store for ALL your data (have it mirror how the user interacts with it, instead of creating this disconnect), or if you can't do that, then I would abandon CoreData and use the basic Cocoa data-types. You can write dictionaries/arrays out to plist files, and the plist files can be written in a binary format to disk (IIRC).

Share This Page