The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








Latest Diggs - Programming

Internet Blogs - Blog Top Sites

Site Hits

Total: 2,639,104
since: 19 Jan 2005

Core Data - Almost too Easy?

posted Wed 18 Apr 07

I am working on another sample application that I am using as a means of teaching me some more things about Cocoa and programming on the Mac in general. I have been particularly fascinated with Core Data, so I started using it again in a new application.

I have a rather unique perspective on persistence tools because I have probably tested and developed in just about every conceivable environment for rapidly creating data-bound desktop applications, including some that called themselves "4GL" and were basically code generators that would inhale a data model and exhale an application. The general feeling I have come away with from tools that make it so that you can write a data-bound desktop application "with no code at all for the low-low price of $19.95!!" is that such tools are crap. They spend so much time insulating you from what is really going on that as soon as you want to do something other than build the "hello world" samples - you're knee deep in crap without a shovel.

This is not the case with Core Data. At first, I was extremely skeptical. I thought to myself, self: this is to easy. This is going to get ugly when I need to make some minor tweaks. So, I went ahead and wrote another sample app and sure enough, I needed to perform a minor tweak.

Problem 1: I created my model. I have two attributes, one is a number (Int32) and the other column is a date column. I created a table view and bound the columns to the attributes in my entity. Everything looked fine. I ran the app. Whenever I tabbed out of a field, I'd get an error showing up in my Console complaining about a data type mismatch. Basically it looked like the table view was sending a string to the bound attribute, and the bound attribute needed an integer or a date.

Solution 1: Thanks to some tips from a knowledgeable resource on Cocoa, I figured out that I could just drag a number formatter into the number cell, and drag a date formatter into the date cell. Problem solved. My thoughts: holy crap that's easy.

Problem 2: I wanted to make it so that whenever I clicked the "+" button that was linked to the "insert:" action, the date on the underlying model object (which is a core data entity) would be today's date. There didn't seem to be any way to do this in the user interface, so I got scared.

Solution 2: It was actually really , really easy. I created a new class that inherited from NSManagedObject called KHWeighInEntity. I then changed the type of my entity from NSManagedObject to KHWeighInEntity. Then, I overrode a single method in this class as follows:

- (void)awakeFromInsert
{
    [self setValue:[NSCalendarDate date] forKey:@"weighInDate"];
}

That's it. Done. I didn't have to go through the hoops of building a brand new "wheel", all I had to do was override the part of the wheel that needed tweaking, and I was done. To me, scenarios like this are why inheritance was conceived. Here's the fantastically cool part: I can continue to model and extend my entity in the entity designer, and it won't impact the code I've already written. I don't have to manually add attributes to my new custom class... it's still all extremely seamless.

The more I use Core Data (and, of course, Cocoa), the more I become a huge fan. In short, Core Data is a rare example of a solution that allows you to rapidly build solutions without writing a single line of code...and... when you need to write code to create a more powerful solution, that task is simple, quick, and painless.

tags:              

links: digg this    del.icio.us    technorati    reddit




1. Niels Berglund left...
Wed 18 Apr 07 11:34 am

This sounds good, however as a recent Mac switcher (from Windows and Win32, .NET) etc, I wonder where in Cocoa do I find database access? I.e. where in Cocoa (or Carbon or Objective C) do I have classes for database access?


2. Kevin Hoffman left...
Wed 18 Apr 07 11:35 am

Core Data gives you access to Sql-lite as a persistence medium for your model/entities.


3. Niels Berglund left...
Wed 18 Apr 07 11:38 am

OK, but what if I want to access data from Oracle, MySQL etc???

Niels


4. Kevin Hoffman left...
Wed 18 Apr 07 11:40 am

I'm sure one of the Cocoa experts reading this can point you in the right direction, but I believe that there is a Cocoa interface for the Mac ODBC drivers.


5. Niels Berglund left...
Wed 18 Apr 07 11:46 am

Thanks Kevin. I'll have a search around myself.

Nieks


