476rick 476rick - 1 month ago 17
C# Question

Xamarin.iOS SQLiteException: Corrupt

I'm building an application with Xamarin.Forms and a Portable Class Library.The app can be used on Android, iOS and UWP.

I've been building a database which now works on both Android and UWP. When I use the same code and run it on iOS it gives me some errors.

This is the exception message:


Unhandled Exception: SQLite.SQLiteException: Corrupt


This is the StackTrace:


at SQLite.PreparedSqlLiteInsertCommand.ExecuteNonQuery (System.Object[] source) [0x00116] in /Users/fak/Dropbox/Projects/sqlite-net/src/SQLite.cs:2480
at SQLite.SQLiteConnection.Insert (System.Object obj, System.String extra, System.Type objType) [0x0010e] in /Users/fak/Dropbox/Projects/sqlite-net/src/SQLite.cs:1381
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3818/ad1cd42d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/3818/ad1cd42d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3818/ad1cd42d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3818/ad1cd42d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128
at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/3818/ad1cd42d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357
at App2.Model.Database.FormDAO+d__2.MoveNext () [0x00066] in D:\SourceTree\App2\App2\Model\Database\FormDAO.cs:25


This is the code where the unhandled exception is:

public async void InsertItem(Form Form)
{
try
{
var result = await Task.Factory.StartNew(() => database.Insert(Form)); //this line
} catch(Exception e)
{
Debug.WriteLine("e.message" + e.Message + ";;;;" + e.StackTrace);
}
}


The function InsertItem(form) is called about 1000 times from another async method.

Why does this code not work on iOS?

Edit:
Other error I get is:


SQLite.SQLiteException: IOError


StackTrace:


at SQLite.PreparedSqlLiteInsertCommand.ExecuteNonQuery (System.Object[] source) [0x00116] in /Users/fak/Dropbox/Projects/sqlite-net/src/SQLite.cs:2480
at SQLite.SQLiteConnection.Insert (System.Object obj, System.String extra, System.Type objType) [0x0010e] in /Users/fak/Dropbox/Projects/sqlite-net/src/SQLite.cs:1381
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3818/ad1cd42d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/3818/ad1cd42d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3818/ad1cd42d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3818/ad1cd42d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128
at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/3818/ad1cd42d/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357
at App2.Model.Database.CourseDAO+d__2.MoveNext () [0x00055] in D:\SourceTree\App2\App2\Model\Database\CourseDAO.cs:26


This exception is throwed when using almost the same method but then with this line:

var result = await Task.Factory.StartNew(() => database.Insert(Course));

Answer

The problem were the async calls to the database as Yuri S and SushiHangover pointed out in the comments.

First I didn't thought that the problem were the async calls to the database because it did work on Android and UWP. But when I made all the calls synchronised the database did work on all the three platforms: UWP, Android, iOS.

Thanks!