<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>Patrick Crosby's Internet Presents</title>
 <link href="http://patrickcrosby.com/atom.xml" rel="self"/>
 <link href="http://patrickcrosby.com"/>
 <updated>2014-04-03T16:36:24+07:00</updated>
 <id>http://patrickcrosby.com</id>
 <author>
   <name>Patrick Crosby</name>
 </author>

 
 <entry>
   <title>JavaScript Error Logging Service using Go and jsErrLog</title>
   <link href="http://blog.patrickcrosby.com/2014/03/28/javascript-error-logging-service-using-go-and-jserrlog.html"/>
   <updated>2014-03-28T00:00:00+07:00</updated>
   <id>http://patrickcrosby.com/2014/03/28/javascript-error-logging-service-using-go-and-jserrlog.html</id>
   <content type="html">&lt;p&gt;In a &amp;ldquo;standard&amp;rdquo; web application where nearly all the work is done on the server,
it&amp;rsquo;s fairly straightforward to track errors.  They happen on your servers, you can log
them, you can send emails when they happen, whatever you want.&lt;/p&gt;

&lt;p&gt;But with web apps where more and more of the functionality is in client-side JavaScript, the
errors on the server disappear.  The app developers have no idea they are happening unless
users notify them.&lt;/p&gt;

&lt;p&gt;It&amp;rsquo;s no surprise that there are a lot of services to help with this problem.
&lt;a href=&#34;https://plus.google.com/+PaulIrish/posts/12BVL5exFJn&#34;&gt;Paul Irish compiled a list&lt;/a&gt; of
some of them.  Some only do JavaScript errors, some track all kinds of errors, some
are based on &lt;code&gt;window.onerror&lt;/code&gt;, some on catching exceptions.&lt;/p&gt;

&lt;p&gt;I wanted something simple to start.  I just want to know that an error occurred.  While
a stack trace would be nice, right now I&amp;rsquo;m ok without it.
So I&amp;rsquo;m trying out &lt;a href=&#34;https://github.com/Offbeatmammal/jsErrLog&#34;&gt;jsErrLog&lt;/a&gt;.  It binds a
function to &lt;code&gt;window.onerror&lt;/code&gt; and sends any error information to a web service.  There is
a free service you can use that&amp;rsquo;s running on AppEngine, but I chose to send the data
to our servers.  All the &lt;code&gt;window.onerror&lt;/code&gt; solutions can only send the URL, file,
line number, error message, and some newer browsers include the column number.&lt;/p&gt;

