UITableViewController & Core Data

There’s progress! Check it out:

UITableView & Core Data

Might not look like much but thats a UITableViewController inside a UINavigationController inside a UITabBarController, driven by a Core Data database using the NSFetchedResultsController. Lots of big words… And I’d like to say that the concepts behind them are simple but some of them aren’t. I mean once you understand them they’re fine, like with anything, but these were a bit harder to learn than the previous ones (such as UIView). I’m talking about Core Data, the rest are pretty straightforward. You see you need to architect your database first, setting up its attributes and relationships. You then get a special handle to your database (an NSManagedObjectContext) which is actually another controller that sits in between your controller and the actual database itself, and maintains a sort of scratchpad for your database. You then formulate fetch requests which are a structured way of saying “Yo database! Insert this info into x”, which you pass to the NSManagedObjectContext instance. If you want the changes you made to the NSManagedObjectContext scratchpad committed to the actual database you call the save: method on it. Thats not all though. The tricky part is the NSFetchedResultsController, which is another layer that sits in between the scratchpad and your controller. And that one is useful because it allows you to execute queries on your db in batches rather than fetching the whole db at once, which could be 10k items…imagine how long that would freeze the UI for (assuming the whole db even fit in memory). Once you get the hang of it it’s pretty cool because it has methods which correspond to the UITableViewController datasource methods–i.e. when the UITableViewController asks you for a cell at an NSIndexPath, you have a similar method that you then call on the NSFetchedResultsController which returns you an object for an NSIndexPath. Lots of controller layers and confusing at first but it’s pretty powerful and very enabling. Well at least thats how I think it works, I’ll probably gain a better understanding once I run into (and sort) some compile- and/or run-time errors. :)

On a lighter note, just went for a swim and a bike ride. Exercise is good when you’ve been staring at a computer for 6 hours straight. There’s a few more things I need to cover such as Mapkit, the GPS, accelerometer and a few more and then I might finally be able to start making some proper apps. I’ve got lots of ideas and can’t wait to finally realise them. Hopefully I’ll make it that far.

Luka

Advertisement
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.