Core Data / Array Controller: Edit on Insert

I’m writing a silly little Cocoa app to track my weight over time, trend line and all. Because, well, no task can be done without writing some code… right? right?!?!?!??

The app draws a graph of the weights in a window and allows the user to add new weights in a drawer with a simple table containing two columns; date & weight. Given the simplicity of the data being entered, I had no desire to have a detail view at the bottom containing date/weight fields.

Hooking a “+” button directly to the NSArrayController worked fine, but then the user had to click on the blank row in the table view to start entering data. Bad UE.

Fixing it is easy as long as you remember that you cannot use the target/action methods for adding items to the array controller because they attempt to do a bit of auto-editing setup themselves.

Simply create a action method like the following and make the “+” button fire said action method:

- (IBAction) insertNewWeight: sender;
{
	NSManagedObject *insertedObject = [weightEntryArrayController newObject];
	unsigned newRow = [weightinator rawWeightCount];
	[weightEntryArrayController insertObject: insertedObject atArrangedObjectIndex: newRow];
	[weightEntryTableView editColumn: 0 row: newRow withEvent: nil select: YES];
	[insertedObject release];
}

This will cause the new entry to be inserted right after the last row in the table view with the cell in column 0 (my date cell) having all contained text selected and ready for editing.

Updated: Yup. I forgot to release the new object.

Updated: Nahh… this isn’t an original idea. It is just a Cocoa/Desktop-App form of the Google 15 widget for your Google Home Page. That one has two faults; it doesn’t work in Safari (fitz is working on that) and it doesn’t work for folks that weigh themselves once a week (such as Weight Watcher participants).



13 Responses to “Core Data / Array Controller: Edit on Insert”

  1. Jim Correia says:

    Bill,

    Shouldn’t you be releasing insertedObject since it was created via -newObject?

    Jim

  2. Papa Joe says:

    Bill,

    get a yellow pad & ruler.

    Draw a straight line down the middle.

    Mark 1 column “DATE”; the other column “WEIGHT”!!!

    enter the info daily.

  3. bbum says:

    Missing the obvious. Writing down the numbers on a pad of paper does not give me a running average, nor does it give a graphic indication of trends over time. That, combined with livable lifestyle changes, is the most effective way to lose weight and keep it off.

    Daily weights fluctuate too much to give a real sense of long term trends.

  4. David says:

    If livable lifestyle changes mean getting rid of the BGE, can I have it? ;-)

  5. bbum says:

    hahahahaha… “Livable” means not having to give up good food. I’m finding that if I focus on preparation and quality of ingredients, I can produce really yummy meals that are actually quite good for me.

    Sure, the salmon had a bit of a butter basting, but less than a half a stick for a 9 lbs salmon really isn’t that much! And the turkey was plain beyond the basting goo.

  6. David says:

    I completely agree. Good ingredients and watching the portion sizes are key…

  7. anon says:

    just cut out High Fructose Corn Syrup. That is all.

  8. bbum says:

    Heh. I love it when anonymous makes an oversimplistic suggestion to the point of useless.

    I minimized HFCS consumption many, many years ago and cut it out pretty much entirely in the last couple of years.

    Yet, I still gained weight.

    Without also exercising and actually eating right (i.e. appropriate sized portions & focus on preparation), eliminating HFCS does little more than following any of the other fad diets.

  9. David says:

    Coincidentally, The Sun magazine talks about the food industry in this month’s issue. It is a fairly challenging magazine to find, but there is an excerpt from the article that I have linked to in the last line. It opens up a .pdf. If you would rather check it out through the online table of contents, you can find them here:

    http://www.thesunmagazine.org/

    The interview and the excerpt from the book “The Omnivore’s Dilemma” really opened up my eyes. As an example, talking about a meal at McDonald’s he says “I wanted to have that meal because I saw it through a whole different lens this time, and that was the lens of corn. That chapter comes at the end of a long section of the book in which I follow a bushel of corn, the cornerstone of the American food chain, to market. I don’t think people have any idea when they look at a McDonald’s menu that they’re mostly looking at different manifestations of corn. It was fed to the animals that were turned into meat; it was used to fry the French fries; it’s the basis of the salad dressings tnd the sodas. With the help of a scientist on theBerkeley campus, I took a McDonald’s meal and put it through a mass spectrometer. We found that a very high percentage of it could be traced back to cornfields in Iowa…”

    Interview with Michael Pollan on How The Food Industry Has Changed the Way We Eat
    Arnie Cooper

    http://www.thesunmagazine.org/365_Pollan.pdf

  10. Nate says:

    Quality food, Sensible portions, moderate exercise, + large dose willpower

    It’s easier said than done.

  11. Aaron Hillegass says:

    Thanks for the great snippet of code. There is one problem: the table view will not properly end editing when an insert occurs. You really need to take care of this:

    - (IBAction)insertNewWeight:(id)sender;
    {
    [[weightEntryTableView window] endEditingFor:nil];
    NSManagedObject *insertedObject = [weightEntryArrayController newObject];
    unsigned newRow = [weightinator rawWeightCount];
    [weightEntryArrayController insertObject:insertedObject atArrangedObjectIndex:newRow];
    [weightEntryTableView editColumn:0 row:newRow withEvent:nil select:YES];
    [insertedObject release];
    }

  12. Robert 'Groby' Blum says:

    Any chance of you sharing the rest of the code with us? I.e. the entire app? I’m using YellowPadAndPen.App right now, and those moving averages sure would be nice…

    BTW: Nate hits it head-on. The large dose of will power is a key ingredient to weight loss. That’s why people like fad diets – they all promise you don’t really need that ;)

  13. Frank McPherson says:

    I’m almost embarrasingly stuck with Core Data right now, can’t even get past the most complete example Apple has on their site, Core Recipes. I wrote about it in my blog.

    To the folks questioning the usefulness of bbum’s app: a weight trend over time app is one of those things you could easily solve another way, for example with a spreadsheet. But Cocoa and Core Data are an elegant tool, and it’s gratifying to build simple, useful things with nice tools.

Leave a Reply

Line and paragraph breaks automatic.
XHTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>