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

ebel3003

macrumors 6502a
Original poster
Jun 20, 2007
630
0
"The Google"
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
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
I think you have to call save: first to commit the changes. From the Developing with Core Data page:

Saving Changes
All changes to the objects managed by Core Data happen in memory and are transient until they are committed to disk. To commit changes to the data model to disk, simply send a save: message to the managed object context. This behavior preserves the traditional document semantics that users expect in document-based applications.
 

ebel3003

macrumors 6502a
Original poster
Jun 20, 2007
630
0
"The Google"
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?
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
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.
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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.
 

ebel3003

macrumors 6502a
Original poster
Jun 20, 2007
630
0
"The Google"
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.
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
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.

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.
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
You can write your own setters/getters for validation without losing the ability to use valueForKey:/setValue:forKey:...

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.
 

knightlie

macrumors 6502a
Feb 18, 2008
546
0
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.

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?
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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?

Yes
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.