Fork us on GitHub

Threadsafe SQLite

Making embedded SQL usable on iOS
Threadsafe SQLite

Threadsafe SQLite

One of the main reasons for the thread API I discussed yesterday is a new threadsafe Database API. This new API allows you to wrap your Database instance with a thread that will hide all access to the database and implicitly make it threadsafe by serializing all requests to a single database thread.

This also has the side effect of removing the hack of working with sqlite on the EDT which could sometimes cause a stutter in the UI when performing SQL. In fact, that was the chief motivation for this particular enhancement!

You can use the new threadsafe database API with a single line:

db = new ThreadSafeDatabase(db);

It’s crucial that once you do this you never use the old db object again!

The reason we need this whole thing is that iOS has a builtin version of sqlite that is thread unsafe at a problematic level. E.g. the garbage collector runs on a separate thread and can cause issues if an object is left dangling.

Share this Post:

Posted by Shai Almog

Shai is the co-founder of Codename One. He's been a professional programmer for over 25 years. During that time he has worked with dozens of companies including Sun Microsystems.
For more follow Shai on Twitter & github.