&lt;p&gt;I wrote a small web service to receive the data from the jsErrLog script.  I had a few
requirements for it:  send errors periodically via email, don&amp;rsquo;t store them anywhere else,
don&amp;rsquo;t send duplicates, don&amp;rsquo;t use too much memory.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s the structure that holds the data from jsErrLog:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type entry struct {
        url      string
        filename string
        line     string
        col      string
        errmsg   string
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There&amp;rsquo;s a map that uses the &lt;code&gt;entry&lt;/code&gt; struct as a key.  You could change the &lt;code&gt;bool&lt;/code&gt; to an
&lt;code&gt;int&lt;/code&gt; if you care about how many times an error occurred.  I&amp;rsquo;m using a &lt;code&gt;sync.RWMutex&lt;/code&gt;
to protect the map.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var dedupe map[entry]bool
var lock sync.RWMutex
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;init&lt;/code&gt; function creates the initial map and starts the dump goroutine.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;func init() {
        dedupe = make(map[entry]bool)
        go dump()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The request handler function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;func ErrLog(w http.ResponseWriter, r *http.Request, s *bingo.Session) {
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It starts by getting the form values into an &lt;code&gt;entry&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;        var e entry
        id := r.FormValue(&amp;quot;i&amp;quot;)
        e.url = r.FormValue(&amp;quot;sn&amp;quot;)
        e.filename = r.FormValue(&amp;quot;fl&amp;quot;)
        e.line = r.FormValue(&amp;quot;ln&amp;quot;)
        e.col = r.FormValue(&amp;quot;cn&amp;quot;)
        e.errmsg = r.FormValue(&amp;quot;err&amp;quot;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then it checks to see if it already has this error.  It read-locks the mutex
before the check.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;        lock.RLock()
        _, exists := dedupe[e]
        lock.RUnlock()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If it doesn&amp;rsquo;t exist, it write-locks the mutex and puts the error info in the
map.  It checks to make sure there aren&amp;rsquo;t too many elements already in the map
as a safeguard.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;        if !exists {
                lock.Lock()
                if len(dedupe) &amp;lt; 1000 {
                    dedupe[e] = true
                }
                lock.Unlock()
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;jsErrLog&lt;/code&gt; script wants JavaScript returned to it, including a special call to
remove a script that it inserts.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;        w.Header().Set(&amp;quot;Content-Type&amp;quot;, &amp;quot;text/javascript&amp;quot;)
        fmt.Fprintf(w, &amp;quot;jsErrLog.removeScript(%s);&amp;quot;, id)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally, the dump function just loops forever.  It sleeps for five minutes, then
checks to see if there&amp;rsquo;s anything in the &lt;code&gt;dedupe&lt;/code&gt; map.  If there is, it emails it
to a system account and clears the map.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;func dump() {
        for {
                time.Sleep(5 * time.Minute)
                lock.Lock()
                if len(dedupe) &amp;gt; 0 {
                        sendmail()
                        dedupe = make(map[entry]bool)
                }
                lock.Unlock()
        }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It&amp;rsquo;s pretty simple, but it does everything we want.  If we need stack traces,
we&amp;rsquo;ll give Airbrake a try.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Responsive SVG</title>
   <link href="http://blog.patrickcrosby.com/2014/03/26/responsive-svg.html"/>
   <updated>2014-03-26T00:00:00+07:00</updated>
   <id>http://patrickcrosby.com/2014/03/26/responsive-svg.html</id>
   <content type="html">&lt;p&gt;Many of the charts on &lt;a href=&#34;http://www.stathat.com&#34;&gt;StatHat&lt;/a&gt; are changing from server-rendered PNGs
to browser-rendered SVGs.  There are a lot of reasons for this, but one is that we&amp;rsquo;d like the
charts to be more responsive to different screen sizes and retina displays.&lt;/p&gt;

&lt;p&gt;Since SVG stands for Scalable Vector Graphics, it should be a piece of cake to scale them.
If you just put an svg with a width and height in an HTML page:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;body&amp;gt;
&amp;lt;svg width=&amp;quot;940&amp;quot; height=&amp;quot;280&amp;quot;&amp;gt;......&amp;lt;/svg&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and resize your browser, it will always stay at 940x280 pixels.&lt;/p&gt;

&lt;p&gt;Remove the width and height and use &lt;code&gt;viewBox&lt;/code&gt; and &lt;code&gt;preserveAspectRatio&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;body&amp;gt;
&amp;lt;svg viewBox=&amp;quot;0 0 940 280&amp;quot; preserveAspectRation=&amp;quot;xMinYMin meet&amp;quot;&amp;gt;......&amp;lt;/svg&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you can resize your browser, look at the page on any device, and it will fill the
browser window, the lines will be nice clean vectors.&lt;/p&gt;

&lt;p&gt;There&amp;rsquo;s just one issue.  For some reason, the svg element is square.  So anything that you&amp;rsquo;d
like to put after the svg is pushed way down the page. A CSS trick that I found
&lt;a href=&#34;http://demosthenes.info/blog/744/Make-SVG-Responsive&#34;&gt;on Dudley Storey&amp;rsquo;s blog&lt;/a&gt; solves this issue.&lt;/p&gt;

&lt;p&gt;Add a container div, and a class for the svg:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;body&amp;gt;
&amp;lt;div class=&amp;quot;svg-container&amp;quot;&amp;gt;
  &amp;lt;svg viewBox=&amp;quot;0 0 940 280&amp;quot; preserveAspectRation=&amp;quot;xMinYMin meet&amp;quot; class=&amp;quot;svg-content&amp;quot;&amp;gt;......&amp;lt;/svg&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The CSS that fixes it all up:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;.svg-container {
    display: inline-block;
    position: relative;
    width: 100%;
    padding-bottom: 30%;
    vertical-align: middle;
    overflow: hidden;
}

.svg-content {
    display: inline-block;
    position: absolute;
    top: 0;
    left: 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The tricky thing is that you need to set the &lt;code&gt;padding-bottom&lt;/code&gt; of the container to the
aspect ratio of the SVG element.  In this case it&amp;rsquo;s (280 / 940) * 100, which is just
about 30%.  So if you have SVG elements with different aspect ratios, you would need
a class for each one with the &lt;code&gt;padding-bottom&lt;/code&gt; value adjusted accordingly. All the
charts on StatHat have the same aspect ratio, so this wasn&amp;rsquo;t an issue.&lt;/p&gt;

&lt;p&gt;&lt;a href=&#34;/examples/svg.html&#34;&gt;View an example chart&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I really like this solution.  It&amp;rsquo;s simple, there&amp;rsquo;s no JavaScript required.
Unfortunately, due to the interactive features of the StatHat charts, we&amp;rsquo;re going to
have to do more and use JS to update certain UI elements.  Also, the resizing of the
text in this solution makes it difficult to read the axes on small screens.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>syntax off</title>
   <link href="http://blog.patrickcrosby.com/2014/03/25/syntax-off.html"/>
   <updated>2014-03-25T00:00:00+07:00</updated>
   <id>http://patrickcrosby.com/2014/03/25/syntax-off.html</id>
   <content type="html">&lt;p&gt;As an experiment, I turned off syntax highlighting a week ago.  Now it seems weird to see it.&lt;/p&gt;

&lt;p&gt;With syntax highlighting on, my eyes focus on the highlighted text:  keywords, strings,
numbers, built-in types and functions, and comments.  The types I create aren&amp;rsquo;t highlighted.
My function names get no special treatment.  Visually, you have to fight through the colors
to get to your own code.&lt;/p&gt;

&lt;p&gt;An argument for syntax highlighting would be that it helps you find errors while you are
editing.  The errors that it might make more clear, however, are basically typos.  There
are so many tools that continuously build, format, lint, and test your code that any of
those errors can be caught immediately by tools that know the language.&lt;/p&gt;

&lt;p&gt;Syntax highlighters often make mistakes.  They are not language parsers.  For example,
&lt;code&gt;string&lt;/code&gt; is a perfectly valid function name in Go.  Yet it is highlighted differently
than &lt;code&gt;timestwo&lt;/code&gt; in this example:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;/media/photos/highlight.png&#34;/&gt;&lt;/p&gt;

&lt;p&gt;Without syntax highlighting, I find code more readable.  We don&amp;rsquo;t read books or articles
with different colored words, why is code different?  But I could be weird:  I still
print out code on paper sometimes to really read it and analyze it.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>401 vs. 403</title>
   <link href="http://blog.patrickcrosby.com/2014/03/24/401_vs_403.html"/>
   <updated>2014-03-24T00:00:00+07:00</updated>
   <id>http://patrickcrosby.com/2014/03/24/401_vs_403.html</id>
   <content type="html">&lt;p&gt;In a &amp;ldquo;standard&amp;rdquo; web application, if a user requests a page and their login session doesn&amp;rsquo;t exist or has expired, they get redirected to a sign in page.&lt;/p&gt;

&lt;p&gt;In a more dynamic web application where all the updates are done via AJAX and full page loads
are rare, something else needs to be done.  Handling a 302 redirect status code with jquery
is possible, but it takes just
as much (or more) code as any other status code.  When developing
&lt;a href=&#34;http://www.stathat.com&#34;&gt;the next web interface for StatHat&lt;/a&gt;, we made the server code as
dumb as possible.  It doesn&amp;rsquo;t know what the interface wants to do if the user is not signed in:  it
could redirect to a sign in page like the original version, it could show a modal dialog,
whatever.&lt;/p&gt;

&lt;p&gt;So we decided to return a 4XX status code and let the JavaScript client decide how to
handle it.  The question is, which one?&lt;/p&gt;

&lt;p&gt;403 Forbidden is often used.  But &lt;a href=&#34;http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4&#34;&gt;look at the spec&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;403 Forbidden&lt;/p&gt;

&lt;p&gt;The server understood the request, but is refusing to fulfill it.
&lt;em&gt;Authorization will not help and the request SHOULD NOT be repeated&lt;/em&gt;. If the
request method was not HEAD and the server wishes to make public why the
request has not been fulfilled, it SHOULD describe the reason for the refusal
in the entity. If the server does not wish to make this information available
to the client, the status code 404 (Not Found) can be used instead.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It clearly states that authorization will not help.  The other main contender
is &lt;a href=&#34;http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2&#34;&gt;401 Unauthorized&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;401 Unauthorized&lt;/p&gt;

&lt;p&gt;The request requires user authentication. The response MUST include a
WWW-Authenticate header field (section 14.47) containing a challenge
applicable to the requested resource. The client MAY repeat the request with a
suitable Authorization header field (section 14.8)&amp;hellip;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This sounds perfect except for the WWW-Authenticate junk.  We aren&amp;rsquo;t going to support
HTTP basic/digest authentication, but it seems like we are still honoring the
intention of the spec to use 401, so we we went ahead with it and just ignored the
WWW-Authenticate part of it.&lt;/p&gt;

&lt;p&gt;We could have created our own error code, but that&amp;rsquo;s breaking the spec even more.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>iPhone/iPad UISearchBar and UISearchDisplayController Asynchronous Example</title>
   <link href="http://blog.patrickcrosby.com/2010/04/27/iphone-ipad-uisearchbar-uisearchdisplaycontroller-asynchronous-example.html"/>
   <updated>2010-04-27T00:00:00+07:00</updated>
   <id>http://patrickcrosby.com/2010/04/27/iphone-ipad-uisearchbar-uisearchdisplaycontroller-asynchronous-example.html</id>
   <content type="html">&lt;p&gt;All of the Apple examples and almost everything I could find on the net showing how to use
&lt;code&gt;UISearchBar&lt;/code&gt; and &lt;code&gt;UISearchDisplayController&lt;/code&gt; operated on an existing set of data in a data structure
in the application.  I want to search either a database locally on the iPhone/iPad or make a
remote search request to an internet API.  Here are my notes on how to get this working.&lt;/p&gt;

&lt;p&gt;First it is necessary to understand the elements involved.  &lt;code&gt;UISearchBar&lt;/code&gt; is the user interface
widget.  The bar with the text field with rounded sides.  You can use this by itself and
connect its delegate to something that implements &lt;code&gt;UISearchBarDelegate&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But, beginning with
iPhone OS 3.0, there is a purportedly better way: &lt;code&gt;UISearchDisplayController&lt;/code&gt;.  There&amp;rsquo;s no
need to subclass it, just use it out of the box.  It is the glue between various parts of the
search process.  You initialize it with a &lt;code&gt;UISearchBar&lt;/code&gt; and a &amp;ldquo;contentsController&amp;rdquo;.  While the
Apple docs state that the contents controller is usually a &lt;code&gt;UITableViewController&lt;/code&gt;, it doesn&amp;rsquo;t
have to be, and I think it&amp;rsquo;s simpler to understand what&amp;rsquo;s going on if you just use a
&lt;code&gt;UIViewController&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;UISearchDisplayController&lt;/code&gt; becomes the delegate for the &lt;code&gt;UISearchBar&lt;/code&gt;.  You need to wire
up a few more connections:  a &lt;code&gt;UISearchDisplayDelegate&lt;/code&gt;, a &lt;code&gt;UITableViewDataSource&lt;/code&gt; that provides
the search result data, and a &lt;code&gt;UITableViewDelegate&lt;/code&gt; for handling selecting the search result table
cells.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s what &lt;code&gt;UISearchDisplayController&lt;/code&gt; does in a typical search scenario:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The user taps the &lt;code&gt;UISearchBar&lt;/code&gt;.  &lt;code&gt;UISearchDisplayController&lt;/code&gt; activates the search interface,
brings up the keyboard.  It creates a table view for the results.&lt;/li&gt;
&lt;li&gt;The user enters text to search for.  &lt;code&gt;UISearchDisplayController&lt;/code&gt; informs the &lt;code&gt;UISearchDisplayDelegate&lt;/code&gt; of this.  The &lt;code&gt;UISearchDisplayController&lt;/code&gt; asks the delegate if it should reload its table (the default is YES).&lt;/li&gt;
&lt;li&gt;When the &lt;code&gt;UISearchDisplayController&lt;/code&gt; wants to reload the table, it asks the &lt;code&gt;UITableViewDataSource&lt;/code&gt; for the number of rows and the cells for each row it wants.  It displays the results table view on top of the existing controller.&lt;/li&gt;
&lt;li&gt;If a user selects one of the rows in the table view owned by the &lt;code&gt;UISearchDisplayController&lt;/code&gt;, it lets the &lt;code&gt;UITableViewDelegate&lt;/code&gt; handle it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The key point (at least for me) to understand here is that &lt;code&gt;UISearchDisplayController&lt;/code&gt; creates its own &lt;code&gt;UITableView&lt;/code&gt;.  It puts it on top of the &lt;code&gt;contentsController&lt;/code&gt; when there are search results.  With all the examples I found, the &lt;code&gt;contentsController&lt;/code&gt; was itself a &lt;code&gt;UITableViewController&lt;/code&gt; and had its own &lt;code&gt;UITableView&lt;/code&gt; that it was displaying.&lt;/p&gt;

&lt;p&gt;The basic examples all work like this:  there&amp;rsquo;s a &lt;code&gt;UITableViewController&lt;/code&gt; with 200 rows of data.  There&amp;rsquo;s a &lt;code&gt;UISearchBar&lt;/code&gt; and a &lt;code&gt;UISearchDisplayController&lt;/code&gt;.  All of the delegates for the &lt;code&gt;UISearchDisplayController&lt;/code&gt; connect to the &lt;code&gt;UITableViewController&lt;/code&gt;.  When the search is done, the &lt;code&gt;UISearchDisplayController&lt;/code&gt; overlays its table with a filtered set of the original rows on top of the existing table of 200 rows.  When the search interface is dismissed, the overlay table is removed and the original table is available.&lt;/p&gt;

&lt;p&gt;Clearly these basic examples don&amp;rsquo;t work for a large database or an internet search API.  You can&amp;rsquo;t load all the results into a table data source before the search and just filter them for the search results.  You need to make a request for the search results, and to keep your app responsive, you should make that request asynchronously.&lt;/p&gt;

&lt;p&gt;Apple hints at how to do this in their documentation for &lt;code&gt;UISearchDisplayDelegate&lt;/code&gt; in the &lt;code&gt;searchDisplayController:shouldReloadTableForSearchString:&lt;/code&gt; and &lt;code&gt;searchDisplayController:shouldReloadTableForSearchScope:&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You might implement this method if you want to perform an asynchronous search. You would initiate the search in this method, then return NO. You would reload the table when you have results.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For this example, I&amp;rsquo;ll create a new project in xcode, an iPad Split-view based application.  Open MainWindow.xib in Interface Builder, remove the RootViewController.  It&amp;rsquo;s a table view controller and doesn&amp;rsquo;t need to be for this example.  Drag a standard view controller into its place.&lt;/p&gt;

&lt;p&gt;Back in xcode, go ahead and trash &lt;code&gt;RootViewController.m&lt;/code&gt; and &lt;code&gt;RootViewController.h&lt;/code&gt;.  Remove references to it in the app delegate and the details controller.  Add a new file, a &lt;code&gt;UIViewController&lt;/code&gt; subclass with &amp;ldquo;Targeted for iPad&amp;rdquo; checked.  Name it GenericViewController.&lt;/p&gt;

&lt;p&gt;In interface builder, change the class of the view controller you added to &lt;code&gt;GenericViewController&lt;/code&gt;.  Add a view to &lt;code&gt;GenericViewController&lt;/code&gt;.  Drag the &amp;ldquo;Search Bar and Search Display Controller&amp;rdquo; to fit underneath the view.  Doing this in Interface Builder makes it automatically connect the search display controller to the &lt;code&gt;GenericViewController&lt;/code&gt; for all its delegates.  This is fine for this example, but feel free to change this wiring if you want.&lt;/p&gt;

&lt;p&gt;You can build it now, it will compile without errors and run.  But if you try to search for anything, it will crash.  This is because &lt;code&gt;GenericViewController&lt;/code&gt; is set to be the delegate for all kinds of things for the search display controller, but none of the required protocols are implemented.  Start by adding the protocols to the interface in &lt;code&gt;GenericViewController.h&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@interface GenericViewController : UIViewController &amp;lt;UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate&amp;gt; {

}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And in the implementation, paste this in for now (copied from the RootViewController default):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
        // Return the number of sections.
        return 1;
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.
        return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @&amp;quot;CellIdentifier&amp;quot;;

        // Dequeue or create a cell of the appropriate type.
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                cell.accessoryType = UITableViewCellAccessoryNone;
        }

        // Configure the cell.
        cell.textLabel.text = [NSString stringWithFormat:@&amp;quot;Row %d&amp;quot;, indexPath.row];
        return cell;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you can build it and when you search, it always returns the same &amp;ldquo;Row 1 - 10&amp;rdquo; data, but you can see &lt;code&gt;UISearchDisplayController&lt;/code&gt; doing its work and overlaying a table view of results.&lt;/p&gt;

&lt;p&gt;Enough synchronous stuff.  Start the asynchronicity!  For the purposes of this demo, we&amp;rsquo;re
going to fake a slow search by using an &lt;code&gt;NSTimer&lt;/code&gt;.  Here&amp;rsquo;s the crucial method to add to &lt;code&gt;GenericViewController&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(mockSearch:) userInfo:searchString repeats:NO];
        return NO;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;By returning &lt;code&gt;NO&lt;/code&gt;, the search display controller won&amp;rsquo;t reload its data.  The timer calls &lt;code&gt;mockSearch&lt;/code&gt; after a two second delay.  Here&amp;rsquo;s &lt;code&gt;mockSearch&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;- (void)mockSearch:(NSTimer*)timer
{
        [_data removeAllObjects];
        int count = 1 + random() % 20;
        for (int i = 0; i &amp;lt; count; i++) {
                [_data addObject:timer.userInfo];
        }
        [self.searchDisplayController.searchResultsTableView reloadData];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;_data&lt;/code&gt; variable is an &lt;code&gt;NSMutableArray&lt;/code&gt; member variable.  It shoves a random number of rows containing the search string (in &lt;code&gt;userInfo&lt;/code&gt;) into the array, then tells the search display controller&amp;rsquo;s table view to reload its data.  I changed these methods to use &lt;code&gt;_data&lt;/code&gt; for the rows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
        return [_data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @&amp;quot;CellIdentifier&amp;quot;;

        // Dequeue or create a cell of the appropriate type.
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                cell.accessoryType = UITableViewCellAccessoryNone;
        }

        // Configure the cell.
        cell.textLabel.text = [NSString stringWithFormat:@&amp;quot;Row %d: %@&amp;quot;, indexPath.row, [_data objectAtIndex:indexPath.row]];
        return cell;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So that&amp;rsquo;s about it.  To use it in the real world, change the &lt;code&gt;NSTimer&lt;/code&gt; call to some asynchronous network or database call.  When it is done, reload the results table just like in &lt;code&gt;mockSearch&lt;/code&gt;.  One nice thing would be to also implement &lt;code&gt;searchDisplayControllerDidBeginSearch:&lt;/code&gt; and &lt;code&gt;searchDisplayControllerDidEndSearch:&lt;/code&gt; to display some sort of &amp;ldquo;Loading&amp;hellip;&amp;rdquo; view on top of the results view until the results come in.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Update:&lt;/em&gt; By popular demand, here is the full source and xcode project for this example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href=&#34;/media/code/SearchExample.tar.gz&#34;&gt;SearchExample.tar.gz&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It&amp;rsquo;s for iPhone OS 3.2 and above only.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Vim Objective-C Colon Indentation</title>
   <link href="http://blog.patrickcrosby.com/2009/10/02/vim-objective-c-colon-lineup.html"/>
   <updated>2009-10-02T00:00:00+07:00</updated>
   <id>http://patrickcrosby.com/2009/10/02/vim-objective-c-colon-lineup.html</id>
   <content type="html">&lt;p&gt;Vim&amp;rsquo;s objective-c mode has an annoying indentation issue.  It tries to line colons up on
subsequent lines, like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[self performSelectorOnMainThread:@selector(finishLoad) 
                       withObject:nil 
                    waitUntilDone:NO];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;rsquo;s nice, but it has an annoying bug that makes it try to align the colons with a
previous line that is unrelated:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if ([someVariable isEqualToString:@&amp;quot;blah&amp;quot;]) {
[self performSelectorOnMainThread:@selector(finishLoad) 
                       withObject:nil 
                    waitUntilDone:NO];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This all takes place in &lt;code&gt;indent/objc.vim&lt;/code&gt; (full path on a MacPorts installation is
&lt;code&gt;/opt/local/share/vim/vim72/indent/objc.vim&lt;/code&gt;, for MacVim it is
&lt;code&gt;/Applications/MacVim.app/Contents/Resources/vim/runtime/indent/objc.vim&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;There is probably a way to fix this in a &lt;code&gt;.vimrc&lt;/code&gt; file, but I just edited &lt;code&gt;objc.vim&lt;/code&gt;.  At
the bottom of the file is a function &lt;code&gt;GetObjCIndent&lt;/code&gt;.  Change the second &lt;code&gt;if&lt;/code&gt; statement
to match the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function GetObjCIndent()
    let theIndent = cindent(v:lnum)

    let prev_line = getline(v:lnum - 1)
    let cur_line = getline(v:lnum)

    if prev_line !~# &amp;quot;:&amp;quot; || cur_line !~# &amp;quot;:&amp;quot;
        return theIndent
    endif

    if prev_line !~# &amp;quot;;&amp;quot; &amp;amp;&amp;amp; prev_line !~# &amp;quot;{&amp;quot;
        let prev_colon_pos = s:GetWidth(prev_line, &amp;quot;:&amp;quot;)
        let delta = s:GetWidth(cur_line, &amp;quot;:&amp;quot;) - s:LeadingWhiteSpace(cur_line)
        let theIndent = prev_colon_pos - delta
    endif

    return theIndent
endfunction
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are some refinements that could be made to fix it for more cases, but this one
fixes the one that was driving me crazy.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Safari Keyboard Shortcuts To Switch Tabs</title>
   <link href="http://blog.patrickcrosby.com/2009/09/10/safari-keyboard-shortcuts-to-switch-tabs.html"/>
   <updated>2009-09-10T00:00:00+07:00</updated>
   <id>http://patrickcrosby.com/2009/09/10/safari-keyboard-shortcuts-to-switch-tabs.html</id>
   <content type="html">&lt;p&gt;&lt;img src=&#34;/media/photos/zebras.jpg&#34; alt=&#34;zebras&#34; /&gt;

&lt;em&gt;Photo by &lt;a href=&#34;http://www.flickr.com/photos/wwarby/2405490902/&#34;&gt;wwarby&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I am so used to the cmd-1, cmd-2, cmd-3, cmd-9
keyboard shortcuts in Firefox to switch to the first, second, third, and last tabs in a
window.  Whenever I try to use Safari and I instinctually use these shortcuts, it goes
to bookmarks (1, 2, 3, and 9), which is incredibly annoying.  Apple provides no way
to change this.&lt;/p&gt;

&lt;p&gt;With Leopard, I was using &lt;a href=&#34;http://store.giraffesoft.ca/&#34;&gt;Safari Commander&lt;/a&gt;, but it
stopped working with Snow Leopard.  There are &lt;a href=&#34;http://twitter.com/jamesgolick/status/3618013749&#34;&gt;hints on twitter&lt;/a&gt; that a new version is coming out soon that will work, but it
requires Safari to run in 32-bit mode.  And Firefox seems to be slower than usual under
Snow Leopard.&lt;/p&gt;

&lt;p&gt;So the solution?  &lt;a href=&#34;http://www.red-sweater.com/fastscripts/&#34;&gt;Fastscripts&lt;/a&gt;.  Thanks to
&lt;a href=&#34;http://justinblanton.com/2009/02/go-to-safari-tab-with-keyboard&#34;&gt;this blog post&lt;/a&gt; by &lt;a href=&#34;http://justinblanton.com/&#34;&gt;Justin Blanton&lt;/a&gt;, I now have scripts to switch to tabs one through five and the
last tab.  I modified his scripts to be for Safari instead of WebKit.  They are each one line
long, but in case you&amp;rsquo;re too lazy to type them in, here they are for you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;/media/code/tab1.scpt&#34;&gt;tab1.scpt&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;/media/code/tab2.scpt&#34;&gt;tab2.scpt&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;/media/code/tab3.scpt&#34;&gt;tab3.scpt&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;/media/code/tab4.scpt&#34;&gt;tab4.scpt&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;/media/code/tab5.scpt&#34;&gt;tab5.scpt&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;/media/code/tab9.scpt&#34;&gt;tab9.scpt&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Put them in &lt;code&gt;~/Library/Scripts/Applications/Safari&lt;/code&gt; then assign them to the correct keys
in the &lt;em&gt;Script Shortcuts&lt;/em&gt; preferences pane in FastScripts.&lt;/p&gt;
</content>
 </entry>
 

</feed>
