MacRuby as a CoreData playground

December 4th, 2009  |  Published in MacRuby, Ruby, iPhonedev

Lately I’ve been working to optimize a Core Data based iPhone app. Core Data has generally been a pleasure to work (along with great developer support from Apple), but as soon as I’ve needed to dig in to work with more complex (read: slow) queries, a few cracks appeared. For example, improper predicates don’t crash or return errors: they just never return. Factor in compile / run / get the app to the right spot, and the debug cycles are painful.

So eyeing (and looking for a productive reason to play/learn…) the latest MacRuby build I thought it might be useful to see if I couldn’t iterate with Ruby when testing more complex predicates to eliminate the entire compile/run phase. After a bit of fiddling, I was able to get this to work quite nicely.

Here is some basic Ruby for use with macirb to illustrate this technique…

framework 'cocoa'
 
modelURL = NSURL.fileURLWithPath('MyCoreData.mom')
storeURL = NSURL.fileURLWithPath('MyCoreData.sqlite')
mom = NSManagedObjectModel.alloc.initWithContentsOfURL(modelURL)
psc = NSPersistentStoreCoordinator.alloc.initWithManagedObjectModel(mom)
ps = psc.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil, error: nil)
moc = NSManagedObjectContext.alloc.init
moc.setPersistentStoreCoordinator(psc)
 
fetch = NSFetchRequest.alloc.init
fetch.setEntity(NSEntityDescription.entityForName('Employee', inManagedObjectContext: moc))
fetch.setFetchLimit(25)
results = moc.executeFetchRequest(fetch, error: nil)

Quick-and-dirty web service: Sinatra rocks it.

November 22nd, 2009  |  Published in Dev, Ruby

I’ve not done much web development over the last few years, other than some random PHP and old school ASP back in the day.  I’ve been doing desktop and more recently mobile development, which I like quite a bit.

However, I’ve been playing around with web stuff recently, under the tutelage and influence of a few solid web devs I respect, and I’m having a lot of fun with it.  At my new gig, we don’t yet have a CIT/nightly builds server setup yet, so I decided to toss together a web service to do on-demand builds for our project manager. So it’s a win:win – a useful down-and-dirty in-house utility and I get a chance to play around with some interesting stuff.

Since I really like ruby, I decided to write this web service using Sinatra.  I took a bit of time to learn Sinatra and took a slight detour into HAML for formatting HTML output.  Even with the learning curve associated with these items (I’m an intermediate with the Ruby language itself) this side project was trivial.  As a matter of fact, without these technologies, I probably wouldn’t have even messed with doing such a throw-away task – I would have waited until we had a ‘real’ build system in place.

Here’s a simplified version of the code…

require 'rubygems'
require 'sinatra'
require 'haml'
 
IPHONE_ROUTE = '/latest-iphone.zip'
ANDROID_ROUTE = '/latest-android.zip'
 
IPHONE_PROJ_DIR = 'zagat-iphone'
ANDROID_PROJ_DIR = 'zagat-android'
 
BUILD_LOG_FNAME = 'build.txt'
 
get '/' do
  @entries = Array.new
  Struct.new("Entry", :href, :description)
  @entries << Struct::Entry.new(IPHONE_ROUTE,'Latest iPhone')
  @entries << Struct::Entry.new(ANDROID_ROUTE, 'Latest Android')
  haml :index
end
 
get IPHONE_ROUTE do
  File.delete(BUILD_LOG_FNAME) if File.exists?(BUILD_LOG_FNAME)
  log = File.new(BUILD_LOG_FNAME, 'w+')
 
  # get source - if we've never got the source before, let's get it now, otherwise, update
  if !File.exists?(IPHONE_PROJ_DIR)
    log << `hg clone foo`
  else
    log << `hg pull  bar`
  end
 
  # build
  if File.exists?(IPHONE_PROJ_DIR)
    Dir.chdir(IPHONE_PROJ_DIR)
    log << `xcodebuild -sdk iphoneos3.0 -configuration "Ad Hoc"`
 
    # create the zip file
    Dir.chdir('build/Ad Hoc-iphoneos')
    log << `zip -r ../../..#{IPHONE_ROUTE} *.app`     
    Dir.chdir('../../..')   
  else     
    puts 'Build failed, go bug someone for support'     
    log.close     
    return   
  end   
 
  log.close   
  # add the log file to the zip   
  `zip .#{IPHONE_ROUTE} #{BUILD_LOG_FNAME}`   
  # return the file   
  send_file( ".#{IPHONE_ROUTE}", :type => 'application/zip' )
end

OS Boot Times: I don’t care

November 21st, 2009  |  Published in Rant

So every time I see OS reviews inevitably the focus turns to boot times. Since I use OS X, I only rarely turn off my computers (desktop or laptop) – I just put them to sleep. About the only time I boot them is when there is an software update that requires it.

