Xamarin Crash on iOS only on SQLite update call for model Xamarin Crash on iOS only on SQLite update call for model sqlite sqlite

Xamarin Crash on iOS only on SQLite update call for model


I know a lot of time have passed from your question but it was one of the few places where I was able to find similar problem (like mine), so I just wanted to share what resolved it in my case.

The problem:

I was getting the exception in different methods that use the database connection from different threads and that led me to believe that SQLite in iOS is for some reason not thread safe, like Android and WinRT (I am using the same code which has some locks and mutexes but methods using deferred queries were causing the issue after the lock is released).

So what fixed it for me was using additional parameter in the connection constructor that enables thread safety of SQLite (Or at least that's what I think is happening :) )

The solution:

var connection = new SQLiteConnection(      new SQLite.Net.Platform.XamarinIOS.SQLitePlatformIOS(),  sqliteFilename,  openFlags: SQLiteOpenFlags.ReadWrite|SQLiteOpenFlags.FullMutex|SQLiteOpenFlags.Create,  storeDateTimeAsTicks: true);

The important thing here is the flag - FullMutex which I assume is not turned on by default in iOS unlike in Android and WinRT

Hope it helps somebody else, and happy coding to all ;)


Try Downloading this package "SQLite.Net PLC" under tools -> Nuget package manager, it worked for me


If anyone else is having this issue on iOS this could help you.

I am using Xamarin Forms and my project works just fine on Android but does not work on iOS. Thats because For iOS, bundle_green (which is a dependency for the Microsoft.data.sqlite) uses the sqlite library that is part of iOS itself, and that library is (apparently) NOT compiled with serialized as the threading mode.

For Android, bundle_green includes a different instance of the sqlite library, instead of using the one from Android, since Android N doesn't allow that anymore. This bundled instance is compiled with serialized threading mode.

You can check the ThreadingMode being used using this snippet

var result = SQLitePCL.raw.sqlite3_threadsafe();string description = "";switch (result){    case 1:        // Thread-safe. Lock statements not required.        description = "Serialized";        break;    case 2:        // Mutexing code is there, but mutexing on database connection and prepared statement objects is disabled.        // Application is responsible for serializing access to database connections and prepared statements, so must use lock statements.        description = "Multi-Threaded";        break;    default:        // SQLite was compiled with mutexing code omitted. It is not safe to use SQLite concurrently from more than one thread.        description = "Mutexing code omitted";        break;}Console.log(description);

So the fix is in your AppDelegate.cs file add this -

SQLitePCL.Batteries_V2.Init();raw.sqlite3_shutdown();raw.sqlite3_config(SQLitePCL.raw.SQLITE_CONFIG_SERIALIZED);raw.sqlite3_initialize();

What this does is sets the ThreadMode to Serialized during runtime and you should stop getting the SIGSEGV error.