Flaviu Simihaian's Blog - Entrepreneur and Developer

iOS: Nested JSON POST With RestKit

Say you want to send some JSON from your RestKit enabled iOS app. You want your JSON to look like this:

First, make the corresponding NSDictionary structure:

Then, create the JSON string from your NSDictionary

Finally, make the request and map the response to a model:

Hope this helps.

Architecture Saturday Part I : Classical

Have you ever visited some historical place like Rome with someone marveling at the domes and cathedrals?

You probably said something like, “Wow! I wonder how they built that.” To which your mate would reply, “wanna get some jaylahtoh?” and you’d both walk on in the colonnade.

Don’t you wish you had said, “that plinth is characteristic of the corinthian style”? No? Ok.

Well, I want to learn more about architecture and here’s some notes I found interesting.

Egyptian pyramids (like Giza) are marvels of architecture because the massive blocks were barged from quarries, dragged on sleds, then up mud ramps. One invention that would have made this task more manageable?

The pulley:

Remember: Parthenon -> Greece. Pantheon -> less badass Roman building.

The Parthenon had a timber roof, which collapsed during various fires. However, the reason it doesn’t have a roof today is that the Turks used it to store gun powder and it exploded during the Venetian bombardment in the 17th century.

The Parthenon does not have a straight line in it. They used the process of entasis, which gives columns a slight bulge to make them seem straight to the eye.

Also the parthenon was not boring-colored back in the day. It most likely radiated bright red, blue and gold. It’s also belongs to the Doric Order.

There are three main orders. Doric Order, a simpler style with a classy frieze above the plain architrave, the Ionic order, with a slightly more ornate capital (that’s the decoration at the top of the column), and the Corinthian Order, which has crazy decoration on the entire entablature as well as the capital.

Here’s a fuzzy pencil drawing a made to tell the difference:

Meanwhile (a few hundred years later really), the Romans mixed volcanic soil with lime and made concrete, which they used to create the Pantheon, a 139 ft dome, in which you could fit a giant soccer ball.

Actually, nobody built a larger dome until Brunelleschi build the Florence dome during the Renaissance.

Concrete allowed the Romans to build large unsupported spaces (note that this is not the steel reinforced concrete we use today, so it couldn’t support too much weight).

Concrete and arches looked really boring, so after conquering the Greeks, the Romans borrowed their decorative column style in the form of pilasters, which are fake columns carved out of the wall. We use them all the time to make our homes look majestic:

As a last note, the Roman Emperor Trajan built a column after his victories against the Dacians (modern day Romania, my homeland).

While the column’s frieze depicts my slaughtered ancestors, the base of the column has fine lettering, inspiring fonts like the one you are reading right now.

Seeding CoreData With RestKit

I’ve recently written a post about seeding your SQLite database into Core Data.

Fortunately, @SteveMoser pointed out I should be using RestKit.

RestKit includes a Restful HTTP Client, Object Mapping, and a bunch of other stuff, like Database Seeding. Big monsters typically scare me, but I gave it a try because I really needed a better way to seed my data.

First thing’s first: you need to set up RestKit for your project. I won’t go into the details, because they are here.

Once RestKit is installed, here’s what you need to do to seed your database from JSON:

I. Create a new target for your application. Call it “Generate Seed” or something.

The way to create a new target is to click on your project, and in the “Summary”, click on Add Target:

II. Click on the newly created target, go to “Build Settings” and search for “macros”. Double click on the value for “Preprocessor Macros” and overwrite whatever is in there with “RESTKIT_GENERATE_SEED_DB” (without the quotes, obviously). Click Done.

III. Now we need to modify out AppDelegate.m a bit. First, import the RestKit.h and RestKit’s CoreData:

#import <RestKit/RestKit.h>
#import <RestKit/CoreData.h>

Here’s what your appFinishLaunchingWithOptions might look like (this is mostly from RestKit’s example Twitter project):

IV. The data we’re seeding will need to be in JSON. So, in our case, I dragged a statuses.json file in XCode and copied it there (make sure it’s in the Seed Target, as we have no use for it in our main Target).

But what is in that JSON file?

Basically this:

"created_at":"Sat Mar 05 13:39:09 +0000 2011",
"text":"Meaningful tweet about Nihilism"
"created_at":"Sat Mar 06 13:39:09 +0000 2011",
"text":"Letdown tweet after the last retwat"