6. Kevin Hoffman left...
Wed 18 Apr 07 11:48 am

Found this - mySQL access from Cocoa: http://mysql-cocoa.sourceforge.net/


7. James Gregurich left...
Thu 19 Apr 07 12:39 am

Kevin,

I have a request.

Could you write an article discussing how you would approach building desktop apps across Windows and Macintosh using Cocoa And C++.NET.

Do you think it is feasible on the Windows side to come up with a design that has a cross platform C++ backend and platform specific GUI layers built on top of cocoa and C++.NET?

What pitfalls would you anticipate?

WHat kinds of basic design decisions would you make to interface STL classes and code with .NET APIs?

Are there enough similarities between .NET and cocoa that one could come up with a coherent set of wrapper classes to abstract them into a consistent interface for cross platform code?

thanks, James


8. HelgeG left...
Thu 19 Apr 07 2:47 am

James, I guess you'll find this page very interesting:

http://developer.apple.com/business/macmarket/imeem.html


9. Johannes Fortmann left...
Thu 19 Apr 07 8:16 am

For connectivity with real databases, there's AJRDatabase (http://sourceforge.net/projects/ajrdatabase/). It's an EOF reimplementation, so you don't get all the CoreData-y goodness. Then there's BaseTen (http://www.karppinen.fi/baseten/), which is under GPL or for-pay license, only has adaptors for Postgres, and is reasonably Coredata-like.


10. Niels Berglund left...
Thu 19 Apr 07 9:11 am

Thanks Johannes! I googled last night after having posted my comments, and came across it Looks interesting! I really wonder why Apple ripped EOF out of Cocoa, what a stupid thing to do


11. James Gregurich left...
Thu 19 Apr 07 10:31 am

HelgeG,

An interesting read. thanks. Sort of the opposite approach to the problem than I was thinking about.

Personally, I would prefer a C++/STL backend. I'm not on the C#/Java bandwagon.

I've bought a bunch of C++.NET books and have been browsing them on and off. looks decent on paper, but MS stuff always looks good on paper even though it can be garbage in practice. So, I'm still holding open the possibility of just using Win32 on the Windows side.

This blog has my interest because comparisons and contrasts of cocoa and .NET are going to be very useful to me in the not-to-distant future. My company has a commercial cross platform app that needs rearchitecting and that process will begin in a few months.

-James


12. Michael James left...
Thu 19 Apr 07 10:34 am

I guess Apple ripped EOF out of Cocoa for 2 reasons.

One, Apple ported WebObjects which is the biggest client of EOF to Java in hopes that companies who have already invested a lot of money, effort, and employees to a J2EE stack would start using WebObjects.

And two, there was a way to create Cocoa apps using EOF even as late as WebObjects 5 (which was the first 100% Java version of the previous Objective-C WebObjects) called EOInterface that integrated with Interface Builder. I don't know if this forced you to write your Cocoa apps in Java, but Apple probably figured that not enough developers were using the technology, so when they unofficially said that all future Cocoa development will be done in Objective-C, that pretty much doomed EOF on Cocoa.

There are a growing number of Mac developers out there that would like an Apple-developed official DB framework. Even better if it was done in Cocoa. You're more than welcome to get in line behind us. :)


13. James Gregurich left...
Thu 19 Apr 07 10:45 am

I'd like to point out something about CoreData that may go unnoticed.

from a practical standpoint, CoreData is a way to connect to a SQL database. However, philosophically, that is not really its purpose in life.

CoreData and Cocoa Bindings are about abstracting models and controllers with the goal of automating the process of creating models and controllers. The idea is that you have interface builder to automate the view layer, but the controller and model layers always had to be manually created...and controllers can be particularly mundane and hairy. So, Apple created tools to bring to models and controllers what views already had.

This explains why CoreData has not been focused on database servers but rather on in-process data stores. CoreData and cocoabindings is about speeding up development of desktop apps and making the code developed more reliable on the first try. Its not really a tool for database connectivity.

EOF is the opposite. it is intended to be a tool for general purpose database access. I have no idea why Apple hasn't exploited EOF better and made it part of OSX. I think they should have, but for some reason, they decided to make it part of webobjects.

-James


14. Timothy Mowlem left...
Thu 19 Apr 07 3:11 pm

I asked the same question recently and found the following:

http://web.mac.com/dru_satori/iWeb/Words%20of%20a%20Geek/ODBC/ODBC.html


15. Fred Alster left...
Fri 20 Apr 07 5:21 am

... and maybe also check out http://jtds.sourceforge.net for db connectivity...


Tag Related Posts

So I'm in the LA Times ;)

Wed 27 Aug 08 2:51 P GMT-05
tags:                  

MobileMe vs. Live Mesh Throwdown - Round 1

Wed 16 Jul 08 10:33 A GMT-05

Building Model Classes in C# and Cocoa

Sun 15 Jun 08 3:13 P GMT-05
tags:            

MobileMe vs. Live Mesh - Round 1

Wed 11 Jun 08 12:20 A GMT-05

NYC SharePoint Developer Needed

Mon 12 May 08 12:09 P GMT-05

My Macbook Air is masculine, dammit!

Mon 17 Mar 08 6:59 P GMT-05
tags:          

iPhone Underrated as a Gaming Device?

Fri 14 Mar 08 1:50 P GMT-05
tags:        

My take on the iPhone SDK

Sat 08 Mar 08 1:39 P GMT-05

Jobs says "not likely" to Flash on the iPhone

Thu 06 Mar 08 1:39 A GMT-05
tags:          

My Macbook Air Review

Sun 02 Mar 08 4:20 P GMT-05

iPhone Roadmap March 6th

Fri 29 Feb 08 10:41 P GMT-05
tags:        

Video of the Macbook Air in Action

Wed 20 Feb 08 3:04 P GMT-05

Macbook Airはきれいですよ!

Sun 17 Feb 08 2:38 A GMT-05

Why is O'Reilly Condoning iPhone Hacking?

Mon 11 Feb 08 3:55 P GMT-05

Evaluating my next laptop purchase

Wed 06 Feb 08 8:40 P GMT-05

The iPhone SDK key has been leaked! Oh Noez!!!1

Tue 29 Jan 08 11:36 A GMT-05
tags:        

Why Geeks just don't "get" the Macbook Air

Thu 17 Jan 08 2:30 P GMT-05

Popcorn + TiVo + Macbook Pro + iPhone == Hell Yeah!

Tue 15 Jan 08 3:11 P GMT-05
tags:          

How my ADC membership changed my life

Mon 31 Dec 07 3:57 P GMT-05
tags:      

Leopard Code Sample : Sprinkling in some Bonjour

Tue 27 Nov 07 2:32 P GMT-05
tags:        

Leopard Sample: A Bound NSCollectionView

Mon 29 Oct 07 1:41 A GMT-05

Leopard is out - let the code samples begin!

Fri 26 Oct 07 10:09 A GMT-05
tags:          

My life is complete : iPhone SDK is CONFIRMED.

Wed 17 Oct 07 6:38 P GMT-05
tags:          

Leopard Shipping October 26th!!

Tue 16 Oct 07 4:59 P GMT-05
tags:        

My iPhone Review

Mon 23 Jul 07 1:09 P GMT-05
tags:        

Microsoft Codename Acropolis - Unwrapped

Wed 20 Jun 07 3:22 P GMT-05
tags:              

The dreaded language bleed-over has begun

Tue 19 Jun 07 6:23 P GMT-05
tags:        

Exploring the Delegate Design Pattern

Mon 14 May 07 6:30 P GMT-05

Orcas EDM Wizard Patched

Fri 27 Apr 07 11:56 A GMT-05
tags:      

Core Data - Almost too Easy?

Wed 18 Apr 07 2:23 P GMT-05

Will Silverlight be DOA?

Mon 16 Apr 07 8:02 P GMT-05