After my son, Liam, was born in July, I bought a book and intended to write a small iOS application. At that time, I hadn’t even thought about “real” programming in a little over 10 years. When I say real, I mean having to worry about writing code at the device level – having to deal with shared memory, memory leaks, reference counts, etc. etc.
The first month was perhaps the most exciting though. Learning Interface Builder and all of its settings, changing xcode configurations to see what each of them did (and learning why it was a bad idea to do so haha…), learning the objective c syntax and coding structure/principals. All of this had me feeling like iOS development was going to be a freaking breeze.
Then one day, I ended up needing to store data – it was a week later that everything changed. I started out with storing what I wanted in the typical beginner area – a custom plist file. I then began working my way into SQLite. Things for a PHP developer are, up to this point, extremely familiar and easy to just bust some code out and go. That is until you get into Core Data.
When I first read up on Core Data, it related very closely to how Redis worked. Since I had worked with Redis and could appreciate the speed and performance one gets from a memory store, I immediately wanted to transition into this. I did some quick and dirty research on what others had to say about Core Data. To them, it “is easy”, and “the best way to store data you plan to use within iCloud” and blah blah blah… I mean, I read all of this and think “THIS is what I am going to use. So – where do we start?”.
At first glance, Core Data (and how it is represented in the docs) sounds so much like a database that you can’t help but start out by treating it like any other *sql DB you’ve dealt with before. Holy crap Batman – what a mistake that was. I think my first mistake was relying on books that “touched” on the subject to give me the understanding I needed for this vast subject matter. And I do mean, no shit, 3 out of 5 books will begin by telling you to think about the data model as a database, or even start relating terminology or the “data model” itself to how a database is structured and managed. At this stage, I had no idea how absolutely damaging this would be for me until it was far into the game…
My story to enlightenment brings us to my first, pretty accurate introduction to Core Data. This happened after watching a 4-part series video on the submit by Stanford University professor Paul Hegarty (part of the CS193p course videos available for Free to watch). After watching these videos 3 or 4 times each, I found myself feeling less and less confused about what Core Data was, how to use UIManagedDocument.
UIManagedDocument was and is still (at the time of this writing), one of the least covered subjects on the net – something I’m very puzzled by considering just how powerful it is. While I feel like I know UIManagedDocument pretty well at this point, this was just one more subject that was going to tortue me for a full month (no exaggeration here) due to the lack of resources you can find on the subject.
When a developer finds themselves getting confused this many times trying to understand what the “community” says is an “easy” concept, either they will stop here and just say “this language/feature/etc. sucks” and never use it again – or, they will embrace the fact that they just don’t have the proper understanding of the concept and seek out how to correct that understanding.
I chose the later of these outcomes. Why? Because I decided I had 2 possible outcomes of doing this. Either a) I’ll eventually realize that the first path (this sucks, never use it again) is actually the more accurate answer after all, or b) I’ll actually learn where I went wrong, how I went wrong, and how I can get on the right path (and maybe even be better at the language because of it).
Surprise surprise if I didn’t actually get better at the language.
If you paid any attention what-so-ever to the “miles long” list of links I posted before this entry, you’ll notice a helluva lot of links relating to Core Data. What I found is that to actually understand Core Data, you have to understand the other pieces of the language as well. For any PHP developer out there saying “uh, what? Why? ScReW THAT!” – hear me out first. The reasoning behind this is that Core Data (if you want to get to the real meaning behind is) is Objective C. While it is true that the “results” of Core Data is stored into a persistent store like SQLite, the data itself is actually nothing more than the same “objects” you create by defining a class.
You see, Core Data is nothing more than a collection of data in the form of Objects. These objects have properties that we use for storing our data, and also data that allow us to “link” objects together through “relationships” (much like inheritance in class structures). But, before I make the hardcore Objective C nerds begin foaming at the mouth, I’ll leave it alone there. Mainly because the point isn’t what Core Data is. The point is that I had finally learned that it is only as much like MySQL as a custom class I create is.
I’m doing a really crappy job of explaining this, but that crappiness is in itself indicative of exactly how I felt when my mind began trying to fight with what I had originally learned about the subject versus what was the reality of what it truly was.
After finally defeating the subject, I then began flying through the rest of iOS though. See, learning Core Data is important – not because of the community saying “its the best”, but because what you learn when you do. By learning core data, I forced myself to learn how strong/weak references matter, how/where/when to use GCD (Grand Central Dispatch), why/how to use notifications, how/why/where to pass objects around, how controllers and views behave, how UITableViewController and UIViewController work, etc. etc. I mean, you learn so much from this that its ridiculous.
I’m going to say that this will need to be a 2 part serious here because I’m getting tired. I’ll finish this post in a followup (and hopefully start diving into the other areas of the language). For now, I’ll close with this:
If you’re starting out with Core Data – please, under no circumstances, do NOT approach it like it is a MySQL or similar Database. Building the model itself is as close to MySQL you will ever get. But even then, it isn’t exact (you’ll see what I mean when you dive into concepts such as to-many and many-to-many relationships). Do yourself a favor and only think about it in Object terms. What do you do if you need to extend an object ? – you extend/subclass or, in core data terms, build a relationship to that object.
Second, get ready to really get an ass whooping if you haven’t fully understood the Objective C language yet. With that in mind, there were 2 things that the community got wrong: 1) they said Objective C is hard. Well, I’m not sure about everyone else, but Objective C was easy to pick up for me. The other is that Core Data is easy – its not. Core Data is complex and is extremely difficult if you don’t fully understand the language and aren’t thinking about it as an Object Warehouse!!
Finally, learn how to use UIManagedDocument. Perhaps I can write a short tutorial on it, but trust me – it makes working with Core Data easier. All because it is easier doesn’t make it better – but for me, I’d rather not have to deal with the unnecessary maintenance of working with Core Data if I don’t have to – plus, if you’re looking to ever start allowing your user’s local data you’re storing have the ability to be stores in iCloud, UIManagedDocument is, by far, the easiest way to go.
For now, I’m happy that I decided to learn this crazy, powerful language. The unfortunate fact is that (as of right now), Apple is making some poor choices with how they are handling things since Mr. Job’s death. They’re going right back into the world of “we’er apple. we don’t have to try, because you already know who we are, and why we’re great.”. That’s a horrible thing for Objective C / iOS developers. It’s horrible, too, for me because I had high hopes. I mean, I’ve embraced Objective C and really want to keep writing it. But for me, I’m going to say I’m very uncertain about the future for this language. Not because of anything negative for the language, of course, but because of its manager. I’m already beginning now to pick up Java/Android development. I’ll still write Objective C, but when I have doubts about a language… yeah, we’ll see.
Anyways, with that, see you all in a month or 2. I’m sure that we’ll see real quick if the tides are indeed going to change on that front. Who knows after all…