RestKit uses the mappings in the code above to map each JSON field to your CoreData model. Then there’s a little more ceremony, and we’re done.

Not really. We haven’t run it yet.

V. To generate the seed database, you’ll want to run the seed target we created. You do that by clicking on the top left between the stop button and the device you select. Run it in the simulator, not on the device.

When you build your code, you may get some weird Mach-O Linker errors like this:

Best thing I’ve found was to delete the seed target and recreate it.

If that doesn’t work, you may try manually deleting the Seed Database and CoreData database in the file system.

After it runs successfully, it will print in the console a message to copy the sqlite seed database it just generated to your project. Open that folder in the Finder and drop it in your project.

You should now be able to run your main target and the seed data will be there.

Next time you change your model or seed data, you just need to delete the Seed App from your simulator, run the seed target again, and copy the seed database it creates to your project.

Hope this helps. mobile tuts+ also has a nice tutorial that might help with some of this stuff.

Float Search Bar to Top of UITableView in iOS

Don’t you wish you could scroll in the image above and keep the search bar floating at the top?

Two steps:

Uno. Add the UIScrollViewDelegate protocol to your interface:

Dos. Implement the scrollViewDidScroll protocol method:

Done. So easy.

But I kind of also want the search text field to be larger (increase its height). How hard can that be?

Well, as it turns out, pretty hard. After spending 1+ hour researching posts like this one, I gave up. I guess it looks OK the way Steve Jobs designed it.

Welcome to iOS.

Cannery Row - Steinbeck’s Startup Novel

Thanks to Chris Halligan for recommending this book to me.

Reading John Steinbeck through the startup lens makes me notice Henri, a character who has been building a boat for seven years because every time he nearly finishes it, he changes it and starts over again:

"But suppose he finishes his boat. Once it's finished people will say, 'Why don't you put it in the water?' Then if he puts it in the water, he'll have to go out in it, and he hates the water. So you see, he never finishes the boat--so he doesn't ever have to launch it."

Makes me think Henri read the Lean Startup. That’s right, I just linked to my own blog post.

Here’s Steinbeck on Sales/Marketing:

"People didn't like him for telling the truth. They scowled, or shook and tapped their heads, they laughed as though they knew it was a lie and they appreciated a liar."

Just kidding, I love marketing. As does Doc (the main character):

"[He] still loved true things but he knew it was not a general love and it could be a very dangerous mistress."


"The things we admire in men, kindness an generosity, openness, honesty, understanding and feeling are the concomitants of failure in our system. And those traits we detest, sharpness, greed, acquisitiveness, meanness, egotism and self-interest are the traits of success. And while men admire the quality of the first they love the produce of the second."

Or, in Richard Frosts’ words, “who wants to be good if he has to be hungry too?”

Perhaps the best part of the book is a story in the last few pages. It’s about a gopher that took up residence in the low weeds in Cannery Row. He was a beautiful gopher and found the perfect place for a gopher hole.

The earth was black and soft yet with a little clay in it so that the tunnels didn’t cave in. There were no gardens nearby so no one would think of setting a trap near him. He built his palace perfectly and stocked it with plenty of food.

Every morning he sat in the entrance of his hole and made penetrating squeaks only audible to other gophers. And still no female appeared. He did this for weeks, and still nothing.


"he had to move two blocks up the hill to a dahlia garden where they put out traps every night."

Maybe I’m reading too much into the gopher story…or maybe you’re the gopher and the startup you should be working on is in the dahlia garden.

Load SQLite Db Into Core Data in iOS 5

UPDATE: I’ve tried using RestKit to seed my db and it’s much easier. I wrote a blog post about it if you’re interested.

Many Model-View-Controller frameworks have a way to specify seed data (initial data for an application to function).

iOS does not. So we have to create another app that builds the database, and then have the main app copy that database and set it as default when the app first loads.

Let’s create that new app and call it UtilityApp and make sure it has the same Core Data Model as our main app:

Copy the SQLite database (which you want to use to populate our Core Data) in the “Supporting Files” in XCode:

Now either in your AppDelegate.m or in a new class, you need to populate your Core Data with the sqlite database data. I made a class called DrugFetcher.h with a public method:

