Thread safe database with FMDB (SQLite) as alternative to Core Data

The apps I developed recently required saving and loading thousands of objects within a very short time on multiple threads, mostly for reasons of caching and offline mode support. I started implementing this behavior in Core Data, which caused a few problems related to thread safety and performance.

I tried using FMDB as an alternative to Core Data, which works surprisingly great. There is not much information about multithreading and thread safety available, that’s why I’m writing this blog post.

To apply the MVC principal, I assume you create a class just for reading/writing into/from the database. You should never access the database directly from outside of it.

After trying different approaches of multithreading, this turns out to be the best solution for my projects.

First of all you need a database queue (provided by FMDB), an NSOperationQueue to queue your queries and a lock for the database.

static FMDatabaseQueue *_queue; 
static NSOperationQueue *_writeQueue; 
static NSRecursiveLock *_writeQueueLock;

If you use a singleton for your database, set up your static variables.

 _queue = [FMDatabaseQueue databaseQueueWithPath:...]; 
 _writeQueue = [NSOperationQueue new]; 
 [_writeQueue setMaxConcurrentOperationCount:1]; 
 _writeQueueLock = [NSRecursiveLock new];

Basically there are 2 use cases now: Read from database and Write to database

To read, all you have to is:

[_writeQueueLock lock];
[_queue inDatabase:^(FMDatabase *db) {
    FMResultSet *res = [db executeQuery:@"..."];
    if ([res next]) {
        ...
    }
    [res close];
}];
[_writeQueueLock unlock];

To write into the database, you have to use the writeQueue:

[_writeQueue addOperationWithBlock:^{
    [_writeQueueLock lock];
    [_queue inDatabase:^(FMDatabase *db) {
        if (![db executeUpdate:@"..." withArgumentsInArray:...]) { ... }
    }];
    [_writeQueueLock unlock];
}];

That’s all you need to really easily save/read data using FMDB. I use a little wrapper that matches properties with the according columns inside the database.

Tags: Core Data, CoreData, fast, FMDB, iOS, iPhone, Multithreading, performance, speed, SQLite, Thread, Threads, Threadsafe   |   Edit on GitHub

SunApps - Native iOS & Android Apps

In the time frame from September 2013 until September 2015 I founded a company called SunApps GmbH.

The Idea

Build a system that allows sport clubs to better communicate with their members. In 2013, all communication still happened through physical mail, or Facebook posts. During that time, Facebook already started hiding posts from followers, so there was a need to better reach members. Using a mobile app, the club could send messages directly to their member’s phones.

The Product

  • Fully native iOS & Android app
  • Real-time Over The Air updates
    • Owner has their own app that shows WIP content changes
    • Owner has a separate “Release” button to rollout changes to the users
  • URL Share Scheme that works across mobile & Desktop
  • Push Notifications
  • 3D Birdiebook for golf clubs
  • Weather Integration
  • A CMS based on Ruby on Rails, fully flexible and customizable, nested menus
  • Using Facebook pages as data source, automatic syncing of content, photos & videos
  • Creating a new native app based on a Facebook page within minutes just by providing the URL
    • Automatic analysing of logo to use prominent colors for the app’s theme
    • Within minutes, the potential customer would have the new app on their phone
    • Automatic creation of screenshots, and submission to the Apple App Store and Google Play
  • POI maps
  • Multi-language support built-in everywhere
  • Recovery mode built in: If a remote change breaks the app for whatever reason, it automatically resets to the last known good state
  • 100% offline mode support
  • QR Code integration
  • Integration of third party web content, with support of injecting additional JS/CSS code to further customize third party content (e.g. external tee time booking systems)

Screenshots

Conclusion

The company was shut down, due to difficulties in Sales. The technical implementation of SunApps was excellent, especially considering this was all built in 2013 it was way ahead of the times. However we had no sales experience, and many sport clubs didn’t understand the need for a mobile app.

Edit on GitHub

Bikemap 3.0

The first version of the Bikemap iPhone app was released a few years ago. It was developed using Appcelerator Titanium back then. The new Bikemap app was designed and built from scratch. It’s really fast and has a lot of nice new features like tracking of a biking route right from the iPhone.

Download Bikemap

Tags: App, Application, Bikemap, Fahrrad, iPad, iPhone, Touren   |   Edit on GitHub