Codetalk

Speeding up SQL on the iPhone

So, while using %LIKE% is a good option for a small dataset, the iPhone doesn’t have the CPU power to trawl through a large dataset quickly. Using PHP with sqlite, we took the large data set which has a description field in it, and split it down to a type of index table. With this, we were able to use the = and get better accuracy.

Example:

Field has index of 34 and a data field of “this is the best movie we have seen”

Our PHP script split this into another table which the main search looks at:

34 this
34 is
34 the
34 best

34 seen

As you can see, we could look for ‘best’, get the index of 34 and pull out the result very quickly. We hope this is useful to someone out there!

LIKE % makes for a slow SQL search

For those who don’t know, SQLite has a full text search module in the full distribution – but on the iPhone, it isn’t there. This means that you have to resort back to good old fashioned LIKE %word% in your SELECT statements. Because LIKE doesn’t speed up when using indexes (it is a full text compare) – it is fairly slow compared to a direct =

When planning your SQLite implementations on the iPhone, make sure you have your data structured in the best possible way for LIKE to fly over it!

Its a SQL weekend!

For those keen on a bit of SQLite for iPhone (or whatever device), check out the http://sqlite.org website.
This is also quite interesting…

Is the iPhone online?

For a current project, I needed to know whether an iPhone or iPod Touch was ‘online’. The code revealed itself in Reachability from the Apple Sample Code. Really really easy!

Add Reachability.m and .h to your project, define a Reachability class, and then add code like below to your project:


online = TRUE;

hostReach = [[Reachability reachabilityWithHostName: @"www.site.com"] retain];
NetworkStatus netStatus = [hostReach currentReachabilityStatus];
switch (netStatus)
  {
     case NotReachable:
       {
          online= FALSE;
       }
  }

If(online)
  {
      //Then do online stuff!
  }