When developing iOS apps, you can be faced with a lot of choices. Choices can lead to tradeoffs. One of these tradeoffs can come about when deciding on a way to store your data. At Spatial Vision we were faced with this very choice when developing our flagship iOS app, Mapscape. One of the core features of Mapscape is the ability to search for any road in the state of Victoria. There are a lot of roads in Victoria and we needed something that had very fast search capabilities.
We decided to look at a technology that was familiar and supported by iOS out of the box: SQLite. SQLite is easy to use directly in iOS, especially with a nice Objective-C wrapper like FMDB. We tried it out and it was good for a database that had a couple of thousand lines in it, but once we loaded our entire data set it seemed a bit sluggish.
Looking for a solution, we found the SQLite FTS extensions. This was encouraging, in that we wouldn't have to replace our existing implementation, just modify it slightly. Though we soon came across another stumbling block. Even though SQLite is built into iOS, it turns out FTS support is not. To get this benefit we would have to compile our own static library.
Here's how we got a custom SQLite library into our app:
1. Get the SQLite source code.
We used the SQLite amalgamation. This puts all of the code required for the SQLite library into 2 files: sqlite3.h and sqlite3.c.
2. Create a new Xcode project.
This project should be a Cocoa Touch Static Library project. This will allow you to embed the SQLite library you build in other projects by just including the project.
3. Add the SQLite files.
Add all the files from the SQLite amalgamation download. Remove any source files added by Xcode.
4. Enable FTS.
To enable FTS in SQLite all you have to do is add the following to the sqlite3.h file:
Compile the library and check that it builds.
5. Add to an Xcode project.
To add the your new SQLite to a new or existing Xcode project, open the project you want to add it to then simply add your SQLite xcodeproject as a sub project. You'll want to make sure your project is no longer importing the default iOS SQLite framework and import your new one. You can do this by clicking on your parent project and clicking on Build Phases. You'll be able to see a list of frameworks being linked to your application under the "Link Binary With Libraries" build phase. Make sure you remove sqlite3.dylib and add your libSQLite.a.
And there you have it. You now have a custom build of SQLite embedded in your application.
Joshua Spry, Mobile Platform Developer