<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Giv Parvaneh &#187; Giv</title>
	<atom:link href="http://www.givp.org/blog/author/admin/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.givp.org/blog</link>
	<description>::::..::..:::::::...:::::....::......:::::::::....::::......::::::::::::::::</description>
	<lastBuildDate>Thu, 22 Jul 2010 21:42:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<meta xmlns="http://www.w3.org/1999/xhtml" name="robots" content="noindex,follow" />
		<item>
		<title>Test-Driven Development &#8211; How Do I Start?</title>
		<link>http://www.givp.org/blog/2010/07/22/test-driven-development-how-do-i-start/</link>
		<comments>http://www.givp.org/blog/2010/07/22/test-driven-development-how-do-i-start/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 21:42:12 +0000</pubDate>
		<dc:creator>Giv</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[p]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.givp.org/blog/?p=188</guid>
		<description><![CDATA[There seem to be a lot of developers who like the idea of Test-Driven Development (TDD) and can clearly see the benefit of having tests written for their code but can&#8217;t seem to get their head around the process. How do you start writing unit tests before writing the actual code? Let&#8217;s start with an [...]]]></description>
			<content:encoded><![CDATA[<p>There seem to be a lot of developers who like the idea of <a href="http://en.wikipedia.org/wiki/Test-driven_development" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Test-driven_development?referer=');">Test-Driven Development</a> (TDD) and can clearly see the benefit of having tests written for their code but can&#8217;t seem to get their head around the process. How do you start writing unit tests <em>before</em> writing the actual code?</p>
<p>Let&#8217;s start with an example. You want to write a method that takes a URL as an argument and have it tell you through a boolean return if it&#8217;s the correct domain or not. It seems simple enough. Just write your method, pass the URL through some regular expression and you&#8217;re done.</p>
<p>But you yourself already know which domains are allowed and which are not so before writing the actual code you can run some tests in your head. E.g. I only want www.bbc.co.uk and its sub-domains on http and https. Nothing else should be allowed. So https://beta.bbc.co.uk/iplayer should return TRUE and http://www.bbcbb.com should return FALSE etc.</p>
<blockquote><p>The process behind TDD is that you first write a failing test. Then you write the actual code and adjust until the test passes.</p></blockquote>
<p>So let&#8217;s write some tests for our domain checker. I&#8217;m using Python and Unittest here:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">unittest</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># this is what we're going to be testing</span>
<span style="color: #ff7700;font-weight:bold;">class</span> Utils<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> is_bbc<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, val<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">pass</span> <span style="color: #808080; font-style: italic;">#placeholder</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># this is the actual test</span>
<span style="color: #ff7700;font-weight:bold;">class</span> TestUtils<span style="color: black;">&#40;</span><span style="color: #dc143c;">unittest</span>.<span style="color: black;">TestCase</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> setUp<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">u</span> = Utils<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> test_is_bbc<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">assertTrue</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">u</span>.<span style="color: black;">is_bbc</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://www.bbc.co.uk/iplayer'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertTrue</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">u</span>.<span style="color: black;">is_bbc</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://www.bbc.co.uk/food'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertTrue</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">u</span>.<span style="color: black;">is_bbc</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://www.bbc.co.uk'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertTrue</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">u</span>.<span style="color: black;">is_bbc</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'https://www.bbc.co.uk'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertTrue</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">u</span>.<span style="color: black;">is_bbc</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://beta.bbc.co.uk'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertFalse</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">u</span>.<span style="color: black;">is_bbc</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://www.bbc.com'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertFalse</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">u</span>.<span style="color: black;">is_bbc</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://www.bbbc.co.uk'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertFalse</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">u</span>.<span style="color: black;">is_bbc</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://.bbc.co.uk'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
suite = <span style="color: #dc143c;">unittest</span>.<span style="color: black;">TestLoader</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">loadTestsFromTestCase</span><span style="color: black;">&#40;</span>TestUtils<span style="color: black;">&#41;</span>
<span style="color: #dc143c;">unittest</span>.<span style="color: black;">TextTestRunner</span><span style="color: black;">&#40;</span>verbosity=<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>.<span style="color: black;">run</span><span style="color: black;">&#40;</span>suite<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>I&#8217;ve created a an empty method where our domain checker is going to live but as you can see it doesn&#8217;t do anything. The tests should immediately make sense. We pass a bunch of domain variations and we know which ones should pass or fail. Naturally, running the test right now will fail:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ python sample.py
test_is_bbc <span style="color: #7a0874; font-weight: bold;">&#40;</span>__main__.TestUtils<span style="color: #7a0874; font-weight: bold;">&#41;</span> ... FAIL
&nbsp;
======================================================================
FAIL: test_is_bbc <span style="color: #7a0874; font-weight: bold;">&#40;</span>__main__.TestUtils<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #660033;">----------------------------------------------------------------------</span>
Traceback <span style="color: #7a0874; font-weight: bold;">&#40;</span>most recent call <span style="color: #c20cb9; font-weight: bold;">last</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>:
  File <span style="color: #ff0000;">&quot;sample.py&quot;</span>, line <span style="color: #000000;">14</span>, <span style="color: #000000; font-weight: bold;">in</span> test_is_bbc
    self.assertTrue<span style="color: #7a0874; font-weight: bold;">&#40;</span>self.u.is_bbc<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'http://www.bbc.co.uk/iplayer'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
AssertionError
&nbsp;
<span style="color: #660033;">----------------------------------------------------------------------</span>
Ran <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #000000; font-weight: bold;">in</span> 0.000s
&nbsp;
FAILED <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">failures</span>=<span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Now you can start writing the actual code and keep running the same tests until it passes:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">unittest</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># this is what we're going to be testing</span>
<span style="color: #ff7700;font-weight:bold;">class</span> Utils<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> is_bbc<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, val<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">match</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'^https?://([^/]+)?<span style="color: #000099; font-weight: bold;">\.</span>bbc<span style="color: #000099; font-weight: bold;">\.</span>co<span style="color: #000099; font-weight: bold;">\.</span>uk'</span>, val<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># this is the actual test</span>
<span style="color: #ff7700;font-weight:bold;">class</span> TestUtils<span style="color: black;">&#40;</span><span style="color: #dc143c;">unittest</span>.<span style="color: black;">TestCase</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> setUp<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">u</span> = Utils<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> test_is_bbc<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">assertTrue</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">u</span>.<span style="color: black;">is_bbc</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://www.bbc.co.uk/iplayer'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertTrue</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">u</span>.<span style="color: black;">is_bbc</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://www.bbc.co.uk/food'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertTrue</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">u</span>.<span style="color: black;">is_bbc</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://www.bbc.co.uk'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertTrue</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">u</span>.<span style="color: black;">is_bbc</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'https://www.bbc.co.uk'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertTrue</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">u</span>.<span style="color: black;">is_bbc</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://beta.bbc.co.uk'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertFalse</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">u</span>.<span style="color: black;">is_bbc</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://www.bbc.com'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertFalse</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">u</span>.<span style="color: black;">is_bbc</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://www.bbbc.co.uk'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertFalse</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">u</span>.<span style="color: black;">is_bbc</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://.bbc.co.uk'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#this should fail</span>
&nbsp;
suite = <span style="color: #dc143c;">unittest</span>.<span style="color: black;">TestLoader</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">loadTestsFromTestCase</span><span style="color: black;">&#40;</span>TestUtils<span style="color: black;">&#41;</span>
<span style="color: #dc143c;">unittest</span>.<span style="color: black;">TextTestRunner</span><span style="color: black;">&#40;</span>verbosity=<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>.<span style="color: black;">run</span><span style="color: black;">&#40;</span>suite<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>The regex may look like it&#8217;s correct but running the test will fail again:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ python sample.py
test_is_bbc <span style="color: #7a0874; font-weight: bold;">&#40;</span>__main__.TestUtils<span style="color: #7a0874; font-weight: bold;">&#41;</span> ... FAIL
&nbsp;
======================================================================
FAIL: test_is_bbc <span style="color: #7a0874; font-weight: bold;">&#40;</span>__main__.TestUtils<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #660033;">----------------------------------------------------------------------</span>
Traceback <span style="color: #7a0874; font-weight: bold;">&#40;</span>most recent call <span style="color: #c20cb9; font-weight: bold;">last</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>:
  File <span style="color: #ff0000;">&quot;sample.py&quot;</span>, line <span style="color: #000000;">22</span>, <span style="color: #000000; font-weight: bold;">in</span> test_is_bbc
    self.assertFalse<span style="color: #7a0874; font-weight: bold;">&#40;</span>self.u.is_bbc<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'http://.bbc.co.uk'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
AssertionError
&nbsp;
<span style="color: #660033;">----------------------------------------------------------------------</span>
Ran <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #000000; font-weight: bold;">in</span> 0.001s
&nbsp;
FAILED <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">failures</span>=<span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>It has failed on the final assert because our code will also allow http://.bbc.co.uk and we obviously don&#8217;t want that. But as you can see we&#8217;ve caught this edge case before deploying our app so we can promptly fix our code.</p>
<p>Hopefully this example demonstrates why it&#8217;s a good idea to start with tests. This is obviously a simple example but on bigger projects predicting the outcome of your system can save you a lot of debugging time in the future.</p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Share and Enjoy:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow"  href="http://twitter.com/home?status=Test-Driven%20Development%20-%20How%20Do%20I%20Start%3F%20-%20http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F07%2F22%2Ftest-driven-development-how-do-i-start%2F" title="Twitter" onclick="pageTracker._trackPageview('/outgoing/twitter.com/home?status=Test-Driven_20Development_20-_20How_20Do_20I_20Start_3F_20-_20http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F07_2F22_2Ftest-driven-development-how-do-i-start_2F&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F07%2F22%2Ftest-driven-development-how-do-i-start%2F&amp;t=Test-Driven%20Development%20-%20How%20Do%20I%20Start%3F" title="Facebook" onclick="pageTracker._trackPageview('/outgoing/www.facebook.com/share.php?u=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F07_2F22_2Ftest-driven-development-how-do-i-start_2F_amp_t=Test-Driven_20Development_20-_20How_20Do_20I_20Start_3F&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F07%2F22%2Ftest-driven-development-how-do-i-start%2F&amp;title=Test-Driven%20Development%20-%20How%20Do%20I%20Start%3F&amp;bodytext=There%20seem%20to%20be%20a%20lot%20of%20developers%20who%20like%20the%20idea%20of%20Test-Driven%20Development%20%28TDD%29%20and%20can%20clearly%20see%20the%20benefit%20of%20having%20tests%20written%20for%20their%20code%20but%20can%27t%20seem%20to%20get%20their%20head%20around%20the%20process.%20How%20do%20you%20start%20writing%20unit%20tests%20be" title="Digg" onclick="pageTracker._trackPageview('/outgoing/digg.com/submit?phase=2_amp_url=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F07_2F22_2Ftest-driven-development-how-do-i-start_2F_amp_title=Test-Driven_20Development_20-_20How_20Do_20I_20Start_3F_amp_bodytext=There_20seem_20to_20be_20a_20lot_20of_20developers_20who_20like_20the_20idea_20of_20Test-Driven_20Development_20_28TDD_29_20and_20can_20clearly_20see_20the_20benefit_20of_20having_20tests_20written_20for_20their_20code_20but_20can_27t_20seem_20to_20get_20their_20head_20around_20the_20process._20How_20do_20you_20start_20writing_20unit_20tests_20be&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F07%2F22%2Ftest-driven-development-how-do-i-start%2F&amp;title=Test-Driven%20Development%20-%20How%20Do%20I%20Start%3F&amp;notes=There%20seem%20to%20be%20a%20lot%20of%20developers%20who%20like%20the%20idea%20of%20Test-Driven%20Development%20%28TDD%29%20and%20can%20clearly%20see%20the%20benefit%20of%20having%20tests%20written%20for%20their%20code%20but%20can%27t%20seem%20to%20get%20their%20head%20around%20the%20process.%20How%20do%20you%20start%20writing%20unit%20tests%20be" title="del.icio.us" onclick="pageTracker._trackPageview('/outgoing/delicious.com/post?url=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F07_2F22_2Ftest-driven-development-how-do-i-start_2F_amp_title=Test-Driven_20Development_20-_20How_20Do_20I_20Start_3F_amp_notes=There_20seem_20to_20be_20a_20lot_20of_20developers_20who_20like_20the_20idea_20of_20Test-Driven_20Development_20_28TDD_29_20and_20can_20clearly_20see_20the_20benefit_20of_20having_20tests_20written_20for_20their_20code_20but_20can_27t_20seem_20to_20get_20their_20head_20around_20the_20process._20How_20do_20you_20start_20writing_20unit_20tests_20be&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F07%2F22%2Ftest-driven-development-how-do-i-start%2F&amp;title=Test-Driven%20Development%20-%20How%20Do%20I%20Start%3F&amp;annotation=There%20seem%20to%20be%20a%20lot%20of%20developers%20who%20like%20the%20idea%20of%20Test-Driven%20Development%20%28TDD%29%20and%20can%20clearly%20see%20the%20benefit%20of%20having%20tests%20written%20for%20their%20code%20but%20can%27t%20seem%20to%20get%20their%20head%20around%20the%20process.%20How%20do%20you%20start%20writing%20unit%20tests%20be" title="Google Bookmarks" onclick="pageTracker._trackPageview('/outgoing/www.google.com/bookmarks/mark?op=edit_amp_bkmk=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F07_2F22_2Ftest-driven-development-how-do-i-start_2F_amp_title=Test-Driven_20Development_20-_20How_20Do_20I_20Start_3F_amp_annotation=There_20seem_20to_20be_20a_20lot_20of_20developers_20who_20like_20the_20idea_20of_20Test-Driven_20Development_20_28TDD_29_20and_20can_20clearly_20see_20the_20benefit_20of_20having_20tests_20written_20for_20their_20code_20but_20can_27t_20seem_20to_20get_20their_20head_20around_20the_20process._20How_20do_20you_20start_20writing_20unit_20tests_20be&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.givp.org/blog/2010/07/22/test-driven-development-how-do-i-start/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Saving data with Titanium mobile</title>
		<link>http://www.givp.org/blog/2010/07/15/saving-data-with-titanium-mobile/</link>
		<comments>http://www.givp.org/blog/2010/07/15/saving-data-with-titanium-mobile/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 20:15:29 +0000</pubDate>
		<dc:creator>Giv</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[appcelerator]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[titanium]]></category>

		<guid isPermaLink="false">http://www.givp.org/blog/?p=181</guid>
		<description><![CDATA[In my previous post I talked about getting a simple table view up and running quickly using Titanium. A few people have asked me to continue the tutorial and explain how to save individual items and then display saved items in a separate window. Saving data is quite simple. You first need to create the [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.givp.org/blog/2010/07/13/building-mobile-app-with-titanium/">my previous post</a> I talked about getting a simple table view up and running quickly using Titanium. A few people have asked me to continue the tutorial and explain how to save individual items and then display saved items in a separate window.</p>
<p>Saving data is quite simple. You first need to create the database and schema. You obviously only need to do this the first time the user opens your app. So in your app.js file you can do something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> db <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">Database</span>.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mydb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
db.<span style="color: #660066;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'CREATE TABLE IF NOT EXISTS SAVEDITEMS  (NAME TEXT)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This should look very familiar to you if you&#8217;re used to SQL. It first instantiates a database object then checks to see if the &#8220;SAVEDITEMS&#8221; table exists. If not, it will simply create it. If it does exist, it will just ignore that command. This will create a table with a single text field for storing the name of a selected item.</p>
<p>Now that we have our table set up, we can read and write to it whenever we like. If you wanted to save an item, you would do this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">db.<span style="color: #660066;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;INSERT INTO SAVEDITEMS ( NAME ) VALUES ('my cool item')&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>To get a list of all items in the database:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> rows <span style="color: #339933;">=</span> db.<span style="color: #660066;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;SELECT * FROM SAVEDITEMS&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
rows.<span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The last line is very important. You must close your query results to avoid memory leakage.</p>
<p>You can now loop through the saved items and stick them in a table view:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// create blank table view</span>
<span style="color: #003366; font-weight: bold;">var</span> tableview <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTableView</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">currentWindow</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>tableview<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// create an empty array</span>
<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// get a list of all saved items</span>
<span style="color: #003366; font-weight: bold;">var</span> rows <span style="color: #339933;">=</span> db.<span style="color: #660066;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'SELECT * FROM SAVEDITEMS'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// loop through all items</span>
<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>rows.<span style="color: #660066;">isValidRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #006600; font-style: italic;">// add each item to the data array and set the table row title</span>
   data.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span>rows.<span style="color: #660066;">field</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>hasChild<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   rows.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// load db data into the table view</span>
tableview.<span style="color: #660066;">setData</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
rows.<span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That should help you get started. You can look at how to use events to save items and also check to see which items have already been saved so you don&#8217;t show the save button etc. Take a look at the <a href="http://github.com/givp/Titanium-Mobile-Reference-Directory-App" onclick="pageTracker._trackPageview('/outgoing/github.com/givp/Titanium-Mobile-Reference-Directory-App?referer=');">demo app</a> I&#8217;ve created to see how I handled the saving logic. Specifically the <a href="http://github.com/givp/Titanium-Mobile-Reference-Directory-App/blob/master/details.js" onclick="pageTracker._trackPageview('/outgoing/github.com/givp/Titanium-Mobile-Reference-Directory-App/blob/master/details.js?referer=');">details.js</a> file.</p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Share and Enjoy:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow"  href="http://twitter.com/home?status=Saving%20data%20with%20Titanium%20mobile%20-%20http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F07%2F15%2Fsaving-data-with-titanium-mobile%2F" title="Twitter" onclick="pageTracker._trackPageview('/outgoing/twitter.com/home?status=Saving_20data_20with_20Titanium_20mobile_20-_20http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F07_2F15_2Fsaving-data-with-titanium-mobile_2F&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F07%2F15%2Fsaving-data-with-titanium-mobile%2F&amp;t=Saving%20data%20with%20Titanium%20mobile" title="Facebook" onclick="pageTracker._trackPageview('/outgoing/www.facebook.com/share.php?u=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F07_2F15_2Fsaving-data-with-titanium-mobile_2F_amp_t=Saving_20data_20with_20Titanium_20mobile&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F07%2F15%2Fsaving-data-with-titanium-mobile%2F&amp;title=Saving%20data%20with%20Titanium%20mobile&amp;bodytext=In%20my%20previous%20post%20I%20talked%20about%20getting%20a%20simple%20table%20view%20up%20and%20running%20quickly%20using%20Titanium.%20A%20few%20people%20have%20asked%20me%20to%20continue%20the%20tutorial%20and%20explain%20how%20to%20save%20individual%20items%20and%20then%20display%20saved%20items%20in%20a%20separate%20window.%0A%0ASav" title="Digg" onclick="pageTracker._trackPageview('/outgoing/digg.com/submit?phase=2_amp_url=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F07_2F15_2Fsaving-data-with-titanium-mobile_2F_amp_title=Saving_20data_20with_20Titanium_20mobile_amp_bodytext=In_20my_20previous_20post_20I_20talked_20about_20getting_20a_20simple_20table_20view_20up_20and_20running_20quickly_20using_20Titanium._20A_20few_20people_20have_20asked_20me_20to_20continue_20the_20tutorial_20and_20explain_20how_20to_20save_20individual_20items_20and_20then_20display_20saved_20items_20in_20a_20separate_20window._0A_0ASav&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F07%2F15%2Fsaving-data-with-titanium-mobile%2F&amp;title=Saving%20data%20with%20Titanium%20mobile&amp;notes=In%20my%20previous%20post%20I%20talked%20about%20getting%20a%20simple%20table%20view%20up%20and%20running%20quickly%20using%20Titanium.%20A%20few%20people%20have%20asked%20me%20to%20continue%20the%20tutorial%20and%20explain%20how%20to%20save%20individual%20items%20and%20then%20display%20saved%20items%20in%20a%20separate%20window.%0A%0ASav" title="del.icio.us" onclick="pageTracker._trackPageview('/outgoing/delicious.com/post?url=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F07_2F15_2Fsaving-data-with-titanium-mobile_2F_amp_title=Saving_20data_20with_20Titanium_20mobile_amp_notes=In_20my_20previous_20post_20I_20talked_20about_20getting_20a_20simple_20table_20view_20up_20and_20running_20quickly_20using_20Titanium._20A_20few_20people_20have_20asked_20me_20to_20continue_20the_20tutorial_20and_20explain_20how_20to_20save_20individual_20items_20and_20then_20display_20saved_20items_20in_20a_20separate_20window._0A_0ASav&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F07%2F15%2Fsaving-data-with-titanium-mobile%2F&amp;title=Saving%20data%20with%20Titanium%20mobile&amp;annotation=In%20my%20previous%20post%20I%20talked%20about%20getting%20a%20simple%20table%20view%20up%20and%20running%20quickly%20using%20Titanium.%20A%20few%20people%20have%20asked%20me%20to%20continue%20the%20tutorial%20and%20explain%20how%20to%20save%20individual%20items%20and%20then%20display%20saved%20items%20in%20a%20separate%20window.%0A%0ASav" title="Google Bookmarks" onclick="pageTracker._trackPageview('/outgoing/www.google.com/bookmarks/mark?op=edit_amp_bkmk=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F07_2F15_2Fsaving-data-with-titanium-mobile_2F_amp_title=Saving_20data_20with_20Titanium_20mobile_amp_annotation=In_20my_20previous_20post_20I_20talked_20about_20getting_20a_20simple_20table_20view_20up_20and_20running_20quickly_20using_20Titanium._20A_20few_20people_20have_20asked_20me_20to_20continue_20the_20tutorial_20and_20explain_20how_20to_20save_20individual_20items_20and_20then_20display_20saved_20items_20in_20a_20separate_20window._0A_0ASav&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.givp.org/blog/2010/07/15/saving-data-with-titanium-mobile/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a native mobile app with Titanium in less than an hour</title>
		<link>http://www.givp.org/blog/2010/07/13/building-mobile-app-with-titanium/</link>
		<comments>http://www.givp.org/blog/2010/07/13/building-mobile-app-with-titanium/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 12:03:46 +0000</pubDate>
		<dc:creator>Giv</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[appcelerator]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[Objective-c]]></category>
		<category><![CDATA[titanium]]></category>

		<guid isPermaLink="false">http://www.givp.org/blog/?p=164</guid>
		<description><![CDATA[When I wrote my last blog entry, I favoured objective-c over Appcelerator&#8217;s Titanium framework thinking it just wasn&#8217;t good enough. Almost 6 months later and I would like to take back that comment. Titanium is no longer an embedded web browser inside an app. It uses Javascript to compile an entire iPhone/Xcode project with objective-c, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://givp.mugal.org/blog/wp-content/uploads/2010/07/animalApp.jpg" alt="" title="animalApp" width="595" height="376" class="alignleft size-full wp-image-166" /><br />
<br />
When I wrote my last blog entry, I favoured objective-c over <a href="http://www.appcelerator.com/" onclick="pageTracker._trackPageview('/outgoing/www.appcelerator.com/?referer=');">Appcelerator&#8217;s Titanium framework</a> thinking it just wasn&#8217;t good enough. Almost 6 months later and I would like to take back that comment. Titanium is no longer an embedded web browser inside an app. It uses Javascript to compile an entire iPhone/Xcode project with objective-c, so it uses native UI elements using clever APIs. This is also true for Android apps, iPad and now Blackberry.</p>
<p>This is not to say there&#8217;s no need to use Xcode ever again. But if you&#8217;re planning to build a general navigation-based app with loads of menus, windows and the ability to pull/push data from the web, it can be done much faster with Titanium. Plus you&#8217;ll be able to create the same app for multiple platforms without re-writing the code.</p>
<p>So I wanted to put together a relatively simple app that lets me browse and search through a list of items, in this example, animals; and be able to group them alphabetically like the iPhone&#8217;s contacts directory. Then I want to click on an item to see more details and be able to save my favourite items for easy access later.</p>
<p>I&#8217;ve created a sample app that you can <a href="http://github.com/givp/Titanium-Mobile-Reference-Directory-App" onclick="pageTracker._trackPageview('/outgoing/github.com/givp/Titanium-Mobile-Reference-Directory-App?referer=');">download</a> and use right now. All you have to do is add your own data and you&#8217;re done.</p>
<p>I&#8217;m going to quickly run you through how to set up the tabs, the list, alphabetical grouping, search and detail view for each item. I won&#8217;t explain the saving feature here but once you do the main part below you should be able to easily follow the rest by looking at <a href="http://github.com/givp/Titanium-Mobile-Reference-Directory-App" onclick="pageTracker._trackPageview('/outgoing/github.com/givp/Titanium-Mobile-Reference-Directory-App?referer=');">the code</a>.</p>
<p>Here&#8217;s how it works. See the sample app to work out where to place individual files.</p>
<p>1. Create a tab group. One for the main list and one tab for saved items:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// create tab group</span>
<span style="color: #003366; font-weight: bold;">var</span> tabGroup <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTabGroup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// create base UI tab and root window</span>
<span style="color: #003366; font-weight: bold;">var</span> mainList <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createWindow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Animals'</span><span style="color: #339933;">,</span>
    backgroundColor<span style="color: #339933;">:</span><span style="color: #3366CC;">'#ffffff'</span><span style="color: #339933;">,</span>
    barColor<span style="color: #339933;">:</span><span style="color: #3366CC;">'#333'</span><span style="color: #339933;">,</span>
    url<span style="color: #339933;">:</span><span style="color: #3366CC;">'mainList.js'</span><span style="color: #339933;">,</span>
    tabBarHidden<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> tabMainList <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTab</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    icon<span style="color: #339933;">:</span><span style="color: #3366CC;">'book.png'</span><span style="color: #339933;">,</span>
    title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Animals'</span><span style="color: #339933;">,</span>
    window<span style="color: #339933;">:</span>mainList
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// create saved items tab</span>
<span style="color: #003366; font-weight: bold;">var</span> faves <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createWindow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Saved Items'</span><span style="color: #339933;">,</span>
    backgroundColor<span style="color: #339933;">:</span><span style="color: #3366CC;">'#ffffff'</span><span style="color: #339933;">,</span>
    barColor<span style="color: #339933;">:</span><span style="color: #3366CC;">'#333'</span><span style="color: #339933;">,</span>
    url<span style="color: #339933;">:</span><span style="color: #3366CC;">'faves.js'</span><span style="color: #339933;">,</span>
    tabBarHidden<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> tabFaves <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTab</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    icon<span style="color: #339933;">:</span><span style="color: #3366CC;">'star.png'</span><span style="color: #339933;">,</span>
    title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Saved Items'</span><span style="color: #339933;">,</span>
    window<span style="color: #339933;">:</span>faves
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//  add tabs</span>
tabGroup.<span style="color: #660066;">addTab</span><span style="color: #009900;">&#40;</span>tabMainList<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
tabGroup.<span style="color: #660066;">addTab</span><span style="color: #009900;">&#40;</span>tabFaves<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
tabGroup.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span>tabMainList<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">currentWindow</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>tabGroup<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That&#8217;s it. We have now created two tabs. mainList.js will take care of the content for the first tab and faves.js for the saved items tab.</p>
<p>2. Create a JSON doc (data.js) containing your data for the list:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myData <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
    <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Armadillo&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;description&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Armadillos eat ants but they are not Anteaters&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Bear&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;description&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Bears are awesome and they like honey&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Dog&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;description&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Dogs are the best and they woof&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Deer&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;description&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;D'oh! a deer...&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Zebra&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;description&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Zebras are stripey and cool&quot;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>3. Set up your directory list in the first tab (mainList.js)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// include the data</span>
Titanium.<span style="color: #660066;">include</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> firstLetter<span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> oldFirstLetter<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// loop through data and add to list</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> myData.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// get first letter of each item for grouping</span>
	firstLetter <span style="color: #339933;">=</span> myData<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">title</span>.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	    oldFirstLetter <span style="color: #339933;">=</span> firstLetter<span style="color: #339933;">;</span>
	    Ti.<span style="color: #660066;">API</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span>oldFirstLetter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>firstLetter <span style="color: #339933;">==</span> oldFirstLetter<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    	        oldFirstLetter <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    	    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    	        oldFirstLetter <span style="color: #339933;">=</span> firstLetter<span style="color: #339933;">;</span>
    	    <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        data.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span>myData<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">title</span><span style="color: #339933;">,</span>hasChild<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>header<span style="color: #339933;">:</span>oldFirstLetter<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        oldFirstLetter <span style="color: #339933;">=</span> firstLetter<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// add search bar</span>
<span style="color: #003366; font-weight: bold;">var</span> search <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createSearchBar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	showCancel<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// create table view</span>
<span style="color: #003366; font-weight: bold;">var</span> tableview <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTableView</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    data<span style="color: #339933;">:</span>data<span style="color: #339933;">,</span>
    search<span style="color: #339933;">:</span>search<span style="color: #339933;">,</span>
    filterAttribute<span style="color: #339933;">:</span><span style="color: #3366CC;">'title'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">currentWindow</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>tableview<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// create table view event listener</span>
tableview.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> win <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createWindow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		url<span style="color: #339933;">:</span><span style="color: #3366CC;">'details.js'</span><span style="color: #339933;">,</span>
		backgroundColor<span style="color: #339933;">:</span><span style="color: #3366CC;">'#ffffff'</span><span style="color: #339933;">,</span>
                barColor<span style="color: #339933;">:</span><span style="color: #3366CC;">'#333333'</span><span style="color: #339933;">,</span>
		title<span style="color: #339933;">:</span>e.<span style="color: #660066;">rowData</span>.<span style="color: #660066;">title</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// send selected item's title to detail page</span>
	win.<span style="color: #660066;">currentItem</span> <span style="color: #339933;">=</span> e.<span style="color: #660066;">rowData</span>.<span style="color: #660066;">title</span><span style="color: #339933;">;</span>
&nbsp;
	Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">currentTab</span>.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span>win<span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>animated<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Notice in the tableview event listener we are telling it to just open a new window on click and pass the item&#8217;s title.</p>
<p>4. Display the selected item&#8217;s data in details.js</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// include the data</span>
Titanium.<span style="color: #660066;">include</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
win <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">currentWindow</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> desc <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// create scroll view for the content</span>
<span style="color: #003366; font-weight: bold;">var</span> scrollView <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createScrollView</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	contentWidth<span style="color: #339933;">:</span><span style="color: #3366CC;">'auto'</span><span style="color: #339933;">,</span>
	contentHeight<span style="color: #339933;">:</span><span style="color: #3366CC;">'auto'</span><span style="color: #339933;">,</span>
	top<span style="color: #339933;">:</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
	showVerticalScrollIndicator<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
	showHorizontalScrollIndicator<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #006600; font-style: italic;">// loop through data and get correct item by title (this is easier than using Id's incase you add new items later)</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> myData.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>myData<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">title</span> <span style="color: #339933;">==</span> win.<span style="color: #660066;">currentItem</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        desc <span style="color: #339933;">=</span> myData<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">description</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// create description label</span>
<span style="color: #003366; font-weight: bold;">var</span> label <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createLabel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	text<span style="color: #339933;">:</span>desc<span style="color: #339933;">,</span>
	height<span style="color: #339933;">:</span><span style="color: #3366CC;">'auto'</span><span style="color: #339933;">,</span>
	width<span style="color: #339933;">:</span><span style="color: #CC0000;">300</span><span style="color: #339933;">,</span>
	top<span style="color: #339933;">:</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>
	font<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>fontSize<span style="color: #339933;">:</span><span style="color: #CC0000;">16</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	color<span style="color: #339933;">:</span><span style="color: #3366CC;">'#333333'</span><span style="color: #339933;">,</span>
	textAlign<span style="color: #339933;">:</span><span style="color: #3366CC;">'left'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That&#8217;s it. I said an hour to develop this but in reality it will probably take you much less once you get used to the APIs. Of course for the data source you can use XHR to get data from the web but if you want your app to be usable without an internet connection, you would have to store the data locally like this.</p>
<p>I hope this was helpful. Let me know if you have any questions.</p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Share and Enjoy:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow"  href="http://twitter.com/home?status=Building%20a%20native%20mobile%20app%20with%20Titanium%20in%20less%20than%20an%20hour%20-%20http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F07%2F13%2Fbuilding-mobile-app-with-titanium%2F" title="Twitter" onclick="pageTracker._trackPageview('/outgoing/twitter.com/home?status=Building_20a_20native_20mobile_20app_20with_20Titanium_20in_20less_20than_20an_20hour_20-_20http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F07_2F13_2Fbuilding-mobile-app-with-titanium_2F&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F07%2F13%2Fbuilding-mobile-app-with-titanium%2F&amp;t=Building%20a%20native%20mobile%20app%20with%20Titanium%20in%20less%20than%20an%20hour" title="Facebook" onclick="pageTracker._trackPageview('/outgoing/www.facebook.com/share.php?u=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F07_2F13_2Fbuilding-mobile-app-with-titanium_2F_amp_t=Building_20a_20native_20mobile_20app_20with_20Titanium_20in_20less_20than_20an_20hour&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F07%2F13%2Fbuilding-mobile-app-with-titanium%2F&amp;title=Building%20a%20native%20mobile%20app%20with%20Titanium%20in%20less%20than%20an%20hour&amp;bodytext=%0A%0AWhen%20I%20wrote%20my%20last%20blog%20entry%2C%20I%20favoured%20objective-c%20over%20Appcelerator%27s%20Titanium%20framework%20thinking%20it%20just%20wasn%27t%20good%20enough.%20Almost%206%20months%20later%20and%20I%20would%20like%20to%20take%20back%20that%20comment.%20Titanium%20is%20no%20longer%20an%20embedded%20web%20browser%20insi" title="Digg" onclick="pageTracker._trackPageview('/outgoing/digg.com/submit?phase=2_amp_url=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F07_2F13_2Fbuilding-mobile-app-with-titanium_2F_amp_title=Building_20a_20native_20mobile_20app_20with_20Titanium_20in_20less_20than_20an_20hour_amp_bodytext=_0A_0AWhen_20I_20wrote_20my_20last_20blog_20entry_2C_20I_20favoured_20objective-c_20over_20Appcelerator_27s_20Titanium_20framework_20thinking_20it_20just_20wasn_27t_20good_20enough._20Almost_206_20months_20later_20and_20I_20would_20like_20to_20take_20back_20that_20comment._20Titanium_20is_20no_20longer_20an_20embedded_20web_20browser_20insi&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F07%2F13%2Fbuilding-mobile-app-with-titanium%2F&amp;title=Building%20a%20native%20mobile%20app%20with%20Titanium%20in%20less%20than%20an%20hour&amp;notes=%0A%0AWhen%20I%20wrote%20my%20last%20blog%20entry%2C%20I%20favoured%20objective-c%20over%20Appcelerator%27s%20Titanium%20framework%20thinking%20it%20just%20wasn%27t%20good%20enough.%20Almost%206%20months%20later%20and%20I%20would%20like%20to%20take%20back%20that%20comment.%20Titanium%20is%20no%20longer%20an%20embedded%20web%20browser%20insi" title="del.icio.us" onclick="pageTracker._trackPageview('/outgoing/delicious.com/post?url=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F07_2F13_2Fbuilding-mobile-app-with-titanium_2F_amp_title=Building_20a_20native_20mobile_20app_20with_20Titanium_20in_20less_20than_20an_20hour_amp_notes=_0A_0AWhen_20I_20wrote_20my_20last_20blog_20entry_2C_20I_20favoured_20objective-c_20over_20Appcelerator_27s_20Titanium_20framework_20thinking_20it_20just_20wasn_27t_20good_20enough._20Almost_206_20months_20later_20and_20I_20would_20like_20to_20take_20back_20that_20comment._20Titanium_20is_20no_20longer_20an_20embedded_20web_20browser_20insi&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F07%2F13%2Fbuilding-mobile-app-with-titanium%2F&amp;title=Building%20a%20native%20mobile%20app%20with%20Titanium%20in%20less%20than%20an%20hour&amp;annotation=%0A%0AWhen%20I%20wrote%20my%20last%20blog%20entry%2C%20I%20favoured%20objective-c%20over%20Appcelerator%27s%20Titanium%20framework%20thinking%20it%20just%20wasn%27t%20good%20enough.%20Almost%206%20months%20later%20and%20I%20would%20like%20to%20take%20back%20that%20comment.%20Titanium%20is%20no%20longer%20an%20embedded%20web%20browser%20insi" title="Google Bookmarks" onclick="pageTracker._trackPageview('/outgoing/www.google.com/bookmarks/mark?op=edit_amp_bkmk=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F07_2F13_2Fbuilding-mobile-app-with-titanium_2F_amp_title=Building_20a_20native_20mobile_20app_20with_20Titanium_20in_20less_20than_20an_20hour_amp_annotation=_0A_0AWhen_20I_20wrote_20my_20last_20blog_20entry_2C_20I_20favoured_20objective-c_20over_20Appcelerator_27s_20Titanium_20framework_20thinking_20it_20just_20wasn_27t_20good_20enough._20Almost_206_20months_20later_20and_20I_20would_20like_20to_20take_20back_20that_20comment._20Titanium_20is_20no_20longer_20an_20embedded_20web_20browser_20insi&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.givp.org/blog/2010/07/13/building-mobile-app-with-titanium/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>iPhone App Development with Google App Engine</title>
		<link>http://www.givp.org/blog/2010/02/14/iphone-app-dev-google-app-engine/</link>
		<comments>http://www.givp.org/blog/2010/02/14/iphone-app-dev-google-app-engine/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 12:11:04 +0000</pubDate>
		<dc:creator>Giv</dc:creator>
				<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Objective-c]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[appengine]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[memcached]]></category>

		<guid isPermaLink="false">http://www.givp.org/blog/?p=138</guid>
		<description><![CDATA[After almost a year of messing around with various iPhone development alternatives such as Phonegap and Titanium, I finally decided to learn Objective-C and do it all properly. I actually think those other frameworks are brilliant as they allow you to use familiar languages like Javascript to quickly create nice apps for both iPhone and [...]]]></description>
			<content:encoded><![CDATA[<p>After almost a year of messing around with various iPhone development alternatives such as <a href="http://phonegap.com/" onclick="pageTracker._trackPageview('/outgoing/phonegap.com/?referer=');">Phonegap</a> and <a href="http://www.appcelerator.com/" onclick="pageTracker._trackPageview('/outgoing/www.appcelerator.com/?referer=');">Titanium</a>, I finally decided to learn Objective-C and do it all properly. I actually think those other frameworks are brilliant as they allow you to use familiar languages like Javascript to quickly create nice apps for both iPhone and Android. But since they rely heavily on the web view element for loading HTML, creating sophisticated apps like Skype would be impossible.</p>
<p>So I set out to create an app for the iPhone with Objective-C. My app is pretty simple. It basically pulls in RSS news, audio podcast and video podcast feeds into a <a href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html" onclick="pageTracker._trackPageview('/outgoing/developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html?referer=');">UITableView</a> list, allowing the user to read, listen and watch news stories from the <a href="http://www.democracynow.org/" onclick="pageTracker._trackPageview('/outgoing/www.democracynow.org/?referer=');">Democracy Now!</a> website.</p>
<p><img class="alignright" title="Democracy Now! app" src="http://www.givp.org/democracyapp/img/democphone.png" alt="" width="164" height="305" /><br />
I managed to put together the app pretty quickly but I ran into a lot of issues when I tried to parse and massage the XML data. For starters, cocoa does not have native support for regular expressions (but there are several external libraries). I wanted to clean up the content I was getting back before displaying it to the user but I soon realised something that would normally take me a few minutes in Python/PHP/Javascript would take a lot longer in Objective-C. Parsing XML using <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html" onclick="pageTracker._trackPageview('/outgoing/developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html?referer=');">NSXMLParser</a> was an absolute nightmare and extremely slow. I rarely work with XML these days and find JSON a much easier protocol to deal with. I even tested the app with some sample JSON data using the excellent <a href="http://code.google.com/p/json-framework/" onclick="pageTracker._trackPageview('/outgoing/code.google.com/p/json-framework/?referer=');">json-framework</a> libarary and it was much easier and faster. Alas, I only had RSS feeds to work with.</p>
<p>The other problem I ran into was slow HTTP requests. It would sometimes take up to 20 seconds just to load the first screen. This was due to a combination of slow connection speeds, long response times from the data provider and a slow XML parser.</p>
<p>The solution I came up with was to do as little as possible in the phone app as far as the data was concerned. I decided to use Google App Engine to fetch the data from the source, parse, rejig, massage and beautify in Python, then serialise and return the results in JSON to the phone app to use.</p>
<p>It may sound like this would increase response times even more since the phone would have to first call GAE, then GAE would need to call the data source and then all the way back to the phone. This is true, however, once the data is with GAE we have the luxury of using <a href="http://code.google.com/appengine/docs/python/memcache/usingmemcache.html" onclick="pageTracker._trackPageview('/outgoing/code.google.com/appengine/docs/python/memcache/usingmemcache.html?referer=');">memcache</a> and <a href="http://code.google.com/appengine/docs/python/datastore/" onclick="pageTracker._trackPageview('/outgoing/code.google.com/appengine/docs/python/datastore/?referer=');">datastore</a>. The RSS and podcast feeds are updated once a day so there&#8217;s no reason to request the data from the source every time the user loads the app. Because each time we have to make the HTTP call, parse the data and load it up. This is extremely slow and unnecessary. We can just make one request a day, then parse, cleanup and cache the results for the next user that requests it.</p>
<p>So the app only talks to GAE. GAE first checks memcache to see if we have a cached version. If we don&#8217;t, it will make the HTTP call, fetch the data, parse, serialise, cache and return results. If we do have a cached version, there&#8217;s nothing else to do but to return the data. A cron job will also run every 24 hours to make sure memcache is up to date.</p>
<p><img src="http://givp.mugal.org/blog/wp-content/uploads/2010/02/bloggae.gif" alt="" /></p>
<p>If you really want a solid and reliable app, you need to think about all the edge cases also. What happens if the cache expires and the data provider&#8217;s website is down? At that exact moment a user loads the app only to get an error message saying there&#8217;s nothing to show. An unlikely scenario but not impossible. So the way I got around this issue was to store the serialised JSON output in GAE&#8217;s datastore as well. We always use the data from memcache but should memcache be empty and the data source down, we can switch over to the datastore and load yesterday&#8217;s content instead. Not ideal but better than having a broken app.</p>
<p>This is a bit of an overkill for such a simple app but it&#8217;s super fast and efficient and will work well for almost any app that relies on 3rd-party APIs. To be fair, it was my lack of experience with Objective-C that led me to using GAE. I feel much more comfortable in Python than Objective-C and I&#8217;m sure an experienced cocoa developer would have no problems parsing and massaging data in the app itself.</p>
<p>Of course there is one other edge case &#8211; Google App Engine could go down or worst, the interwebz could break. In which case, a simple error message will suffice.</p>
<p>You can download <a href="http://itunes.apple.com/app/democracy-now-war-peace-report/id353540657" onclick="pageTracker._trackPageview('/outgoing/itunes.apple.com/app/democracy-now-war-peace-report/id353540657?referer=');">Democracy Now! app on iTunes</a></p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Share and Enjoy:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow"  href="http://twitter.com/home?status=iPhone%20App%20Development%20with%20Google%20App%20Engine%20-%20http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F02%2F14%2Fiphone-app-dev-google-app-engine%2F" title="Twitter" onclick="pageTracker._trackPageview('/outgoing/twitter.com/home?status=iPhone_20App_20Development_20with_20Google_20App_20Engine_20-_20http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F02_2F14_2Fiphone-app-dev-google-app-engine_2F&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F02%2F14%2Fiphone-app-dev-google-app-engine%2F&amp;t=iPhone%20App%20Development%20with%20Google%20App%20Engine" title="Facebook" onclick="pageTracker._trackPageview('/outgoing/www.facebook.com/share.php?u=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F02_2F14_2Fiphone-app-dev-google-app-engine_2F_amp_t=iPhone_20App_20Development_20with_20Google_20App_20Engine&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F02%2F14%2Fiphone-app-dev-google-app-engine%2F&amp;title=iPhone%20App%20Development%20with%20Google%20App%20Engine&amp;bodytext=After%20almost%20a%20year%20of%20messing%20around%20with%20various%20iPhone%20development%20alternatives%20such%20as%20Phonegap%20and%20Titanium%2C%20I%20finally%20decided%20to%20learn%20Objective-C%20and%20do%20it%20all%20properly.%20I%20actually%20think%20those%20other%20frameworks%20are%20brilliant%20as%20they%20allow%20you%20t" title="Digg" onclick="pageTracker._trackPageview('/outgoing/digg.com/submit?phase=2_amp_url=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F02_2F14_2Fiphone-app-dev-google-app-engine_2F_amp_title=iPhone_20App_20Development_20with_20Google_20App_20Engine_amp_bodytext=After_20almost_20a_20year_20of_20messing_20around_20with_20various_20iPhone_20development_20alternatives_20such_20as_20Phonegap_20and_20Titanium_2C_20I_20finally_20decided_20to_20learn_20Objective-C_20and_20do_20it_20all_20properly._20I_20actually_20think_20those_20other_20frameworks_20are_20brilliant_20as_20they_20allow_20you_20t&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F02%2F14%2Fiphone-app-dev-google-app-engine%2F&amp;title=iPhone%20App%20Development%20with%20Google%20App%20Engine&amp;notes=After%20almost%20a%20year%20of%20messing%20around%20with%20various%20iPhone%20development%20alternatives%20such%20as%20Phonegap%20and%20Titanium%2C%20I%20finally%20decided%20to%20learn%20Objective-C%20and%20do%20it%20all%20properly.%20I%20actually%20think%20those%20other%20frameworks%20are%20brilliant%20as%20they%20allow%20you%20t" title="del.icio.us" onclick="pageTracker._trackPageview('/outgoing/delicious.com/post?url=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F02_2F14_2Fiphone-app-dev-google-app-engine_2F_amp_title=iPhone_20App_20Development_20with_20Google_20App_20Engine_amp_notes=After_20almost_20a_20year_20of_20messing_20around_20with_20various_20iPhone_20development_20alternatives_20such_20as_20Phonegap_20and_20Titanium_2C_20I_20finally_20decided_20to_20learn_20Objective-C_20and_20do_20it_20all_20properly._20I_20actually_20think_20those_20other_20frameworks_20are_20brilliant_20as_20they_20allow_20you_20t&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F02%2F14%2Fiphone-app-dev-google-app-engine%2F&amp;title=iPhone%20App%20Development%20with%20Google%20App%20Engine&amp;annotation=After%20almost%20a%20year%20of%20messing%20around%20with%20various%20iPhone%20development%20alternatives%20such%20as%20Phonegap%20and%20Titanium%2C%20I%20finally%20decided%20to%20learn%20Objective-C%20and%20do%20it%20all%20properly.%20I%20actually%20think%20those%20other%20frameworks%20are%20brilliant%20as%20they%20allow%20you%20t" title="Google Bookmarks" onclick="pageTracker._trackPageview('/outgoing/www.google.com/bookmarks/mark?op=edit_amp_bkmk=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F02_2F14_2Fiphone-app-dev-google-app-engine_2F_amp_title=iPhone_20App_20Development_20with_20Google_20App_20Engine_amp_annotation=After_20almost_20a_20year_20of_20messing_20around_20with_20various_20iPhone_20development_20alternatives_20such_20as_20Phonegap_20and_20Titanium_2C_20I_20finally_20decided_20to_20learn_20Objective-C_20and_20do_20it_20all_20properly._20I_20actually_20think_20those_20other_20frameworks_20are_20brilliant_20as_20they_20allow_20you_20t&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.givp.org/blog/2010/02/14/iphone-app-dev-google-app-engine/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>KALX 90.7 FM iPhone App</title>
		<link>http://www.givp.org/blog/2010/02/09/kalx-90-7-fm-iphone-app/</link>
		<comments>http://www.givp.org/blog/2010/02/09/kalx-90-7-fm-iphone-app/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 12:30:37 +0000</pubDate>
		<dc:creator>Giv</dc:creator>
				<category><![CDATA[Objective-c]]></category>

		<guid isPermaLink="false">http://www.givp.org/blog/?p=142</guid>
		<description><![CDATA[My first iPhone/iPod Touch app is out! I&#8217;m waiting for the approval of a second app. When that&#8217;s done, I&#8217;ll do a proper post about all things Objective-C and iPhone SDK. KALX app on iTunes Share and Enjoy:]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" title="KALX app" src="http://www.givp.org/kalxapp/img/kalxphone.png" alt="" width="164" height="305" />My first iPhone/iPod Touch app is out! I&#8217;m waiting for the approval of a second app. When that&#8217;s done, I&#8217;ll do a proper post about all things Objective-C and iPhone SDK.</p>
<p><a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=354549285&amp;mt=8" onclick="pageTracker._trackPageview('/outgoing/itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=354549285_amp_mt=8&amp;referer=');">KALX app on iTunes</a></p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Share and Enjoy:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow"  href="http://twitter.com/home?status=KALX%2090.7%20FM%20iPhone%20App%20-%20http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F02%2F09%2Fkalx-90-7-fm-iphone-app%2F" title="Twitter" onclick="pageTracker._trackPageview('/outgoing/twitter.com/home?status=KALX_2090.7_20FM_20iPhone_20App_20-_20http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F02_2F09_2Fkalx-90-7-fm-iphone-app_2F&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F02%2F09%2Fkalx-90-7-fm-iphone-app%2F&amp;t=KALX%2090.7%20FM%20iPhone%20App" title="Facebook" onclick="pageTracker._trackPageview('/outgoing/www.facebook.com/share.php?u=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F02_2F09_2Fkalx-90-7-fm-iphone-app_2F_amp_t=KALX_2090.7_20FM_20iPhone_20App&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F02%2F09%2Fkalx-90-7-fm-iphone-app%2F&amp;title=KALX%2090.7%20FM%20iPhone%20App&amp;bodytext=My%20first%20iPhone%2FiPod%20Touch%20app%20is%20out%21%20I%27m%20waiting%20for%20the%20approval%20of%20a%20second%20app.%20When%20that%27s%20done%2C%20I%27ll%20do%20a%20proper%20post%20about%20all%20things%20Objective-C%20and%20iPhone%20SDK.%0A%0AKALX%20app%20on%20iTunes" title="Digg" onclick="pageTracker._trackPageview('/outgoing/digg.com/submit?phase=2_amp_url=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F02_2F09_2Fkalx-90-7-fm-iphone-app_2F_amp_title=KALX_2090.7_20FM_20iPhone_20App_amp_bodytext=My_20first_20iPhone_2FiPod_20Touch_20app_20is_20out_21_20I_27m_20waiting_20for_20the_20approval_20of_20a_20second_20app._20When_20that_27s_20done_2C_20I_27ll_20do_20a_20proper_20post_20about_20all_20things_20Objective-C_20and_20iPhone_20SDK._0A_0AKALX_20app_20on_20iTunes&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F02%2F09%2Fkalx-90-7-fm-iphone-app%2F&amp;title=KALX%2090.7%20FM%20iPhone%20App&amp;notes=My%20first%20iPhone%2FiPod%20Touch%20app%20is%20out%21%20I%27m%20waiting%20for%20the%20approval%20of%20a%20second%20app.%20When%20that%27s%20done%2C%20I%27ll%20do%20a%20proper%20post%20about%20all%20things%20Objective-C%20and%20iPhone%20SDK.%0A%0AKALX%20app%20on%20iTunes" title="del.icio.us" onclick="pageTracker._trackPageview('/outgoing/delicious.com/post?url=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F02_2F09_2Fkalx-90-7-fm-iphone-app_2F_amp_title=KALX_2090.7_20FM_20iPhone_20App_amp_notes=My_20first_20iPhone_2FiPod_20Touch_20app_20is_20out_21_20I_27m_20waiting_20for_20the_20approval_20of_20a_20second_20app._20When_20that_27s_20done_2C_20I_27ll_20do_20a_20proper_20post_20about_20all_20things_20Objective-C_20and_20iPhone_20SDK._0A_0AKALX_20app_20on_20iTunes&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.givp.org%2Fblog%2F2010%2F02%2F09%2Fkalx-90-7-fm-iphone-app%2F&amp;title=KALX%2090.7%20FM%20iPhone%20App&amp;annotation=My%20first%20iPhone%2FiPod%20Touch%20app%20is%20out%21%20I%27m%20waiting%20for%20the%20approval%20of%20a%20second%20app.%20When%20that%27s%20done%2C%20I%27ll%20do%20a%20proper%20post%20about%20all%20things%20Objective-C%20and%20iPhone%20SDK.%0A%0AKALX%20app%20on%20iTunes" title="Google Bookmarks" onclick="pageTracker._trackPageview('/outgoing/www.google.com/bookmarks/mark?op=edit_amp_bkmk=http_3A_2F_2Fwww.givp.org_2Fblog_2F2010_2F02_2F09_2Fkalx-90-7-fm-iphone-app_2F_amp_title=KALX_2090.7_20FM_20iPhone_20App_amp_annotation=My_20first_20iPhone_2FiPod_20Touch_20app_20is_20out_21_20I_27m_20waiting_20for_20the_20approval_20of_20a_20second_20app._20When_20that_27s_20done_2C_20I_27ll_20do_20a_20proper_20post_20about_20all_20things_20Objective-C_20and_20iPhone_20SDK._0A_0AKALX_20app_20on_20iTunes&amp;referer=');"><img src="http://www.givp.org/blog/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.givp.org/blog/2010/02/09/kalx-90-7-fm-iphone-app/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