I come in to work, hook the MBP to an external monitor, hit a key, and off I go within a couple of seconds.

When I’m mobile, I open the laptop and again, off I go.

Shutdown / reboot cycles are just so old school and … irrelevant to me.

Is it just me? Is this really important to folks, or is this just a habit of reviewers from the era when resume / power management was non-existent / primitive?

Tags: ,

New to OS X: Developer Quick Hits

November 19th, 2009  |  Published in OSX  |  1 Comment

I have a web (Drupal) dev friend who recently switched to OS X so I wrote this streams-of-consciousness guide for him to help him during his “swimming in glue” transition period. Others have said I should toss it up as google fodder, so here it is in it’s unedited and gory detail.

Common keystrokes

  • ⌘ ==> Command key, your main shortcut key used by everything.  The Windows or Super key on non-mac keyboards
  • ⌥ ==> Option key, aka Alt
  • ^  ==> Control
  • ↑  ==> Shift
  • ⌘⌥D toggles the Dock (I leave it pretty much dismissed at all times)
  • ⌘↑Delete takes out the trash
  • ^A, ^E emacs bindings are everywhere for Home/End of line love
  • ^Up, ^Down for page up / page down
  • ⌘Up, ⌘Down for Top / Bottom of ‘document’
  • ⌘S Save file
  • Great Apple article concerning Keyboard Shortcuts:  http://support.apple.com/kb/HT1343

Quicksilver is your friend.  This is an app launcher app that’s The Shiz.  You will know you have graduated to OS X power user when you have no apps in the dock and everything is started from Quicksilver
Grab it here:  http://www.blacktree.com/ (be sure to get the one for 10.6)  It’s been wonky in 10.6, so surf around if you have problems.

Editors

Internet Utils

  • Defacto FTP client is the venerable Cyberduck:  http://cyberduck.ch/ (grab the version for Snow Leopard)
  • colloquy – Solid IRC client http://colloquy.info/

Misc Utils

Misc OS X stuff (more keyboard friendly stuffs)

  • Exposé.  Press F3 or 4 fingers down gesture on the trackpad.  Exposé rules for managing tons of windows.
  • ⌘Tab – just like the Alt+Tab of windows.  You can also do an Exposé via ⌘Tab by pressing Down when you select the app you want.
  • Spaces.  Virtual desktops, and this feature works well with Exposé. I bind both Spaces and Exposé to different (side) buttons on my mouse.
  • Address Book can sync with Google. ⌘, to pull up preferences (this is an OS X standard keystroke for all app prefs, btw).
  • ⌘H ==> Hide This Window  ⌘⌥H ==> Hide All Other Windows.  The biggest difference to using OS X effectively is going native with how it deals with windows in general (vs Gnome/MS Windows where the app == window).  It will feel “weird” for a while until this stuff becomes habitual.  The Hide thing is cool.  Hide + ⌘Tab is a fast way to manage windows.  Experiment to find your own groove…

Windows 7. OS X. Parallels. Hours Lost.

November 17th, 2009  |  Published in Rant  |  1 Comment

I need Windows for some minor BlackBerry development I’ve been doing at work.

Ok, fine, let’s go buy Windows 7 and run it under bootcamp.  Well, yeah, but I wanna VM it, since I don’t want to really live in Windows 7, just visit.  I mean, all my stuff is … over there.

So off I go to a dev (BlackBerry, ugh) conference and when I get back I’m tasked with modding some BB code which requires me to fire up Parallels.  Groovy.

Windows7 is whining it needs to be (re)activated.  Yeah, ok, I get that, all this back and forth business has the activation server confused, fine.  I go through the online process only to be denied:  please call our automated line.

Ugh, I hate those things.  Fine, I call.

I put in all these numbers only to be told the automated system can’t help me and it needs to have me chat with a human.  UGH.  So, Ms Natchagonehdumauhchna asks for the numbers off the (failed) activation screen and I dutifully bark them back.

“I’m sorry, we cannot help you, call the retailer you bought your copy of windows from.”

Stunned F-ing Silence.

Are you kidding me?

I’m completely certain the dude at Staples isn’t going to be able to help ACTIVATE YOUR EMPLOYER’S PRODUCT. And I kinda get from the tone that your implying I stole this.  Nooo, I bought the thing with good ole fashion plastic and intend to expense it to my company.

I just don’t have time for all this mess, so I google around to some windows fan site and hack it to reset the stinking Doomsday Timer.  I suppose I deal with this later.  This is a VALID COPY OF WINDOWS.  I’M TRYING TO DO THE RIGHT THING HERE.

Alas, I don’t have time to jack with this nonsense.  And now I’ve wasted more time with my need to dump this rant upon the universe.