+ (void) populateDrugsInManagedObjectContext:(NSManagedObjectContext *)context;

We need to import sqlite3:

#import "/usr/include/sqlite3.h"

However, to have access to that, we need to add it from the “Build Phases” tab of our project:

Here’s my DrugFetcher.m

The script above opens the SQLite db, and adds each row to a model.

Now the “seed” database is prepared. We need get it from the UtilityApp and add it to our main app’s resources. You can run the Utility app on your desktop and then find the path where your Application is running and copy the db from there.

I prefer to ssh into the device just for my peace of mind that it was created on the target device. You’ll need to jailbreak your device to ssh into it, a process which I covered here.

To copy the file you run something like this on your device:

scp /var/mobile/Applications/LONG-APP-CODE-FROM-DEBUGGER/Documents/StoreContent/persistentStore user@your_machine:~/path/to/app/

On your computer, I recommend opening the ‘persistentStore’ with sqlite3 and making sure there are no extra tables, and delete all the contents of the Z_METADATA table. I’ve run into issues with this before.

$ sqlite3 persistentStore
delete * from Z_METADDATA;

Add the persistentStore (a SQLite db) to the main app Supporting Files.

Now, in your main app, you’ll want to change your AppDelegate a bit to make sure it uses the new db. If it’s the first time the app is loaded, it will copy the persistentStore to Documents/CoreDataStore.sqlite. From then on, it will use CoreDataStore.sqlite as its data store.

We’ll use NSPersistentStoreCoordinator to achieve this. Here’s my AppDelegate.h:

The copying on first load is achieved in the persistentStoreCoordinator method, like so:

Then, in the same persistentStoreCoordinator method, we add our new store to the persistentStoreCoordinator:

Here’s the full AppDelegate.m for reference:

I realize this gets confusing, so ask questions below. I’ve wasted plenty of my time loading data, so maybe this post saves some of yours.

Max Field Length From a Data File

Sometimes you have a .csv or a pipe delimited file that you need to insert into a database. You need to create the table using SQL and you’re not sure whether to use VARCHAR(40) or VARCHAR(100).

If only you could quickly tell what the longest field for each column was…

Here’s a script to do that:

import csv, sys

lengths = {}

with open(sys.argv[1], 'rt') as f:
    reader = csv.DictReader(f, delimiter='|') #pipe separated
    for index, row in enumerate(reader):
        if index == 0:
            for heading in row.keys():
                lengths[heading] = 0 #set the dict headings
        for column in row.keys():
            if (len(row[column]) > lengths[column]):
                lengths[column] = len(row[column]) #add length
    print lengths

Then, just run it from the command line:

$ python longest_field.py random_file.txt

Hope this helps.

AJAX Autocomplete Search With Django and jQuery

Let’s say you have some data that you want to autocomplete in a text field as the user types.

This is what your Django model might look like if you were searching for drug names:

from django.db import models
class Drug(model.Model):
    rxcui = models.IntegerField()
    short_name = models.CharField(max_length=50)
    is_brand = models.IntegerField(max_length=1)

It’s very likely that your data is in a different format (csv or pipe delimited), like this:

Rxcui|Short Name|Is Brand
1234 |Advil     |1

So we need to import that data. A database copy command (postgres, mysql, sqlite) will not deal very nicely with our Django auto-incrementing keys, so we need to add the data using a django view, such as this:

import csv
from myproject.main.models import Drug
def load_drugs(file_path):
    "this loads drugs from pipe delimited file with headers"
    reader = csv.DictReader(open(file_path))
    for row in reader:
        drug = Drug(rxcui=row['Rxcui'], short_name=row['Short Name'], is_brand=row['Is Brand'])

Now load the django console and import the data:

$ python manage.py shell
$ from main.view import load_drugs
$ load_drugs('/absolute/path/to/pipe/file.txt')

Cool. Now we need to actually write the template where the search is. We’ll use jQuery Autocomplete because that’s what the cool kids do. Let’s first import jQuery and jQueryUI in our base template:

<link rel="stylesheet" href="http://code.jquery.com/ui/1.8.18/themes/base/jquery-ui.css" type="text/css" media="all" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript">
</script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script>

Now let’s add this to the template we want the search field on:

<div class="ui-widget">
  <label for="drugs">Drugs: </label>
  <input id="drugs">

