Cocoa: CoreData Application Not Saving Attributes

Discussion in 'Mac Programming' started by ebel3003, Nov 5, 2008.

  1. ebel3003 macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #1
    Hello MR,

    After leaving the Cocoa scene for web dev, I've decided to return and learn Cocoa once and for all (and it's working this time). Unfortunately I've found myself at a stand-still once again.

    I created a new Core Data application in hopes of it doing most of the work for me when saving information. It's been great so far. However, I've found that the application will only save what's in the Core Data database if called with an action through Interface Builder. I'm unable to call the add: method through my implementation file and have it save contents of the database into a file. It will add objects to the array fine, but will not save its contents, even when they are clearly present in my table view.

    Code:
    NSMutableDictionary *Item = [[NSMutableDictionary alloc] init];
    [Item setValue:@"AttributeFor1" forKey:@"Attribute1"];
    [Item setValue:@"AttributeFor2" forKey:@"Attribute2"];
    [Item setValue:@"AttributeFor3" forKey:@"Attribute3"];
    [myArrayController addObject:Item];
    Is this an issue of delegates or something?

    Thanks
     
  2. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    I think you have to call save: first to commit the changes. From the Developing with Core Data page:

     
  3. ebel3003 thread starter macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #3
    Thanks for your reply. Unfortunately calling the save: action didn't seem to work, the file still came out blank. Is it because I am using NSMutableDictionarys? If I call the add: method, it works fine but those are empty entries. If I call addObject:myMutableDictionary, it inserts fine, displays fine, but doesn't get saved. Is a ManagedObject what I need to implement?
     
  4. Krevnik macrumors 68030

    Krevnik

    Joined:
    Sep 8, 2003
    #4
    You should really go back and read through the CoreData documentation at this point, otherwise it will just get more confusing from here on out.

    Any object in the CoreData DB must be created via the managed object context for the DB, you can't use alloc/init. It also needs to be one of the entity types. If you aren't setting up custom classes for your entities in the data model, then this will always be NSManagedObject.

    NSManagedObject is KVC-compliant, so you will be using setValue:forKey: and getValueForKey: like you would with any other KVC-compliant class.
     
  5. ebel3003 thread starter macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #5
    Thanks for the responses. I'll go back to the documentation and read up a bit.
     
  6. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #6
    To add to the previous responses you should create custom subclasses of NSManagedObject for your core data objects with setters/getters so you can validate etc and not use setValue:forKey: and valueForKey: directly

    This is a mistake I have made in D&D Manager and the code would be much cleaner and better if I had done this right.
     
  7. lucasgladding macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #7
    In case anyone is interested, Pragmatic Programmers has a Core Data book in beta right now. You can find it here:

    http://pragprog.com/titles/mzcd/core-data

    I haven't read it yet and can't comment on the quality, but thought someone might be interested.

    Kind regards
     
  8. ebel3003 thread starter macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #8
    I was able to get everything working with NSManagedObjects and am going to work on subclassing my own now. I'm also going to look into that book as I've found Pragmatic's stuff helpful.

    Thanks again.
     
  9. Krevnik macrumors 68030

    Krevnik

    Joined:
    Sep 8, 2003
    #9
    You can write your own setters/getters for validation without losing the ability to use valueForKey:/setValue:forKey:...

    It is actually critical that you continue to follow normal KVC convention with your setters/getters if you want to use Cocoa bindings and get validation with it.
     
  10. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #10
    Of course, but you shouldn't generally use that method to set/get stuff and should rather use your own code to call them and validate instead.
     
  11. knightlie macrumors 6502a

    Joined:
    Feb 18, 2008
    #11
    Could you elaborate just a tad - do you mean that for an NSManagedObject descendant, I should use, say, [transaction setAmount:500] as opposed to [account setValue:500 forKey:mad:"amount"], having defined a -setAmount: message?
     
  12. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #12
    Yes
     

Share This Page