|
If you're as new to Core Data as I am, check out this video tutorial, I found it extremely useful.
I have been able to produce the currency converter sample in OS X Tiger and I was fairly pleased. I was surprised by how much of the plumbing took place in the NIB and how little took place in the code. I like that. If you're an old hat at .NET, you'll know that even with the advent of partial classes, you still have code lingering around somewhere that has hundreds of lines that look like this:
myControl.Click += new EventHandler(....blah );
I have no idea which I like better, though I can imagine how the loosely coupled message sending facilities of Objective-C on the Cocoa frameworks allows for some pretty reusable code as well as UI. In other words, I think it would be easier to take a GUI that performed a set of tasks and re-use it in another application because the NIB is not only isolated, but can be plugged into any xcode project. I'm sure all you Apple programmers are thinking, "*yawn*" , but hey - I'm new here.
For my next foray into the unknown (which is a pretty unusual spot for me to be in... I've been programming .NET for so long, I am unaccustomed to feeling lost and overwhelmed by a programming environment) I decided to take a whack at Core Data. Core Data is, I believe, something that was introduced in OS X 10.2 or 10.3. Case in point, the big SAMS book I have on Cocoa written for 10.1 doesn't have a single word referring to Core Data. I'm hoping the one someone from a previous post recommended (en route to me now) will have more information on Core Data.
It's tough to pinpoint a good comparison for Core Data. You could actually consider Core Data to be in the same category as the ADO.NET Entity Framework that is still in CTP status. The ADO.NET EF creates an abstraction layer above the physical data model and allows you to define entities, attributes, and relationships between entities. The EF will happily map entities to underlying SQL Server (potentially other providers will be available in the future). Core Data also allows you to define entities, entity relationships, and attributes on those entities. I think the main difference is that those entities are your data model and Core Data allows you to persist your model as binary, XML, or SQL.
Here's a sample scenario to compare the two:
You have some software like Quicken or MS Money. You have bank accounts, and you have a ledger that contains a list of transactions. Each transaction belongs to a given bank account, but also has been assigned a category. Anybody with any exposure to relational databases is probably already envisioning the data structure for this app.
At this point I was thinking I was looking at really two different takes on the same concept - object-relational mapping. The Entity Framework from Microsoft and Core Data from Apple. It also seemed pretty obvious that the Entity Framework is designed specifically for dealing with data contained externally in relational databases while Core Data is designed specifically for storing data on the local disk for desktop applications. I think its hard to say which one is "better" since they both tackle two different problems. However, I did love the fact that I didn't have to set up this completely arbitrary identifier columns that served only to link related entities - Core Data links them for me without creating excess "baggage" on the entity.
The next thing I did was create some GUI that was bound to my objects. I did this both in Leopard and in Tiger. I can't discuss the Leopard experience, but it hasn't changed so radically that my description of the Tiger experience is invalid. Basically I used an NSTableView, created an Array Controller, bound the array controller to the list of Account entities and then bound the table view to the array controller. For each column, I set the value binding to a model expression like accountName for the account name, etc. Within about 5 minutes, I had a grid in my window and was able to click my "Add" and "Remove" buttons to actually create new accounts and manipulate their data. You can do the same thing in C# 2005 with the data navigation control, object data providers, and a couple lines of code rigged up to the navigator's add and remove buttons.
Here's what I liked about the Cocoa experience more than the C# experience:
So, in short, my experience with Core Data was pleasant. The whole thing just felt very smooth and I got the feeling that it would feel as smooth for an application with 40 entities as it does for an application with 2 entities. You can do pretty much everything in C# 2005 with the EF that you can do with Core Data, but there is really no compelling object-relational system for "desktop applications" (aside from targeting MSDE in your entity mapping schema) that is built-in "out of the box" in .NET, whereas Core Data fills that need but lacks in its ability to communicate with remote relational databases.
Anyway - I am still looking for showstoppers or blockbusters - things that either make me want to run screaming away from Cocoa programming or things that make me want to abandon C# programming. So far I have found neither and will keep looking as I learn more Objective-C and Cocoa.
It's been interesting reading about your OSX dev experiences. Since I know
you'v worked with WPF, I would like to see some discussion of how interface
builder and databinding compare to the same functionality in WPF, using
tools like Expression Blend in combination with VS. I think this is only
fair, since WPF is the choice for Windows development moving forward.
Also, I would like to mention that Microsoft just released SQL Server
Compact Edition, which is an rdbms that is fully embeddable in
applications; and its free. This certainly addresses some of the
functionality in Core Data, especially when combined with an O/R mapper
like NHibernate or the upcoming EF.
Core Data was introduced with Mac OS X Tiger 10.4, not 10.2 or 10.3.
However, its "spiritual ancestor" - the Enterprise Objects Framework - has
been available on Mac OS X as part of WebObjects for many years, and was
originally shipped by NeXT in the early-mid 1990s.
How would BaseTen compare to ADO.NET EF? http://www.karppinen.fi/baseten/
Well, since I'm a complete newbie to the OS X world as well as to Cocoa
development, I had no idea that Baseten existed. If Baseten utilizes the
same feel and syntax that Core Data uses, but it allows the persistence to
be remote in PostgreSQL databases with Stored procs (does PostgreSQL do
procs? I'm not sure...) then I'd say Baseten fills the remote relational
database gap left by the predominantly desktop-based Core Data.
Thanks for the comment. I think I will do an upcoming blog post comparing binding syntax and procedure with WPF/XAML and Core Data. I'd have to do a programmatic binding sample to compare Apples vs Apples (har har) because Core Data binding can be done entirely through the GUI without writing a line of code, whereas all of the WPF binding is done through XAML (or should be, if you're a XAML purist like myself)
Hi Kevin
thanks for sharing your experience. I am getting ready to do the same thing
- as soon as Leopard is out: get a Mac Pro and give Objective-C serious
look. I have similar background C#, ex-Java, ex-C++, ex-C. What you wrote
so far sounds very encouraging. Please keep on writing.
To improve the look & feel of CoreData you may also want to check out
mogenerator (short for Managed Object Creator,
http://www.rentzsch.com/code/mogenerator_v1.1) . To add some dynamical
features see http://www.2pi.dk/tech/cocoa/.
Jon Rentzsch did a great job on the videos. You might also find these
useful:
http://www.cocoadevcentral.com/articles/000085.php
http://www.cocoadevcentral.com/articles/000086.php
It's been a blast reading all of this Kevin; I'm definitely staying tuned.
Glad to see you moving so quickly!
Don't feel bad about feeling lost in Cocoa, Carbon, etc. They are immensely
huge. Just have fun exploring. Be sure to look at Leopard as soon as you
can get your hands on. The publicly known trifecta of garbage collection,
new syntax and Core Animation alone should be enough to blow your mind if
you are into programming desktop apps. But there's also some stuff we
NDA-ridden ADC members can't talk about, and it's pretty neat too!
Hi Kevin, I am enjoying your blog describing your forays into the wonders
of Cocoa land. I am also fairly new to Cocoa programming. Is it definitely
the case that Core Data cannot be linked to a remote SQL database (Oracle,
MySQL, Postgres, etc)? ODBC drivers are available for various databases I
believe. Looking at the Cocoa Fundamentals guide section on Core Data there
is a layer called the 'Persistent Object Store' which seems to be the
interface to the datasource itself. I couldn't see a corresponding class in
Foundation for such a thing but maybe there is one in a different
framework? Can any experienced Cocoa coders comment please?
Have a look at the ODBCKit framework and Andrew Satori's home page,
http://www.druware.com/products/odbckit.html and
http://web.mac.com/dru_satori/iWeb/Words%20of%20a%20Geek/ODBC/ODBC.html
I've recently been doing some with using JSON-RPC infrastructure that knows
about GID's and EO's to integrate a Cocoa client talking to a
WebObjects/EOF application server system. It seems to be working out quite
well.
Kevin, Years ago when i was learning Obj-C on the NeXT, I was amazed to see
how easily an application I had developed to analyze and plot the data from
my research could be reused. Essentially, I had a graphical view of the
data curves, interactively selected points for fitting curves to it and
stored those points into a text file. It took me several months to learn
OOP wiht NeXTStep, but it took about 3 hours to reuse the objects in a very
similar but completely different application. That was power.