Our own javascript to get the autocomplete is this (you can put it anywhere after the jQuery imports):

$(function() {
    source: "/api/get_drugs/",
    minLength: 2,

Where is the ajax call happening? jQuery Autocomplete does it for us. But how does it know which fields to get? It doesn’t! Let’s implement the url:

url(r'^api/get_drugs/', 'myproject.main.view.get_drugs', name='get_drugs'),

And now,the real work. Our view:

def get_drugs(request):
    if request.is_ajax():
        q = request.GET.get('term', '')
        drugs = Drug.objects.filter(short_name__icontains = q )[:20]
        results = []
        for drug in drugs:
            drug_json = {}
            drug_json['id'] = drug.rxcui
            drug_json['label'] = drug.short_name
            drug_json['value'] = drug.short_name
        data = json.dumps(results)
        data = 'fail'
    mimetype = 'application/json'
    return HttpResponse(data, mimetype)

Note that jQuery autocomplete sends the query as “term” and it expects back three fields: id, label, and value. It will use those to display the label and then the value to autocomplete each drug.

We now have an AJAX jQuery Autocomplete Search with Django. Hope this helps.

Pair Programming With Tmux Screencast


Tmux Home

Man Page

Screencast by Peter Cooper

Tmux Rant y Bryan Liles

Funny, the changelog episode this week also covered tmux. So, check it out.

My ~/.tmux.conf

# use vi mode
setw -g mode-keys vi

# remap prefix to Control + a
set -g prefix C-a
unbind C-b
bind C-a send-prefix

# force a reload of the config file
unbind r
bind r source-file ~/.tmux.conf

# quick pane cycling with Ctrl-a
unbind ^A
bind ^A select-pane -t :.+

# move around panes like in vim (only in tmux 1.6)
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
bind h select-pane -L

# Sane scrolling
set -g mode-mouse on

Also, note that Ctrl-A D will let you view and detach other clients. :)

On the Sorrows of Young Werther

Everyone (almost) raves about Johann Wolfgang von Goethe nowadays.

Despite my reluctance to read in translation, I Prime Shipped myself an Everyman’s Library copy of his Selected Works for Christmas.

Goethe wrote The Sorrows of Young Werther when he was my age, 24.

Werther is a young well-educated artist admiring the idyllic German countryside. He falls in love with Lotte, a beautifully positive woman engaged to some doofus. They marry.

Werther comes to visit more often, attaching himself evermore to Lotte, until he can no longer bear the pain of her unrequited love and he meticulously kills himself.

A banal story were it not for the ease with which the reader identifies with Werther.

The story is mostly in his intelligent letters to his friend:

“I have a deep respect for religion, you know that; I feel that it is a support for man’s weary soul, a comfort for many who die of thirst. Only – can it, must it, be that for everyone? ”

Werther is lonely, and he is aware of the effect solitude has on him: “Our fortune or misfortune depends on the objects and persons to which we compare ourselves, and for that reason nothing is more dangerous than solitude. Our imagination, by its nature inclined to exalt itself, and nourished by the fantastic imagery of poetry, creates a series of beings of which we are the lowest, so that everything else appears more wonderful, everyone else more perfect.”

This reminds me of how entrepreneurs feel when working on an idea or a coder building a product by themselves.

Werther seems to be speaking to a Stanford Entrepreneurship class when saying, “all unusual people who have accomplished something great or seemingly impossible have always been proclaimed to be drunk or mad.”

Moreover, Goethe went viral after publishing it. The Sorrows of Young Werther was an extraordinary and immediate bestseller in Germany and abroad. It even caused some of the earliest recordings of copycat suicides (that’s where people start committing suicide more to emulate their hero).

Funnier still, Goethe renounced the book and the Sturm and Drang movement he started (and which went on to become Romanticism), calling it a “sickness.” Goethe abandoned his young passion and turned towards “higher” artistic and classical values.

On the other hand, Werther believed “nothing in the world makes a person indispensable but love.” :

“He admires my intelligence and my talents more than my heart, which is, after all, my only pride, and the fountainhead of all - all strength, happiness, and misery. Anyone can know what I know. My heart alone is my own.”

I think Werther was an entrepreneur. He had a startup he was extremely passionate about. But he did not know when to pivot.

Goethe did.

Fork Flaviu on GitHub