<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3059468813255868046</id><updated>2011-10-12T11:08:40.838+01:00</updated><category term='celko'/><category term='cheat sheet'/><category term='proxy'/><category term='jdbc'/><category term='concurrenct'/><category term='uialert'/><category term='key shortcuts'/><category term='joshua bloch'/><category term='last.fm'/><category term='maven'/><category term='mvn'/><category term='dynamic proxy'/><category term='objective c'/><category term='private properties'/><category term='lion'/><category term='osx'/><category term='threading'/><category term='sleep'/><category term='inheritance'/><category term='haltonisms'/><category term='iphone'/><category term='spring'/><category term='nscondition'/><category term='afp'/><category term='maven3'/><category term='eclipse'/><category term='work'/><category term='thrift'/><category term='hibernate'/><category term='macfuse'/><category term='java'/><category term='seam'/><category term='nsproxy'/><category term='di'/><category term='nszombie'/><category term='command pattern'/><category term='programming'/><category term='sqlite'/><category term='maven2'/><category term='don&apos;t reinvent the wheel'/><category term='builder'/><category term='interface builder'/><category term='xcode'/><category term='sql'/><category term='clean code'/><category term='oo'/><category term='mac'/><category term='macfusion'/><category term='optimization'/><category term='pattern'/><category term='fail'/><category term='j2ee'/><category term='whoami'/><category term='ioc'/><category term='profile'/><title type='text'>Swish Movement</title><subtitle type='html'>Geeky ramblings of a Java Server Side developer.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>18</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-5497625181182097753</id><published>2011-08-22T21:16:00.011+01:00</published><updated>2011-08-22T21:56:21.710+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='sleep'/><category scheme='http://www.blogger.com/atom/ns#' term='osx'/><category scheme='http://www.blogger.com/atom/ns#' term='mac'/><category scheme='http://www.blogger.com/atom/ns#' term='lion'/><title type='text'>Update: Lion sleep woes solved</title><content type='html'>&lt;p&gt;In my &lt;a href="http://swish-movement.blogspot.com/2011/08/more-osx-lion-woes.html"&gt;previous post&lt;/a&gt; I was complaining how my MacBook would no longer sleep. The sleep function worked well after installing Lion but then stopped working unexpectedly.&lt;br /&gt;It turns out that this is not an uncommon problem with many people complaining of the same issue. Advice on how to fix it varies wildly with solutions including (but not limited to): SMC reset, PRAM reset, turn off preference X, delete folder Y - there are even &lt;a href="http://www.dragonone.com/products/macosx/pleasesleep/"&gt;3rd party application&lt;/a&gt; to force your Mac to sleep.&lt;/p&gt;&lt;p&gt;However the &lt;a href="https://discussions.apple.com/message/15790642#15790642"&gt;advice that worked for me&lt;/a&gt; was actually fairly logical:&lt;/p&gt;&lt;b&gt;1.&lt;/b&gt; Use &lt;tt&gt;pmset&lt;/tt&gt; to view the power management settings: &lt;br /&gt;&lt;pre&gt;localhost:work teabot$ pmset -g&lt;br /&gt;    ...&lt;br /&gt;    disksleep	10&lt;br /&gt;&lt;b&gt;    sleep		0 (imposed by 46)&lt;/b&gt;&lt;br /&gt;    hibernatemode	3&lt;br /&gt;    ...&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;The interesting thing in the output is the line that states '&lt;tt&gt;sleep 0&lt;/tt&gt;' - presumably indicating that sleep mode is disabled. Further along this line we find '&lt;tt&gt;imposed by 46&lt;/tt&gt;'. This is actually telling us the id of the process (PID) that is blocking sleep mode.&lt;/p&gt;&lt;b&gt;2.&lt;/b&gt; List the process with the matching PID: &lt;br /&gt;&lt;pre&gt;localhost:work teabot$ ps -ef | grep 46&lt;br /&gt;    0  &lt;b&gt;46&lt;/b&gt;  1 0 9:05pm ??  0:00.16 /usr/sbin/cupsd -l&lt;br /&gt;    0 107 &lt;b&gt;46&lt;/b&gt; 0 9:05pm ??  0:00.59 ipp://nano.local.:631/printers/HP_Photosmart_C4100_series 7...&lt;br /&gt;    ...&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;So we can see that a queued print job is inhibiting sleep mode and in this case I resolved the issue by clearing my print queue at which point my MacBook was happy to sleep once more.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;It's pretty bizarre that a queued print job should disable sleep mode. The action of me closing the MacBook lid should make clear my intention of wanting the computer to sleep. If the print job is so important - perhaps a warning dialog should be issued so that I might have some idea why sleep mode will not be activated?&lt;/p&gt;&lt;p&gt;I assume that other types of process may inhibit sleep mode. Thankfully the &lt;tt&gt;pmset&lt;/tt&gt; command includes some unexpectedly useful output to help us figure out which process is responsible. I can't imagine how long it would've taken me to find the errant print job without it.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-5497625181182097753?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/5497625181182097753/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2011/08/update-lion-sleep-woes-solved.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/5497625181182097753'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/5497625181182097753'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2011/08/update-lion-sleep-woes-solved.html' title='Update: Lion sleep woes solved'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-6754504950332487806</id><published>2011-08-22T08:22:00.007+01:00</published><updated>2011-08-22T10:20:10.741+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='fail'/><category scheme='http://www.blogger.com/atom/ns#' term='osx'/><category scheme='http://www.blogger.com/atom/ns#' term='lion'/><title type='text'>More OSX Lion woes</title><content type='html'>I having more small yet significant problems with OSX Lion. I'm running the latest version - 10.7.1 - on both my MacPro and MacBook.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;My MacPro:&lt;/b&gt;&lt;br /&gt;I find that I frequently lose the ability to resolve DNS names. This is only resolved by restarting the machine. Some Googling suggests that this issue may be due to a failing &lt;tt&gt;mDNSResponser&lt;/tt&gt; process. I use this machine as a media server, so in practice I get up in the morning and try to tune some Internet radio only to find that I need to reboot my media server before I can do so. I'm going to try killing the process so that I don't have to restart the machine each time. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;My MacBook:&lt;/b&gt;&lt;br /&gt;Sometimes my MacBook just won't sleep. Closing the lid, Cmd-Alt-Eject, and the Apple menu option have no effect. It's really frustrating. Some people report that this can happen if Internet Sharing is enabled - it's not in my case. Apple's 'sleep' implementation used to be its crowning jewel. Coupled with OSX's stability I could: go months without rebooting my laptop, have a working system within seconds of opening the lid, happily sling my MacBook into a bag just moments after shutting the lid. I can't anymore...&lt;br /&gt;&lt;br /&gt;When I wake this machine from sleep - when I can actually get it to sleep that is, I often find that the wireless card is in an unknown state. This makes it impossible to connect to a wireless network or switch the wireless off. The machine seems to sort itself out after a few minutes but it's quite annoying - especially if I'm trying to look something up in a hurry.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Summary:&lt;/b&gt; With this extremely poor OSX Lion release Apple has reminded us what it was like to run Windows XP on a cheap laptop in 2002.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-6754504950332487806?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/6754504950332487806/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2011/08/more-osx-lion-woes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/6754504950332487806'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/6754504950332487806'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2011/08/more-osx-lion-woes.html' title='More OSX Lion woes'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-7389359765693194746</id><published>2011-08-10T15:46:00.006+01:00</published><updated>2011-08-22T22:00:40.945+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='thrift'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='maven2'/><category scheme='http://www.blogger.com/atom/ns#' term='mvn'/><category scheme='http://www.blogger.com/atom/ns#' term='profile'/><category scheme='http://www.blogger.com/atom/ns#' term='inheritance'/><category scheme='http://www.blogger.com/atom/ns#' term='maven3'/><category scheme='http://www.blogger.com/atom/ns#' term='maven'/><title type='text'>Inheriting Maven profiles</title><content type='html'>I'm in the process of moving a number of projects from Ant/&lt;a href="http://ant.apache.org/ivy/"&gt;Ivy&lt;/a&gt; to Maven. Many of our projects use &lt;a href="http://thrift.apache.org/"&gt;Thrift&lt;/a&gt; and we use a &lt;a href="http://maven.davidtrott.com/repository/org/apache/thrift/tools/maven-thrift-plugin/maven-metadata.xml"&gt;Maven Thrift plugin&lt;/a&gt; to conveniently compile our Thrift IDL. Given that all of our Thrift projects have the same setup, the plugin is declared in a our parent POM.&lt;br /&gt;The plugin is declared in a profile that is only activated when a thrift source directory is present:&lt;br /&gt;&lt;pre&gt;  &amp;lt;profile&amp;gt;&lt;br /&gt;    &amp;lt;id&amp;gt;uses-thrift&amp;lt;/id&amp;gt;&lt;br /&gt;    &amp;lt;activation&amp;gt;&lt;br /&gt;      &amp;lt;file&amp;gt;&lt;br /&gt;        &amp;lt;exists&amp;gt;${basedir}/src/main/thrift&amp;lt;/exists&amp;gt;&lt;br /&gt;      &amp;lt;/file&amp;gt;&lt;br /&gt;    &amp;lt;/activation&amp;gt;&lt;br /&gt;    &amp;lt;build&amp;gt;&lt;br /&gt;      &amp;lt;plugins&amp;gt;&lt;br /&gt;        &amp;lt;plugin&amp;gt;&lt;br /&gt;          &amp;lt;groupId&amp;gt;org.apache.thrift.tools&amp;lt;/groupId&amp;gt;&lt;br /&gt;          &amp;lt;artifactId&amp;gt;maven-thrift-plugin&amp;lt;/artifactId&amp;gt;&lt;br /&gt;          ...&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;I actually have a number of plugins defined in such profiles so that they become active only when relevant source artefacts are present in the sub-project.&lt;/p&gt;&lt;p&gt;This all &lt;span style="font-weight: bold;"&gt;looks&lt;/span&gt; good, but in practice I found that the profiles were never activated in sub-modules that were being built from a parent project. It was as if the activation state was determined at the parent level, not on a per module basis.&lt;/p&gt;&lt;p&gt;It seems that profile inheritance does function as I expected in Maven 2. It does however work perfectly with Maven 3.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-7389359765693194746?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/7389359765693194746/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2011/08/inheriting-maven-profiles.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/7389359765693194746'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/7389359765693194746'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2011/08/inheriting-maven-profiles.html' title='Inheriting Maven profiles'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-6288618208178961326</id><published>2011-07-22T08:40:00.006+01:00</published><updated>2011-07-22T09:15:59.659+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='macfuse'/><category scheme='http://www.blogger.com/atom/ns#' term='afp'/><category scheme='http://www.blogger.com/atom/ns#' term='osx'/><category scheme='http://www.blogger.com/atom/ns#' term='lion'/><title type='text'>AFP failing between OSX Lion machines</title><content type='html'>I recently upgraded both my MacPro and MacBook to Lion. The upgrades went fine, but I now find that I cannot open a non-guest AFP connection between the two machines. On the client I see '&lt;b&gt;Connection Failed...&lt;/b&gt;' and in the server console I find:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;22/07/2011 08:39:20.350 AppleFileServer: _Assert: /SourceCache/afpserver/afpserver-581/afpserver/AFPRequest.cpp, 2005&lt;br /&gt;22/07/2011 08:39:20.354 AppleFileServer: _Assert: /SourceCache/afpserver/afpserver-581/afpserver/AFPRequest.cpp, 2005&lt;br /&gt;22/07/2011 08:39:20.400 AppleFileServer: Calling ReadLock on lock &lt; 0x2a833128 &gt; while holding WriteLock&lt;br /&gt;22/07/2011 08:39:20.524 ReportCrash: DebugSymbols was unable to start a spotlight query: spotlight is not responding or disabled.&lt;br /&gt;22/07/2011 08:39:20.965 com.apple.launchd: (com.apple.AppleFileServer[424]) Job appears to have crashed: Illegal instruction: 4&lt;br /&gt;22/07/2011 08:39:21.062 ReportCrash: Saved crash report for AppleFileServer[424] version ??? (???) to /Library/Logs/DiagnosticReports/AppleFileServer_2011-07-22-083921_localhost.crash&lt;br /&gt;22/07/2011 08:39:22.389 com.apple.launchd: (com.apple.AppleFileServer) Throttling respawn: Will start in 8 seconds&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I also have a &lt;a href="http://pastebin.com/Q1mv5PRV"&gt;crash report&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;This is fairly annoying as my MacPro acts as the Time Machine backup for my MacBook. Has anyone experienced anything similar and are there any workarounds?&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Update:&lt;/b&gt; I found a work around: I removed all folders from 'System Preferences/Sharing/File Sharing/Shared folders' and I can now connect. The unfortunate side effect of this workaround is that now the whole file system on the server is shared - subject to the authenticated user's privileges.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-6288618208178961326?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/6288618208178961326/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2011/07/afp-failing-between-osx-lion-machines.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/6288618208178961326'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/6288618208178961326'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2011/07/afp-failing-between-osx-lion-machines.html' title='AFP failing between OSX Lion machines'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-6219702464503634442</id><published>2011-05-24T18:19:00.055+01:00</published><updated>2011-05-25T16:04:03.975+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='pattern'/><category scheme='http://www.blogger.com/atom/ns#' term='joshua bloch'/><category scheme='http://www.blogger.com/atom/ns#' term='builder'/><title type='text'>An ordered builder pattern</title><content type='html'>&lt;div&gt;I was recently implementing the &lt;a href="http://en.wikipedia.org/wiki/Builder_pattern"&gt;builder pattern&lt;/a&gt;&amp;nbsp;for some immutable objects and was taking the approach described by Joshua Bloch in his excellent book '&lt;a href="http://java.sun.com/docs/books/effective/"&gt;Effective Java&lt;/a&gt;'. I particularly like the readability that the pattern provides when constructing complex objects and I think this arises because each property value is contextualized by the preceding method name:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family:'courier new';font-size:small;"&gt;&amp;nbsp;&amp;nbsp;Person.Builder builder = new Person.Builder();&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family:'courier new';font-size:small;"&gt;&amp;nbsp;&amp;nbsp;Person person = builder.id(10).name("Jim").age(21)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family:'courier new';font-size:small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.pets("cat", "dog").build();&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;I also like working with builders in an IDE because the auto-complete process can nicely lead you through the object's construction with a few taps of &lt;i&gt;Ctrl+Space&lt;/i&gt;. However, there are a few things that could be improved with the pattern:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Handling of mandatory state&lt;/b&gt;&lt;/div&gt;&lt;div&gt;There are two obvious ways we can enforce mandatory properties with the pattern. Firstly we could pass mandatory property values in the builder constructor. This gives us a compile-time enforcement of mandatory properties. However, for many mandatory properties it can suffer from the problem that the pattern was meant to avoid - telescoping constructors. Imagine that the &lt;span style="font-family: 'courier new'; font-size: small; "&gt;id&lt;/span&gt;&amp;nbsp;and &lt;span style="font-family: 'courier new'; font-size: small; "&gt;name&lt;/span&gt;&amp;nbsp;are mandatory:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span style="font-family:'courier new';font-size:small;"&gt;&amp;nbsp;&amp;nbsp;Person.Builder builder = new Person.Builder(1, "Jim");&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family:'courier new';font-size:small;"&gt;&amp;nbsp;&amp;nbsp;Person person = builder.age(21).pets("cat", "dog").build();&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;The second way we can enforce mandatory properties is by checking the builder state in the &lt;/span&gt;&lt;span style="font-family: 'courier new'; font-size: small; "&gt;build()&lt;/span&gt;&amp;nbsp;method. This preserves the readablility API but relegates the mandatory property checking to run-time - quite a penalty.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Redundant invocation paths&lt;/b&gt;&lt;/div&gt;&lt;div&gt;Normally a property setter method on a builder just returns the builder instance:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family:'courier new';font-size:small;"&gt;&amp;nbsp;&amp;nbsp;public Builder id(int id) {&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family:'courier new';font-size:small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.id = id;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family:'courier new';font-size:small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return this;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family:'courier new';font-size:small;"&gt;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;This means that on the returned value we can call any other builder method, including the one that we just invoked. We could - if we wanted to - chain many redundant calls that overwrite the builder state:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family:'courier new';font-size:small;"&gt;&amp;nbsp;&amp;nbsp;Person person = builder.id(10).id(11).id(12).id(13).build();&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family: 'courier new'; "&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;While this isn't a likely to occur, it shows that the path we take through the building of the object isn't incredibly directed - it would be nice if we couldn't set the &lt;span style="font-family: 'courier new'; font-size: small; "&gt;id&lt;/span&gt;&amp;nbsp;more than once. We could do this at run-time but it would be far better if we were prevented from even writing such code! This lack of order also degrades our IDE auto-complete experience a little - imagine a builder with 20 properties - every time we see the auto-complete suggestions we have to mentally parse a list of 20 property setters - including those we've already set.&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Adding order to the builder pattern&lt;/b&gt;&lt;/div&gt;&lt;div&gt;I had a go at addressing these (minor) short comings of the pattern and with some simple additions was able to create a builder that can lead you through the building process and enforce mandatory properties at compile-time in a nice contextualized way. The trick is to create a number of subordinate builder classes.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Consider an immutable value class:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="font-family:'courier new';font-size:small;"&gt;&amp;nbsp;&amp;nbsp;public class Person {&lt;/div&gt;&lt;div style="font-family:'courier new';font-size:small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private final int id;    // MANDATORY&lt;/div&gt;&lt;div style="font-family:'courier new';font-size:small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private final String name; // MANDATORY&lt;/div&gt;&lt;div style="font-family:'courier new';font-size:small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private final int age;  // OPTIONAL&lt;/div&gt;&lt;div style="font-family:'courier new';font-size:small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private final Set&lt;string&gt;&amp;nbsp;pets; // OPTIONAL&lt;/string&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="font-family:'courier new';font-size:small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Now let's deal with the mandatory fields. It makes sense that you must first set the &lt;span style="font-family: 'courier new'; font-size: small; "&gt;id&lt;/span&gt;&amp;nbsp;property before doing anything else. Therefore the only possible method invocation from our builder class will be &lt;span style="font-family: 'courier new'; font-size: small; "&gt;id(int id)&lt;/span&gt;&amp;nbsp;- but what should this return? The next step in our builder sequence is to set the mandatory name, therefore we should return a builder class on which you can only invoke the &lt;span style="font-family: 'courier new'; font-size: small; "&gt;name(String name) &lt;/span&gt;method, so we defined a subordinate builder class &lt;span style="font-family: 'courier new'; font-size: small; "&gt;NameRequiredFieldBuilder&lt;/span&gt;, that only allows the name to be set and return this from the invocation of &lt;span style="font-family: 'courier new'; font-size: small; "&gt;id(int id)&lt;/span&gt;:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family:'courier new';font-size:small;"&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;public static class Builder {&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public NameRequiredFieldBuilder id(int id) {&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.id = id;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return requiredName;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family: 'courier new'; font-size: small; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family:'courier new';font-size:small;"&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public static class NameRequiredFieldBuilder {&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private final Builder builder;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private NameRequiredFieldBuilder(Builder builder) {&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.builder = builder;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public &lt;b&gt;&lt;i&gt;?&lt;/i&gt;&lt;/b&gt;&amp;nbsp;name(String name) {&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;builder.name = name;&lt;/div&gt;&lt;/div&gt;&lt;div style="font-family:'courier new';font-size:small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return &lt;b&gt;&lt;i&gt;?&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family: 'courier new'; font-size: small; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;So we can now change the builder invocations like so: &lt;span style="font-family: 'courier new'; font-size: small; "&gt;builder.id(10).name("Jim") &lt;/span&gt;- in fact we no option but to call these methods, in this order. Let's deal with providing the builder with the the optional fields. At this point we'd like to be able to call &lt;span style="font-family: 'courier new'; font-size: small; "&gt;age(int age)&lt;/span&gt;, &lt;span style="font-family: 'courier new'; font-size: small; "&gt;pets(String... pets)&lt;/span&gt;, or &lt;span style="font-family: 'courier new'; font-size: small; "&gt;build()&lt;/span&gt;. and we'd like to do this using the object returned by &lt;span style="font-family: 'courier new'; font-size: small; "&gt;name(String name)&lt;/span&gt;. To achieve this we define a subordinate builder class for the optional fields and the &lt;span style="font-family: 'courier new'; font-size: small; "&gt;build()&lt;/span&gt;&amp;nbsp;method:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: 'courier new'; font-size: small; "&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;public static class OptionalFieldsBuilder {&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private final Builder builder;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private OptionalFieldsBuilder(Builder builder) {&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.builder = builder;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public OptionalFieldsBuilder age(int age) {&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;builder.age = age;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return builder.optionalFields;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public OptionalFieldsBuilder pets(String... pets) {&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for (String pet : pets) {&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;builder.pets.add(pet);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return builder.optionalFields;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public Person build() {&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return new Person(builder);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;}&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;An instance of &lt;span style="font-family: 'courier new'; font-size: small; "&gt;OptionalFieldsBuilder&lt;/span&gt;&amp;nbsp;is then returned by &lt;span style="font-family: 'courier new'; font-size: small; "&gt;NameRequiredFieldBuilder.&lt;/span&gt;&lt;span style="font-family: 'courier new'; font-size: small; "&gt;name(String name)&lt;/span&gt;:&lt;/div&gt;&lt;div&gt;&lt;span style="font-family: 'courier new'; font-size: small; "&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="font-family: 'courier new'; font-size: small; "&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;public OptionalFieldsBuilder name(String name) {&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;builder.name = name;&lt;/div&gt;&lt;/div&gt;&lt;div style="font-family: 'courier new'; font-size: small; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return optionalFields;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family: 'courier new'; font-size: small; "&gt;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family: 'courier new'; font-size: small; "&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;So now we can call the builder property setters for the optional fields and then finally call the method to build an object instance. &lt;a href="http://pastebin.com/CXyxh658"&gt;Full source code for this example&lt;/a&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Some observations&lt;/b&gt;&lt;/div&gt;&lt;div&gt;Breaking the builder into a number of subordinate builder classes allows us to introduce order into the build process. We can lead users of our API through the construction process, having them specify mandatory fields values first, only once, and potentially in order of importance. The modified pattern ensures that all mandatory fields are supplied at compile-time as it's not possible to invoke &lt;span style="font-family: 'courier new'; font-size: small; "&gt;build()&lt;/span&gt;&amp;nbsp;otherwise. The modified pattern does away with constructor supplied parameters and potential telescoping issues. Furthermore the subordinate builder classes limit method choices in our IDE's auto-complete mechanism to only the pertinent properties.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;We could introduce additional structure to the invocations of the setters of the optional properties, however this might lead to a proliferation of subordinate builder classes. I feel that there is no need to dictate the order in which optional properties are set, and indeed how many times they are set.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;From an implementation perspective, the overhead of creating subordinate builder classes for a few mandatory properties is small. A more scalable approach would be to use an annotation processor to generate the builder classes at compile time as had been done with Jan-Kees van Andel's &lt;a href="http://code.google.com/p/make-builder"&gt;make-builder&lt;/a&gt; tool.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-6219702464503634442?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/6219702464503634442/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2011/05/ordered-builder-pattern.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/6219702464503634442'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/6219702464503634442'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2011/05/ordered-builder-pattern.html' title='An ordered builder pattern'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-5819867398148504753</id><published>2011-05-24T18:08:00.004+01:00</published><updated>2011-05-24T18:18:14.525+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='last.fm'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='work'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Change of tack</title><content type='html'>It's been a while since I've developed anything for IOS and I'm not back working with Java again at &lt;a href="http://www.last.fm/user/teabot"&gt;Last.fm&lt;/a&gt;. I have been dabbling with C/C++ on some Arduino projects at the &lt;a href="http://wiki.hackspace.org.uk/wiki/London_Hackspace"&gt;London Hackspace&lt;/a&gt; - but other than that I'm pretty much a full-time Java developer. However, I really enjoyed writing about my programming experiences and have missed jotting my thoughts down on a this blog. Rather than start a new blog for general programming posts I've decided to repurpose this one given that it already contains some possibly useful programming content.&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Hopefully my (3) followers won't be too put out by this :-) I have updated the strap-line and description of this blog to reflect this new direction.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;For the record, the most successful Swish Movement post was that relating to &lt;a href="http://swish-movement.blogspot.com/2009/05/private-properties-for-iphone-objective.html"&gt;private methods in Objective-C &lt;/a&gt;- I still get comments on that post!&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-5819867398148504753?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/5819867398148504753/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2011/05/change-of-tack.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/5819867398148504753'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/5819867398148504753'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2011/05/change-of-tack.html' title='Change of tack'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-1752547753202769306</id><published>2009-09-09T10:22:00.003+01:00</published><updated>2009-09-09T11:04:35.875+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='macfusion'/><category scheme='http://www.blogger.com/atom/ns#' term='macfuse'/><category scheme='http://www.blogger.com/atom/ns#' term='osx'/><title type='text'>Accessing SSHFS within finder</title><content type='html'>I usually need to access remote systems using SSHFS and SFTP from my Mac. Unfortunately OSX Finder does not support this by default. To make my life a little easier I used MacFUSE and created a shell script to mount the relevant file system:&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;    #!/bin/sh&lt;br /&gt;    sshfs user@myhost.com:/aPath/ ~/Desktop/myhost.com.mount-point \&lt;/span&gt;&lt;/span&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;        -oauto_cache,reconnect,volname=myhost&lt;/span&gt;&lt;/span&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;However, this can be a little frustrating so I was very happy when a friend introduced me to &lt;a href="http://www.macfusionapp.org/"&gt;MacFusion&lt;/a&gt; - a GUI based client for mounting SSHFS volumes. Try it!&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-1752547753202769306?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/1752547753202769306/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2009/09/accessing-sshfs-within-finder.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/1752547753202769306'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/1752547753202769306'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2009/09/accessing-sshfs-within-finder.html' title='Accessing SSHFS within finder'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-8168038952874405349</id><published>2009-06-22T23:30:00.005+01:00</published><updated>2009-06-23T15:28:50.514+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='interface builder'/><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='ioc'/><category scheme='http://www.blogger.com/atom/ns#' term='di'/><category scheme='http://www.blogger.com/atom/ns#' term='seam'/><title type='text'>Dependency Injection with the iPhone SDK</title><content type='html'>&lt;div style="text-align: left;"&gt;Like many Enterpise Java developers - my first exposure to the &lt;a href="http://en.wikipedia.org/wiki/Inversion_of_control"&gt;Inversion of Control&lt;/a&gt; principle (IOC) was when applying the then radical Spring Framework some years ago. I believe that there was an IOC/DI micro container project being incubated at Apache Jakarta prior to Spring but it certainly didn't enjoy the same adoption rate.&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;We now find that we rely heavily on the &lt;a href="http://en.wikipedia.org/wiki/Dependency_injection"&gt;Dependency Injection&lt;/a&gt; provided by our containers and frameworks. They take the responsibility for the monotinous, repetative, and tedious creational and configuration concerns of our classes leaving with much cleaner code. With some small amount of configuration they create our objects, configure them with appropriate values, and wire then up - injecting references into dependent objects. As developers this makes out lives much easier - we're no longer writing lots of boiler plate code - knowing that the frameworks will take care of it for us. Indeed we find the IOC principal in the latest releases of the EJB specification and in popular frameworks such as &lt;a href="http://www.springsource.org/about"&gt;Spring&lt;/a&gt;, &lt;a href="http://seamframework.org/"&gt;Seam&lt;/a&gt; and &lt;a href="http://code.google.com/p/google-guice/"&gt;Guice&lt;/a&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;But what does this have todo with iPhone development? Since starting work on a large iPhone project I have mostly concentrated my efforts on improving the data access and business logic layers of our application. However, over time I began to move towards the view tier - refactoring some of our &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;UIViewContoller&lt;/span&gt;s. More often than not the existing view controllers where monolithic classes servicing quite complex views. The views themselves could be broken down into logical units so I worked hard to move the various responsibilities of the view controllers into seperate classes that then became composite members of a parent controller - for example:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;MyViewController&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;Became:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;MyParentViewController&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;MyListViewController&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;MyToolbarController&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;MyDetailViewController&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;While this neatly divided and encapsualted responsibility - it created many dependencies that needed to be satisfied. For example, the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;MyViewController&lt;/span&gt; would have to construct it's sub-controllers and pass in references to UI components such as &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;UIButton&lt;/span&gt;s etc. so that they could then be managed by the sub-controller object. To do of this I would have to write a lot of boring, error-prone code - passing view components in to object via &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;init&lt;/span&gt; method arguments or properties. As well as being a little tedious this also prevents the code being as clean as it could be.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Fortunately I was missing an iPhone SDK Inversion of Control trick. The Interface Builder application is commonly used to create views using a hierarchical view component model - a concept that I assume most are familiar with - you might draw a rectangular view, add a few buttons and arrange them etc. The magic begins when you specify which &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;UIViewController &lt;/span&gt;class should act as controller; Once the class has be set you can begin drawing connecting lines between the actual view components and the member variable names in that class - you describe you object dependencies using a visual metaphor - very cool! &lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_6YOYua-aQv4/SkDXIdta6gI/AAAAAAAAAKE/BpMIJdTJXOA/s1600-h/cool-association.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 305px;" src="http://2.bp.blogspot.com/_6YOYua-aQv4/SkDXIdta6gI/AAAAAAAAAKE/BpMIJdTJXOA/s400/cool-association.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5350512897730210306" /&gt;&lt;/a&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This object dependency configuration is stored in the view's XIB (nib) file and is used by the iPhone frameworks to instantiate and wire up your objects at runtime.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;While this is fairly intuitive for situations where we have views mapping to single monlithic controllers, it takes a little more work when we have composite controllers. In this scenario subsets of our view components might be managed by a class that is not and does not need to be a fully fledged &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;UIViewController&lt;/span&gt;. However, this sub-contoller class will be a member of a parent composite &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;UIViewController&lt;/span&gt; that pulls together multiple sub-controllers - each with their own distinct responsibilities for various view components within the larger view. Clearly, we would like to use Interface Builder to direct the instantiation of these sub-controllers and satisfy the referential dependencies between them, the parent controller, and the view components.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;We can do this in Interface Builder by using the 'Object' component from the component library. We create a new object for the NIB by dragging the Object component from the library. We can then specify the class of the object we wish to instantiate using the Inspector.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 238); -webkit-text-decorations-in-effect: underline; "&gt;&lt;img src="http://4.bp.blogspot.com/_6YOYua-aQv4/SkDcMyD6knI/AAAAAAAAAKM/X6w19hgm8Rc/s400/create-object.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5350518469470884466" style="display: block; margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto; text-align: center; cursor: pointer; width: 400px; height: 205px; " /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;However, this new object - created when the NIB is loaded - will be orphaned as it's not currently associated with any other objects. Lets assume that the parent controller may need to communicate with the sub-controller. In this event we need to create an &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;IBOutlet&lt;/span&gt; in the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;MyParentViewController&lt;/span&gt; class:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';font-size:small;"&gt;    @interface MyParentViewController : UIViewController  {&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';font-size:small;"&gt;    @private&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';font-size:small;"&gt;        IBOutlet MySubController* subController;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';font-size:small;"&gt;    ...&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;In addition to this lets assume that the responsibility of the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;MySubController&lt;/span&gt; includes something button related and hence must have a reference to a &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;UIButton&lt;/span&gt; in the view. In the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;MySubController&lt;/span&gt; class we need to create another &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;IBOutlet&lt;/span&gt; - this time for the button:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'courier new', -webkit-fantasy;"&gt;    &lt;/span&gt;@interface MySubController : NSObject {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'courier new', -webkit-fantasy;"&gt;    &lt;/span&gt;@private&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'courier new', -webkit-fantasy;"&gt;        &lt;span class="Apple-style-span"  style=" ;font-family:'courier new', fantasy;"&gt;IBOutlet UIButton* button;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'courier new', -webkit-fantasy;"&gt;    &lt;/span&gt;...&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Notice that &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;MySubController&lt;/span&gt; does not extend &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;UIViewController&lt;/span&gt; - it doesn't need to as it's responsibilities are small and may be focused purely on a subset of view components. We can now wire these objects up in Interface Builder:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 238); -webkit-text-decorations-in-effect: underline; "&gt;&lt;img src="http://1.bp.blogspot.com/_6YOYua-aQv4/SkDiOAD-G9I/AAAAAAAAAKU/zGP_ioKnSL4/s400/wire-up.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5350525087478848466" style="display: block; margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto; text-align: center; cursor: pointer; width: 400px; height: 241px; " /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span"&gt;Now when the NIB is loaded the sub-controller object will be instantiated and a reference injected into the parent controller - all without any code or additional effort. With this approach we can begin to break-up those monolithic view controllers into classes that adhere to the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt; so that our view controllers aren't doing 'too much'. In addition to this we can keep our classes nice and clean as we don't have to litter them with extra properties and &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;init&lt;/span&gt; method arguments to pass references to dependent objects. We can rely on the platform to satisfy these dependencies by injection according to the relationships we defined previously in Interface Builder.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;Clearly the above example does not live in the real world. However, I have found that for complex views an controllers the approach works extremely well.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-8168038952874405349?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/8168038952874405349/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2009/06/dependency-injection-with-iphone-sdk.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/8168038952874405349'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/8168038952874405349'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2009/06/dependency-injection-with-iphone-sdk.html' title='Dependency Injection with the iPhone SDK'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_6YOYua-aQv4/SkDXIdta6gI/AAAAAAAAAKE/BpMIJdTJXOA/s72-c/cool-association.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-2302236745786658357</id><published>2009-05-28T21:26:00.003+01:00</published><updated>2009-05-28T21:48:57.909+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='sql'/><category scheme='http://www.blogger.com/atom/ns#' term='sqlite'/><category scheme='http://www.blogger.com/atom/ns#' term='optimization'/><category scheme='http://www.blogger.com/atom/ns#' term='iphone'/><title type='text'>SQLite optimization on the iPhone</title><content type='html'>&lt;div&gt;Our current iPhone application makes quite heavy use of SQLite. We persist the model in the database using a Data Access Object pattern built on top of my own &lt;a href="http://swish-movement.blogspot.com/2009/05/revisiting-my-old-friend-jdbc-pt-1.html"&gt;JDBC like SQLite layer&lt;/a&gt; and it is all still quite lightweight. CoreData whould have been prefereable but we have to be compatible with iPhone SDK versions prior to 3.0. We also use the database to rank trends using indexed and weighted keywords and some additional temporal factors. Thankfully the text data we search through on the device is pre-indexed so we don't have to worry too much about getting suitable data into the tables. However we do need the database to run queries against this data and we spotted early on that optimization would be beneficial here.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I had some ideas of where we could improve and with a little Googling I found a sparse but useful &lt;a href="http://web.utk.edu/~jplyon/sqlite/SQLite_optimization_FAQ.html#indexes"&gt;SQLite optimization FAQ compiled by Jim Lyon&lt;/a&gt;. I quickly put together a hit list of optimizations that I'd try.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;Use a between construct instead of LIKE&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;The FAQ explains that a LIKE cannot use an index and thus where possible it should be replaced with the &amp;gt; and &amp;lt; operators. For example:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;    word LIKE 'dog%'&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Can be replaced with the more efficient:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;    word &amp;gt; 'dog' AND word &amp;lt; 'doh'&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;It's not a trivial replacement. Firstly the strings you are comparing must be of one case only. I also ran into trouble with international characters which of course don't fit nicely into the byte ranges. Furthermore you must write a little logic to generate the last character(s) of the 'upper limit term' (in this case 'dog'). Incrementing the last character is all well and good - but what about when you want to perform:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;    LIKE 'oz%'&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I simply appended a low value character to the string to obtain the 'upper limit term' and ended up with something like: '&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;oz!&lt;/span&gt;' Thankfully I could work within these limitations for our use-cases and performance was much improved.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;Move members of IN clauses into temporary tables&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Many of our queries used variable length IN clauses. This made it unfeasible to prepare and cache the resultant statements and they would be prepared fresh each time. This sucked up and incredible amount of time - we might spend a second just preparing the statement. A typical clause is shown here:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;    AND id IN (32, 45, 67, 68, 80)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I decided that if I moved these values into a temporary table I could use a sub-select within the IN clause and hence end up with a static statement that I could prepare once and cache:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;    AND id IN (SELECT id FROM temporary_ids)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;In addition to this I hoped to use a PRAGMA directive described in the FAQ to move the temporary tables off of the flash disk and into memory:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;    PRAGMA temp_store MEMORY&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;However, this setting does not seem to take effect on the iPhone version of SQLite which was somewhat disappointing. That said, the restructuring of my IN clauses did provided yet another significant performance improvement. I wouldn't be surprised if the row inserts into the temp table actually take longer than the execution of a given IN clause. But in this instance I am avoiding the costly preparation of a statement on each call.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;Order sub-queries so that smaller results are returned first&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;The FAQ suggests in section 5.3 that sub-queries or criteria should appear in an order such that the criteria that will exclude the most rows should appear first. I take this to mean that this (poor) example query:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;    SELECT o.id&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;    FROM owner o, pet p&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;    WHERE &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;o.age &gt; 12&lt;/span&gt; AND p.name = 'nathan' AND p.id = o.pet_id&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Should be rewritten as:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;    SELECT o.id&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;    FROM owner o, pet p&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;    WHERE &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;p.name = 'nathan'&lt;/span&gt; AND o.age &gt; 12 AND p.id = o.pet_id&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Because the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;p.name &lt;/span&gt;criteria is far more selective than that using &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;o.age&lt;/span&gt;. Okay, so it's not a great example query. However, in our queries it was quite clear which criteria would be the most selective.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;Other practices&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Prior to implementing these optimizations we were using many of the best practices recommended in the FAQ including:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Batching commands into transactions&lt;/li&gt;&lt;li&gt;Using indexes where appropriate and justifying the indexes with the EXPLAIN command&lt;/li&gt;&lt;li&gt;VACUUMing the database file before making release builds - this had a noticeable effect on the database file size but I couldn't say that it improved the performance.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-2302236745786658357?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/2302236745786658357/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2009/05/sqlite-optimization-on-iphone.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/2302236745786658357'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/2302236745786658357'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2009/05/sqlite-optimization-on-iphone.html' title='SQLite optimization on the iPhone'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-96861690342474314</id><published>2009-05-28T18:00:00.004+01:00</published><updated>2009-05-28T20:25:23.228+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='nszombie'/><category scheme='http://www.blogger.com/atom/ns#' term='uialert'/><title type='text'>Never forget</title><content type='html'>&lt;div&gt;Often when we are developing applications for the iPhone we want to enable extra 'tools' in the execution environment so we can get a better grasp of what our software is actually doing. Typically we'll use some kind of logging, and after reading &lt;a href="http://www.cocoadev.com/index.pl?NSZombieEnabled"&gt;&lt;span class="Apple-style-span"&gt;Adhamh Findlay's post&lt;/span&gt;&lt;/a&gt; on the use of NSZombie I'll often have NSZombieEnabled turned on.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Both of these tools will have an impact on the behaviour of our application if enabled. Logging will certainly slow the application down and NSZombie can make the app run out of memory. Therefore, we need to be conscious of these potential hazards so that we don't try to start fixing problems that are actually just artifacts of the tools. More importantly we must be certain that none of these useful developer settings make their way into released code. It's not hard to imagine that one could let logging slip into a build - at a minimum this will cost you another cycle of building a release and potentially much, much much more.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;We found ourselves making this mistake a couple of times with internal releases so thankfully it was only QA who complained and not customers. But having made this mistake we resolved to make it very difficult to repeat. So here's the first thing that our developers see when they start-up the application in a development mode:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_6YOYua-aQv4/Sh64ZJMg_HI/AAAAAAAAAJ8/r7YRUTtOWGY/s1600-h/alert.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 180px;" src="http://1.bp.blogspot.com/_6YOYua-aQv4/Sh64ZJMg_HI/AAAAAAAAAJ8/r7YRUTtOWGY/s400/alert.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5340908950212639858" /&gt;&lt;/a&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Sure, dismissing the alert is a little annoying but it reminds us of the particulars of the current execution environment and saves us from making a costly mistake. We can then release our software with greater confidence. From a code perspective it's lightweight - in &lt;span class="Apple-style-span" style=""&gt;our &lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;viewDidLoad&lt;/span&gt;&lt;/span&gt; method of our root &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;UIViewController&lt;/span&gt;&lt;/span&gt; we call:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;    &lt;span class="Apple-style-span" style=""&gt;[ForgetfulDevelopersAlert showDeveloperWarningIfRequired];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;In the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;ForgetfulDevelopersAlert&lt;/span&gt;&lt;/span&gt; implementation we check for logging like so:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;    BOOL logging = NO;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;    #if LOG_LEVEL != NONE&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;        logging = YES;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;    #endif&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;And we detect for NSZombie usage with:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    + (BOOL) __isNSZombieEnabled {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;        return getenv("NSZombieEnabled")&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;            || getenv("NSAutoreleaseFreedObjectCheckEnabled");&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;With these flags available it is straight forward to construct a message and show the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;UIAlert&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;    if (logging || zombies) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;        UIAlertView* alert = [[ForgetfulDevelopersAlert alloc]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;            __initAlertWithLogging:logging andZombies:zombies];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;        [alert show];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;        [alert release];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This can of course be extend to check for other development settings. For example I know of one particular case where an app was submitted to the AppStore and it was not noticed until it had finally been approved that only 'test' ad banners had been enabled. Potentially this lost some revenue and required another approval cycle with Apple - but it is a very easy mistake to make that perhaps could have been avoided with something like:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:'courier new';font-size:13px;"&gt;    &lt;span class="Apple-style-span" style=""&gt;[ForgetfulDevelopersAlert showWarningIfTestAdsEnabled];&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style=" ;font-family:'courier new';font-size:13px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;:-)&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-96861690342474314?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/96861690342474314/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2009/05/cost-of-forgetfulness.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/96861690342474314'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/96861690342474314'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2009/05/cost-of-forgetfulness.html' title='Never forget'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_6YOYua-aQv4/Sh64ZJMg_HI/AAAAAAAAAJ8/r7YRUTtOWGY/s72-c/alert.png' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-8975959491567523683</id><published>2009-05-27T13:05:00.007+01:00</published><updated>2009-05-28T20:20:02.170+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='private properties'/><category scheme='http://www.blogger.com/atom/ns#' term='oo'/><category scheme='http://www.blogger.com/atom/ns#' term='objective c'/><title type='text'>Private properties for iPhone Objective-C</title><content type='html'>&lt;div&gt;Keeping members private is extremely important in Object Oriented design as it allows us to adhere to the principles of &lt;a href="http://en.wikipedia.org/wiki/Encapsulation_(computer_science)"&gt;&lt;span class="Apple-style-span"&gt;encapsulation&lt;/span&gt;&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Information_hiding"&gt;&lt;span class="Apple-style-span"&gt;information hiding&lt;/span&gt;&lt;/a&gt;, and makes it easier to keep our objects thread safe. We often want to use @properties in Objective-C to take care of retain counts in setters for us. However, unless you are careful these properties will always be public. This 'kitchen sink' approach results in all of your internal object state being exposed on your public API. This is dangerous because:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Developers can modify the internal state of your objects when they shouldn't&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Developers are 'tempted' to access things that they shouldn't as they can see 'apparently useful' things on the interface.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Developers are confused when they read the interface - they need to somehow work out which public properties they can use and which they can't.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;In short: &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;NEVER&lt;/span&gt; expose class internals publicly - it is very bad OO practice.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Private setters are discussed in &lt;a href="http://theocacao.com/document.page/516"&gt;&lt;span class="Apple-style-span"&gt;Scott Stevenson's Objective-C tutorial&lt;/span&gt;&lt;/a&gt;, but the post does not explain how to make both the setter and getter private - furthermore I was tripped up by a coupled of points that are not illustrated in Scott's examples. So how do we create completely private properties in Objective-C? The steps are as follows:&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;Make the member variable @private. This is not essential to create the property but if we're making the property private then it'd be wise to make the member variable accessible only from within our class.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;DO NOT&lt;/span&gt; declare the property in the main @interface in the header (.h) file&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Create an @interface for an extension (unnamed category) in the .m file &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;ABOVE&lt;/span&gt; the @implementation&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Declare your private @property in the extension interface&lt;br /&gt;&lt;/li&gt;&lt;li&gt;@synthesize the private property in your @implementation as normal.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div&gt;See code below for an example:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;ClassWithPrivateProperty.h&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    @interface ClassWithPrivateProperty : NSObject {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    &lt;/span&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style=""&gt;@private&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;        NSString* member;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    - (void) trySettingPrivateProperty;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    @end&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;ClassWithPrivateProperty.m&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    #import "ClassWithPrivateProperty.h"&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style=""&gt;    @interface ClassWithPrivateProperty ()&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style=""&gt;    @property (nonatomic,retain) NSString* member; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style=""&gt;    @end&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    @implementation ClassWithPrivateProperty&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    &lt;/span&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style=""&gt;@synthesize member;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    - (void) trySettingPrivateProperty {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;        self.member = @"A Value";&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;        NSLog(@"myClass.member = %@", self.member);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    @end&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-8975959491567523683?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/8975959491567523683/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2009/05/private-properties-for-iphone-objective.html#comment-form' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/8975959491567523683'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/8975959491567523683'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2009/05/private-properties-for-iphone-objective.html' title='Private properties for iPhone Objective-C'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-1368093899884795053</id><published>2009-05-25T10:36:00.023+01:00</published><updated>2009-05-28T21:31:49.533+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='concurrenct'/><category scheme='http://www.blogger.com/atom/ns#' term='nscondition'/><category scheme='http://www.blogger.com/atom/ns#' term='command pattern'/><category scheme='http://www.blogger.com/atom/ns#' term='threading'/><title type='text'>A simple concurrency framework for the iPhone</title><content type='html'>&lt;div&gt;The iPhone app we're working on has become quite large and complex - necessarily so. However, from the start of development many months ago the product has grown organically. Although almost feature complete - the app was quite unstable and failed to deliver in terms of performance so some refactoring was needed.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;As I worked my way through spaghetti code, I came across many threading objects and asynchronous calls that were initiated and 'managed' all over the application, sometimes in the view, sometimes the model, and sometimes on the controllers. This effectively made it impossible to determine what the application was doing at any given time. To make matters worse it it was not clear that we were being thread-safe as access to some shared data was unsynchronized. These findings were the likely root of our stability issues. In addition to this we found some tight thread-sleep loops. These were a possible candidate for some performance issues but also a total abomination.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;To tackle these issues I had a plan. I would create a framework that provided the threading functionality required by our application use-cases in a safe and centralized manner. I'll now describe the framework.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The foundation of the framework is an old favourite - the &lt;a href="http://en.wikipedia.org/wiki/Command_pattern"&gt;&lt;span class="Apple-style-span"&gt;command pattern&lt;/span&gt;&lt;/a&gt;. Any piece of work we want to do on a thread - be it make an HTTP call, parse a document, or asynchronously write to the database - is expressed as a command. In practice we must implement a protocol:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;    @protocol Command&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;    - (void) execute;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;    @end&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Concrete command classes usually pass some input parameters in the constructor and provide an accessor so that the caller can extract any result of the processing that takes place in the execute method. The execute method implementation is the 'stuff' you actually want to DO be it synchronously or asyncrhonously.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Fundamentally the framework executes commands. However, regardless of your concrete command implementation - the framework can execute your command in a synchronous (blocking) or asynchronous manner. What a relief for the developers - forget about how we safely do something asynchronously and concentrate on the thing you actually want to DO. The framework provides the following methods on the core class - &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;CommandSchedulerQueue&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; - for command execution:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    - (void) &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;addCommandToQueueAndReturn&lt;/span&gt;:(id&amp;lt;Command&amp;gt;&lt;/span&gt;&lt;command&gt;&lt;span class="Apple-style-span" style=""&gt;)command;&lt;/span&gt;&lt;/command&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    - (void) &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;addCommandToQueueAndReturn&lt;/span&gt;:(id&lt;/span&gt;&lt;command&gt;&lt;span class="Apple-style-span" style=""&gt;&amp;lt;Command&amp;gt;)command &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/command&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;command&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-weight: normal; "&gt;        &lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;withCallback&lt;/span&gt;:(id&lt;/span&gt;&lt;mutablecallback&gt;&lt;span class="Apple-style-span" style=""&gt;&amp;lt;MutableCallback&amp;gt;)callback;&lt;/span&gt;&lt;/mutablecallback&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/command&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    - (void) &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;addCommandToQueueAndWait&lt;/span&gt;:(id&lt;/span&gt;&lt;command&gt;&lt;span class="Apple-style-span" style=""&gt;&amp;lt;Command&amp;gt;)command &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/command&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;command&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-weight: normal; "&gt;        &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;withTimeoutInSeconds&lt;/span&gt;:(NSInteger)timeout;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/command&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;As the method names suggest - the framework provides a command queue internally which mandates that the commands be executed in the sequence that they were added to the queue. This may sound a little restrictive but it is perfectly possible to create multiple command queues or even allow more than one command to be executed concurrently on a given queue by allocating the queue more threads.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;So before I describe the functionality given to commands and callbacks by the framework, let me mention what's going on inside the framework - and specifically the thing we call the &lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;CommandSchedulerQueue&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; class. From the outset I wanted to stick to using well understood Foundation components and classes. Indeed the Foundation framework provides a &lt;a href="http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/index.html"&gt;&lt;span class="Apple-style-span"&gt;rich set of classes to deal with concurrency&lt;/span&gt;&lt;/a&gt; - so use them! At the core of the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;CommandSchedulerQueue&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; class is an &lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;NSOperationQueue&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, we wrap our command execution in an &lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;NSOperation&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and schedule it on the operation queue. This is nothing new - our application used &lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;NSOpeationQueue&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;s in the past but often created them all over the place and applied them in an inconsistent manner. By wrapping an &lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;NSOperationQueue&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; in our &lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;CommandSchedulerQueue&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; we can add useful behavior and hide &lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;NSOperationQueue&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; specifics that previously tempted developers to do undesirable things.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;So what kind of behavior do we add to the command execution?&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;We can block the caller until the operation - or rather the command - is complete. We do this in a safe and performant manner using the &lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;NSCondition&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; class.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;We can invoke a callback to the caller if they have scheduled a command to be executed asyncronously.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;We can capture an exception raised during the command's execution and either  throw it back to the caller immediately if the command was executed synchronously, or we can deliver the exception in the callback in the case of asynchronous execution.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;Let's take a look at the callback:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    @protocol Callback&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    - (void) &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;callbackInvokedF&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;callback&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;orCommand&lt;/span&gt;:(id&lt;/span&gt;&lt;/span&gt;&lt;command&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&amp;lt;Command&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;callback&gt;&lt;command&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;)command&lt;/span&gt;&lt;/span&gt;&lt;/command&gt;&lt;/callback&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/command&gt;&lt;/callback&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    - (void) &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;throwExecutionException&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    @end&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    @protocol MutableCallback &amp;lt;Callback&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;callback&gt;&lt;/callback&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    - (void) setExecutionException:(NSException*)exception;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    @end&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;When implementing the callback for a command we need to provide the behavior of the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;callbackInvoked&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; method - i.e what we want to happen when the callback is finally fired. Often we'll want to send a message to the original caller to tell them that their command is no longer executing. The receiver of the callback can then invoke &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;throwExecutionException&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; to handle any exception that occurred during the execution of the command. An implementation of &lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;throwExecutionException&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is actually provided in an abstract &lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&amp;lt;MutableCallback&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; implementation and the &lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;CommandSchedulerQueue&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; knows how to set the exception within the callback object.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;So that's the basic framework. In our iPhone application we now put all concurrent functionality through this framework which allows us to keep the threading complexity in one place and provides us with excellent visibility on what the app is doing at any given time. We can schedule commands knowing that they'll be executed in sequence and with the confidence that we won't miss any exceptions thrown - even if they're thrown on another thread. Finally we can schedule operations and block until completion in a clean manner (no tight thread-sleep loops!) while hiding the specifics of &lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;NSCondition&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-1368093899884795053?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/1368093899884795053/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2009/05/simple-concurrency-framework-for-iphone.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/1368093899884795053'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/1368093899884795053'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2009/05/simple-concurrency-framework-for-iphone.html' title='A simple concurrency framework for the iPhone'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-555362914697577467</id><published>2009-05-03T16:32:00.000+01:00</published><updated>2009-05-03T16:12:43.884+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='clean code'/><category scheme='http://www.blogger.com/atom/ns#' term='haltonisms'/><title type='text'>Code as prose</title><content type='html'>&lt;div&gt;I recently started reading &lt;a href="http://www.amazon.co.uk/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882"&gt;Clean Code&lt;/a&gt; - and immediately felt guilty for not reading it earlier. In the first few pages Grady Booch suggests that clean code should read like prose. If we take this a little further use an analogy where the code of an application could be thought of as a book we could then ask the question: '&lt;span class="Apple-style-span" style="font-style: italic;"&gt;How would the the shelves of our local bookshop appear?&lt;/span&gt;' Their contents would surely reflect the current state and quality of IT projects at large.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Personally I think there would be a fair few fine works of prose and many of those you could take home for free. However, the majority of those shelves would be chock full of vaguely similar &lt;a href="http://www.flickr.com/photos/timboduke/2657395815/"&gt;children's colouring-in books that have been carelessly scribbled in&lt;/a&gt;. Markings from the 'crayon of implementation' would wildly stray over the clear and purposeful black outlines of original intent.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Flickr is surprisingly devoid of badly executed 'colouring-ins' but has no shortage of perfectly filled pages from such books - perhaps this is yet another example of &lt;a href="http://www.nysscpa.org/cpajournal/2003/1203/nv/nv2.htm"&gt;sunlight being the best disinfectant&lt;/a&gt;. &lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-555362914697577467?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/555362914697577467/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2009/05/code-as-prose.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/555362914697577467'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/555362914697577467'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2009/05/code-as-prose.html' title='Code as prose'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-3338089876635151737</id><published>2009-05-03T08:58:00.022+01:00</published><updated>2009-05-28T20:24:36.565+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='hibernate'/><category scheme='http://www.blogger.com/atom/ns#' term='don&apos;t reinvent the wheel'/><category scheme='http://www.blogger.com/atom/ns#' term='sqlite'/><category scheme='http://www.blogger.com/atom/ns#' term='celko'/><category scheme='http://www.blogger.com/atom/ns#' term='jdbc'/><title type='text'>Revisiting JDBC (pt. 1)</title><content type='html'>About 6 or 7 years ago I was writing a lot of SQL and &lt;a href="http://java.sun.com/javase/6/docs/technotes/guides/jdbc/"&gt;JDBC&lt;/a&gt; - I remember being particularly pleased when I developed an efficient implementation of &lt;a href="http://www.intelligententerprise.com/001020/celko.jhtml"&gt;Celko's Nested sets&lt;/a&gt; to represent hierarchical data in a Content Management project. At the time JDBC was a pretty neat way of interfacing with a relational database. However, we Enterprise Java developers have for the most part left JDBC behind in favor of excellent ORM frameworks such as &lt;a href="http://www.hibernate.org/"&gt;Hibernate&lt;/a&gt; and although we are using JDBC more that ever, we do so with it operating under the covers - tucked away within our ORM framework. Sure there may be times when we have to step back to JDBC - how well would Hibernate handle hierarchical data? - but they are infrequent.&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Now as I mentioned in previous posts, the main reason I found myself developing - well, debugging at first - on the iPhone platform was to investigate why a SQLite based feature of our application wasn't functioning as well as the original Java proof-of-concept. Fairly soon I was immersed in the world of &lt;a href="http://www.sqlite.org/c3ref/intro.html"&gt;SQLite's C/C++ interface&lt;/a&gt; - JDBC this was not. From a Java point of view it's low-level: Error conditions signaled by return value on almost every function and pointers aplenty. What I wanted was JDBC - or rather &lt;span class="Apple-style-span" style="font-style: italic;"&gt;OBJC&lt;/span&gt;DBC - but in fact I wanted much more because in the JDBC world I had also become used to:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Excellent connection and prepared statement pooling with the likes of &lt;a href="http://commons.apache.org/dbcp/"&gt;DBCP&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;Concise utility methods that allowed me to avoid JDBC boiler-plate in the form of &lt;a href="http://commons.apache.org/dbutils/"&gt;DbUtil&lt;/a&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;I Googled a while to see if such a thing existed in the iPhone domain and thankfully found many likely candidates on the &lt;a href="http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers"&gt;SQL Wiki&lt;/a&gt; (see: Objective-C section) ranging from simple wrappers to ORM frameworks. However, the simple wrappers were not as clean and object orientated as I was used to with JDBC and the ORM frameworks would not give me enough control to write some of the highly optimized SQL queries that our application demanded. I set about writing my own wrapper. One could argue that I was &lt;a href="http://www.codinghorror.com/blog/archives/001145.html"&gt;reinventing the wheel&lt;/a&gt; - but I'm always happy to learn more about wheels. My requirements were as follows:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Option to pool connections and statements&lt;/li&gt;&lt;li&gt;Check for &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;every&lt;/span&gt; error condition that SQLite could possibly set and convert these into exceptions so we can adopt a &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;try&lt;/span&gt;&lt;/span&gt;/&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;catch&lt;/span&gt;&lt;/span&gt;/&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;finally&lt;/span&gt;&lt;/span&gt; approach when accessing the database.&lt;/li&gt;&lt;li&gt;Provide a clean and simple programmatic interface.&lt;/li&gt;&lt;li&gt;Move all direct interactions with SQLite into a few sensible classes - C based SQLite code had previously been spread liberally throughout the application.&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;My core classes were to be as follows:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;PooledDataSource&lt;/span&gt;&lt;/span&gt; - A connection data source that also pools connections. Calling close on a connection actually returns it to the pool.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;Connection&lt;/span&gt;&lt;/span&gt; - Encapsulates a SQLite database handle - can also pool statements that have been prepared from this connection. Calling prepare on a connection might actually fetch and reuse a pooled &lt;span class="Apple-style-span"  style=" ;font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;PreparedStatement&lt;/span&gt;&lt;/span&gt; rather than creating a new instance. rovides methods to manage transactions.&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;PreparedStatement&lt;/span&gt;&lt;/span&gt; - Prepares transient and non-transient statements, binds parameter values to statements, and executes statements. Returns &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;ResultSet&lt;/span&gt;&lt;/span&gt;s for &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;SELECT&lt;/span&gt;&lt;/span&gt; queries. Calling close on a prepared statement returns it to the statement pool if it is not transient.&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;ResultSet&lt;/span&gt;&lt;/span&gt; - An interface for stepping through a cursor and retrieving values from the row.&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;With these classes in place I could write some typical Data Access Object code:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;    PreparedStatement statement;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;    ResultSet results;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;    NSNumber maxId = nil;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;    @try {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;        stmt = [con prepare:@"SELECT MAX(id) FROM foo WHERE bar = ?"];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=";font-family:'courier new';"&gt;        [con begin]; // OK - so we &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;don't need&lt;/span&gt; this for a SELECT&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;        results = [stmt executeWithValues:@"dog", nil];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;        if ([results next]) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;           maxId = [results getInt:1];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=";font-family:'courier new';"&gt;        [con commit]; // for illustration &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;only&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;    @catch (NSException* sqlX) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=";font-family:'courier new';"&gt;        [con rollback]; // for illustration &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;only&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;        // handle error&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;    } @finally {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt; &lt;span class="Apple-style-span" style=""&gt;       &lt;/span&gt;[DatabaseCommons closeQuietly:results, statement, con];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;I'll get more into the actual class implementation details in part 2.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-3338089876635151737?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/3338089876635151737/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2009/05/revisiting-my-old-friend-jdbc-pt-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/3338089876635151737'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/3338089876635151737'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2009/05/revisiting-my-old-friend-jdbc-pt-1.html' title='Revisiting JDBC (pt. 1)'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-5968782936945330464</id><published>2009-05-02T17:10:00.002+01:00</published><updated>2009-05-28T20:23:05.355+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dynamic proxy'/><category scheme='http://www.blogger.com/atom/ns#' term='nsproxy'/><category scheme='http://www.blogger.com/atom/ns#' term='proxy'/><title type='text'>Dynamic Proxies</title><content type='html'>&lt;div&gt;Part of our iPhone application uses proxy objects lazily load the full-fat objects behind the scenes. The initial implementation simply used an object that had the same core interface of the proxied object and used a factory to load the full-fat object in the event that a property was called on the proxy that it couldn’t service with it’s own internal state.&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    @implementation MyProxyClass // : NSObject ... &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;myprotocol&gt;&lt;/myprotocol&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    -&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;(NSString*)doSomethingIntensive {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&lt;span class="Apple-style-span" style="white-space: normal; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;if ([self fullFatObjectNotLoaded]) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;            [self loadFullFatObject];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;        return [fullFat doSomethingIntensive];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    ...&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This was all well and good - but eventually the model sitting behind the proxy grew and grew until we had a rather large hierarchy of classes that had to be managed by the proxy. To do this the proxy would have to present all of the varied properties on all of the classes in the model hierarchy. In development terms alone this wasn’t scalable - we’d keep having to add new properties to the proxy as they were added to the model; instead a shortcut around this issue had been implemented. The proxy exposed the full-fat object with it’s own getter:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    @interface MyProxyClass : NSObject ...&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    -&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;(MyClass*)getFullFat;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    ...&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The problem with this is of course that now the caller to the proxy must actually know about the proxy - it can no longer just use the MyProtocol protocol (&lt;span class="Apple-style-span" style=""&gt;Java: ‘Interface’&lt;/span&gt;) and must now be aware of the type ‘&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;MyProxyClass&lt;/span&gt;’. This makes the calling code tightly coupled to the lazy loading process - exactly what we don’t want.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;As enterprise Java developers we see dynamic proxies intercepting calls to our classes everywhere in our EJB containers, Dependency injection frameworks (e.g: Spring, Seam), and ORM frameworks (Hibernate for example). In truth we rarely see them - perhaps in a call stack in the debugger - but we know they are there wrapping rich services around our objects without our intervention.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;What I wanted was something similar for our iPhone application - some kind of Objective-C dynamic proxy that could seamlessly proxy our (perhaps overly) complex model and remain hidden from the classes who would actually be be calling the proxy.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Now writing a dynamic proxy service in Java is probably difficult. I haven’t written one but I imagine it entails reflection and byte code generation - not necessarily the stuff that the average Java developer gets stuck into. Besides - there are good libraries that do this already and do it well in the Java world. However - in the world of Objective-C it is surprisingly easy to do out of the box using a Foundation class named ‘&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;NSProxy&lt;/span&gt;&lt;/span&gt;’.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Any selector messages (&lt;span class="Apple-style-span" style=""&gt;Java: ‘method calls’&lt;/span&gt;) invoked on the proxy invoke a default method on the proxy called ‘&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;forwardInvocation&lt;/span&gt;&lt;/span&gt;’ including a description of the invocation (selector, arguments, etc.) By overriding the ‘&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;forwardInvocation&lt;/span&gt;&lt;/span&gt;’ method we can route the method invocation wherever we like. In the case of our lazy loading proxy we’d like to check to see if our proxy can service the invocation (we can use '&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;respondsToSelector&lt;/span&gt;&lt;/span&gt;' for this) and if not - load up the object we are the proxy for and then forward the invocation to that full fat object like so:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="white-space: pre; font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="white-space: normal; "&gt;    @implementation MyProxyClass // : NSProxy ...&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;-(void)forwardInvocation:(NSInvocation*)invocation {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="white-space: normal; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;SEL selector = [invocation selector];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;        &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="white-space: normal; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;if ([self fullFatNotLoadedAndCanBeHandledByProxy:selector]) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;            &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="white-space: normal; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;[invocation setTarget:lowFat];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;      &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="white-space: normal; "&gt;&lt;span class="Apple-style-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;} else if (fullFat != nil) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;            &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="white-space: normal; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;[invocation setTarget: fullFat];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;      &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="white-space: normal; "&gt;&lt;span class="Apple-style-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;} else {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;            &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="white-space: normal; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;NSAssert(FALSE, @"Hope this never happens");&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;       &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="white-space: normal; "&gt;&lt;span class="Apple-style-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;       &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="white-space: normal; "&gt;&lt;span class="Apple-style-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;[invocation invoke];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;       &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="white-space: normal; "&gt;&lt;span class="Apple-style-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;return;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="white-space: pre; "&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Of course - it’s a bit more complicated than that. &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;NSProxy&lt;/span&gt;&lt;/span&gt; is not an actual object as such - it’s not a subclass of &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;NSObject&lt;/span&gt;&lt;/span&gt;. So if you send an ‘init’ invocation to a &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;NSProxy&lt;/span&gt;&lt;/span&gt; instance it doesn’t actually call the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;init&lt;/span&gt; (&lt;span class="Apple-style-span" style=""&gt;Java: ‘default constructor’&lt;/span&gt;) on the proxy instance - it doesn’t have one. Instead this is treated like an other method and we find that we have an invocation of ‘&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;forwardInvocation&lt;/span&gt;&lt;/span&gt;’ with selector ‘&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;init&lt;/span&gt;&lt;/span&gt;’. And in this event what object reference should we return to the caller? It gets even more confusing when we find that calls to extremely type specific methods such as: ‘&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;isKindOfClass&lt;/span&gt;&lt;/span&gt;’, ‘&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;conformsToProtocol&lt;/span&gt;&lt;/span&gt;’, and ‘&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;respondsToSelector&lt;/span&gt;&lt;/span&gt;’ also get directed straight to ‘&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;forwardInvocation&lt;/span&gt;&lt;/span&gt;’ and that we have to handle those too! How confusing - this would never happen in Java!&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;So this is the way that I chose to implement the lazy loading proxy:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;The proxy would have an internal ‘low-fat’ skeleton object that acts as a placeholder for the proxied object - in my case it had a member that held the full fat objects database ID. The low fat object is alloced when the proxy gets a request for the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;NSMethodSignature&lt;/span&gt;&lt;/span&gt; for the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;initWithId&lt;/span&gt; method - the proxy passes a reference to it&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;self&lt;/span&gt;&lt;/span&gt; - not the databaseId - we have to wait for the '&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;forwardInvocation&lt;/span&gt;&lt;/span&gt;' call to get at the method call argument.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=";font-family:'courier new';"&gt;    @implementation MyProxyClass // : NSProxy ...&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=";font-family:'courier new';"&gt;    ...&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;   -(NSMethodSignature*)methodSignatureForSelector:(SEL)selector {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;        if (selector == @selector(initWithId:)) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;            if (lowFat == nil) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;                lowFat = [[LowFatObject alloc] initWithProxy:self];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;            } else {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;                NSAssert(FALSE, @"Hope this never happens");&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;            }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;        ...&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=";font-family:'courier new';"&gt;    ...&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=";font-family:'courier new';"&gt;    @end&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=";font-family:'courier new';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    @implementation LowFatObject&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    @synthesize databaseId;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    -(id)initWithProxy:(MyProxyClass*)aProxy {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;        if (self = [super init]) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;            proxy = aProxy;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;            // NOTE: 'proxy' member of type MyProxyClass!&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;        return self;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;span class="Apple-style-span" style=""&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;The ‘&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;initWithId&lt;/span&gt;’ call to the proxy would actually be routed to the low-fat object to but note that the low fat object has already been alloced and init'ed by the proxy. So instead it adopts the databaseId value and (importantly) returns a reference to the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;proxy&lt;/span&gt; - &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;not&lt;/span&gt; it&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;self&lt;/span&gt;&lt;/span&gt;.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="  ;font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    @implementation LowFatObject&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    -(id)initWithProxy:(MyProxyClass*)aProxy...&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    ...&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style=" ;font-family:'courier new';font-size:13px;"&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    -(id)initWithId:(int)aDatabaseId {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;        databaseId = aDatabaseId;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;        return &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;proxy&lt;/span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;        // We want the caller to have a ref to the &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;proxy&lt;/span&gt;!&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;We're still left with the issue of deciding when to load the full-fat item. To do this we need to check if the low-fat skeleton object is able to service the invocation and if not we need to load the full-fat object and set that as the invocation target. We do this when we're asked to supply the '&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;NSMethodSignature&lt;/span&gt;&lt;/span&gt;' for the pending invocation. Before we dive into this recall that the invocation could be for something where the return type is extremely type specific - such as '&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style=""&gt;respondsToSelector&lt;/span&gt;&lt;/span&gt;' - in those eventualities we MUST also load the full-fat object because only the can we really know what the object we are a proxy for is actually capable of.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=";font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style=";font-family:Georgia;"&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=";font-family:'courier new';"&gt;    @implementation MyProxyClass // : NSProxy ...&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=";font-family:'courier new';"&gt;    ...&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    -(NSMethodSignature*)methodSignatureForSelector:(SEL)selector {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;        ... init method handler ...&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;        NSObject* target;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;        // Is the selector extremely type specific?&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;        if (selector == @selector(isKindOfClass:)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;            || selector == @selector(conformsToProtocol:)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;            || selector == @selector(respondsToSelector:))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;        {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;            [self loadFullFatTarget];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;            target = fullFat;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;        } else if ([self fullFatNotLoadedAndCanBeHandledByProxy:selector]) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;            // The low-fat item responds to the selector&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;            // and the full-fat hasn't been loaded&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;            target = lowFat;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;        } else {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;            // We need to load the full fat because the low-fat&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;            // skeleton can't handle it&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;            [self loadFullFatTarget];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;            NSAssert1(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;                [fullFat respondsToSelector:selector],&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;                @"Neither full/low fat object can handle selector %@",&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;                selector&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;            );&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;            target = fullFat;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;        return [target methodSignatureForSelector:selector];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span" style=""&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Despite being a little fiddly this approach has worked quite well in practice. As a Java developer it's quite refreshing to be able to construct something as powerful as a Dynamic Proxy using a core class supplied in the SDK. Although the model we are proxying probably has much room for refactoring we can now at least add and remove properties from the model classes and have them implicitly supported by the proxy without any further modification to the proxy itself.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-5968782936945330464?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/5968782936945330464/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2009/05/dynamic-proxies.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/5968782936945330464'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/5968782936945330464'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2009/05/dynamic-proxies.html' title='Dynamic Proxies'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-9038835668429330357</id><published>2009-05-02T16:59:00.001+01:00</published><updated>2009-05-03T10:44:57.307+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='eclipse'/><category scheme='http://www.blogger.com/atom/ns#' term='xcode'/><category scheme='http://www.blogger.com/atom/ns#' term='key shortcuts'/><title type='text'>From (My)Eclipse to XCode</title><content type='html'>As any Java developer worth his salt knows - if you’re going to be writing code in a productive way - you should at least know a handful of keyboard shortcuts in Eclipse. Yes I’m sure others use Netbeans and IntelliJ IDEA - but I have met them rarely and they often seem to have selected their IDE just to be different, awkward, or both (flame war starts here).&lt;br /&gt;&lt;br /&gt;So important is the knowledge of shortcuts that I’ve often seen them on cheat sheets, stuck on the side of mugs, and described incredulously in code reviews when one developer who knows a shortcut, watches the other developer painfully scroll through a menu or type out a class name. Some developers - so happy that they’ve discovered something powerful - email all of their developer friends with the newly learnt shortcut only to be ridiculed in a barrage of replies with sentiments to the effect of: ‘You’ve been earning £X00 a day developing with Eclipse for 5 years and you didn’t know about shortcut X?’&lt;br /&gt;&lt;br /&gt;So anyway, my first big hurdle with XCode was shortcuts. Without those I could not be as productive as I’d like. In fact - that wasn’t my first hurdle - first I needed to stop using Eclipse shortcuts in XCode.&lt;br /&gt;&lt;br /&gt;So I’ll list my favorite XCode shortcuts. They are there in the preference panels, in books and on the side of mugs. But if like me you can’t be bothered to look and would rather have someone else tell you here they are:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;alt-cmd-up&lt;/span&gt;: Switch between header and implementation file&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;alt-cmd-left&lt;/span&gt;: go to previous file&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;alt-cmd-right&lt;/span&gt;: go to next file&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;cmd-b&lt;/span&gt;:  start/stop build&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;shift-cmd-k&lt;/span&gt;: clean all targets&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;cmd-return&lt;/span&gt;: build and go (launches the iPhone simulator/device with your app)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;shift-cmd-d&lt;/span&gt;: find a source file&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;shift-cmd-f&lt;/span&gt;: find text in the project sources&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-9038835668429330357?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/9038835668429330357/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2009/05/from-myeclipse-to-xcode.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/9038835668429330357'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/9038835668429330357'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2009/05/from-myeclipse-to-xcode.html' title='From (My)Eclipse to XCode'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-2459321193918223339</id><published>2009-05-02T16:52:00.002+01:00</published><updated>2009-05-03T11:08:28.562+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='cheat sheet'/><category scheme='http://www.blogger.com/atom/ns#' term='whoami'/><category scheme='http://www.blogger.com/atom/ns#' term='sqlite'/><category scheme='http://www.blogger.com/atom/ns#' term='j2ee'/><title type='text'>Background</title><content type='html'>So a little bit of background. I have been happily developing with Java for over 9 years and am very appreciative of the wealth of frameworks available that make my job easier and more productive. In the company I work for currently I have been developing an enterprise server side Java application whose job is to aggregate and feed data to an iPhone based client. It got to the point where the server was virtually complete, scaling well and had few additional features remaining in the product backlog. We ‘server guys’ had applied best practices, patterns, and frameworks with a very satisfactory result.&lt;br /&gt;&lt;br /&gt;I then began architecting features of the iPhone client application - with no intention of actually developing on the platform but merely providing the iPhone developers with a proof of concept that they could effectively ‘transcribe‘ into the iPhone domain. The feature in question required an SQLite database on the iPhone and as a server guy I could throw SQL together with ease - so it made sense that I should do the design. However, as the iPhone implementation of my designs progressed, many issues presented themselves on the client that just didn’t exist in my Java-based proof of concept. It was at this point I realized that I’d have to delve into the world of Objective-C and XCode.  Having done a bit of C programming at university and written one MFC based OpenGL app for my coursework 12 years previous - I was struck with fear at the learning curve I probably faced.&lt;br /&gt;&lt;br /&gt;I was very pleasantly surprised. In terms of language structure and syntax - the core things that make Java development easier (or rather: less difficult) than C were all in place in Objective-C. Exceptions, Thread classes, synchronization, interfaces, etc. were there - although often they are often called something totally different and aren’t quite like the Java equivalent. I was given Dylan McNamee’s ‘&lt;a href="http://www.mecodegoodsomeday.com/ObjectiveCRef.pdf"&gt;Java to Objective-C cheat sheet&lt;/a&gt;’ which proved a useful starting point and I was soon at ease. Then, having had a quick look at the language  I wanted to get started debugging the iPhone implementation of the SQLite POC.&lt;br /&gt;&lt;br /&gt;I’d recommend that any Java developer interested in the iPhone platform jump right in - they won’t find the learning curve that steep and in my opinion you can apply all the software development fundamentals that one is often required to know in the Enterprise Java domain to this (much smaller) platform.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-2459321193918223339?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/2459321193918223339/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2009/05/background.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/2459321193918223339'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/2459321193918223339'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2009/05/background.html' title='Background'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3059468813255868046.post-8875282763883617292</id><published>2009-05-02T16:51:00.003+01:00</published><updated>2009-05-03T11:08:55.562+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='whoami'/><category scheme='http://www.blogger.com/atom/ns#' term='j2ee'/><category scheme='http://www.blogger.com/atom/ns#' term='iphone'/><title type='text'>Why this is here</title><content type='html'>As an experienced Enterprise Java developer forced onto the iPhone development platform, I have noticed many opportunities where the experience I have gained in the enterprise can be used to good effect on even Apple’s smallest public platform.&lt;br /&gt;&lt;br /&gt;To me at least it seems that Java developers with some good development experience behind them can lend much to this relatively new platform and the community that is rapidly building up around it. I did not find this so much to be the case when working with J2ME which at the time (2004) made it prohibitively expensive in performance terms to stick rigorously to respected patterns and frameworks. The iPhone platform is arguably far more powerful than that J2ME stack I developed for in the past and therefore allows the developer to pay more attention to good design and development practice.&lt;br /&gt;&lt;br /&gt;In this blog I hope to look at problems encountered on the iPhone platform and how they were solved with lessons learned from J2EE development.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3059468813255868046-8875282763883617292?l=swish-movement.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://swish-movement.blogspot.com/feeds/8875282763883617292/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://swish-movement.blogspot.com/2009/05/why-this-is-here.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/8875282763883617292'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3059468813255868046/posts/default/8875282763883617292'/><link rel='alternate' type='text/html' href='http://swish-movement.blogspot.com/2009/05/why-this-is-here.html' title='Why this is here'/><author><name>Elliot West</name><uri>http://www.blogger.com/profile/07923631905285214423</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
