mercredi 20 mai 2015

Sqlite Exception in Mvvmcross using Messenger Plugin

I'm facing the following crash in my application:

 2015-05-20 21:07:55.950 App[88568:2654974] critical:   at (wrapper managed-to-native) SQLite.SQLite3.Prepare2 (intptr,string,int,intptr&,intptr) <IL 0x0003c, 0xffffffff>
 2015-05-20 21:07:55.950 App[88568:2654974] critical:   at SQLite.SQLite3.Prepare2 (intptr,string) <IL 0x0000f, 0x00067>
 2015-05-20 21:07:55.950 App[88568:2654974] critical:   at SQLite.SQLiteCommand.Prepare () <IL 0x00011, 0x0005f>
 2015-05-20 21:07:55.950 App[88568:2654974] critical:   at SQLite.SQLiteCommand/<ExecuteDeferredQuery>d__0`1.MoveNext () <IL 0x0003f, 0x000bf>
 2015-05-20 21:07:55.950 App[88568:2654974] critical:   at System.Collections.Generic.List`1.AddEnumerable (System.Collections.Generic.IEnumerable`1<T>) [0x00013] in /     Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Collections.Generic/List.cs:146
 2015-05-20 21:07:55.951 App[88568:2654974] critical:   at System.Collections.Generic.List`1..ctor (System.Collections.Generic.IEnumerable`1<T>) [0x0002f] in /Developer/     MonoTouch/Source/mono/mcs/class/corlib/System.Collections.Generic/List.cs:69
 2015-05-20 21:07:55.951 App[88568:2654974] critical:   at System.Linq.Enumerable.ToList<TSource> (System.Collections.Generic.IEnumerable`1<TSource>) [0x00006] in ///     Library/Frameworks/Xamarin.iOS.framework/Versions/8.9.1.3/src/mono/mcs/class/http://ift.tt/1Hiv5l5
 2015-05-20 21:07:55.951 App[88568:2654974] critical:   at SQLite.SQLiteCommand.ExecuteQuery<T> () <IL 0x0001b, 0x0009b>
 2015-05-20 21:07:55.951 App[88568:2654974] critical:   at SQLite.SQLiteConnection.Query<T> (string,object[]) <IL 0x0000f, 0x000a3>
 2015-05-20 21:07:55.951 App[88568:2654974] critical:   at SQLite.SQLiteConnection.Get<T> (object) <IL 0x00024, 0x000d3>
 2015-05-20 21:07:55.951 App[88568:2654974] critical:   at App.Core.Services.DatabaseServices.DatabaseService.GetThread (string) [0x0001b] in /Users/gbastos/Documents/     Gabriel Docs/Development/App/http://ift.tt/1FCBhcO
 2015-05-20 21:07:55.951 App[88568:2654974] critical:   at App.Core.ViewModels.ThreadsListViewModel.UpdateThreadList (App.Core.Messengers.DatabaseMessenger) [0x0010f] in /     Users/gbastos/Documents/Gabriel Docs/Development/App/http://ift.tt/1FCBht2
 2015-05-20 21:07:55.952 App[88568:2654974] critical:   at Cirrious.MvvmCross.Plugins.Messenger.Subscriptions.WeakSubscription`1/<>c__DisplayClass1.<TypedInvoke>b__0 () <     IL 0x0000c, 0x00032>
 2015-05-20 21:07:55.952 App[88568:2654974] critical:   at Cirrious.CrossCore.Core.MvxMainThreadDispatcher.ExceptionMaskedAction (System.Action) <IL 0x00001, 0x00033>
 2015-05-20 21:07:55.952 App[88568:2654974] critical:   at Cirrious.MvvmCross.Touch.Views.MvxTouchUIThreadDispatcher/<>c__DisplayClass1.<RequestMainThreadAction>b__0 () <     IL 0x00006, 0x00027>
 2015-05-20 21:07:55.952 App[88568:2654974] critical:   at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Developer/MonoTouch/Source/maccore/src/Foundation/     NSAction.cs:164
 2015-05-20 21:07:55.952 App[88568:2654974] critical:   at (wrapper runtime-invoke) object.runtime_invoke_void__this__ (object,intptr,intptr,intptr) <IL 0x0004e,      0xffffffff>
 2015-05-20 21:07:55.952 App[88568:2654974] critical:   at <unknown> <0xffffffff>
 2015-05-20 21:07:55.952 App[88568:2654974] critical:   at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x000a6,      0xffffffff>
 2015-05-20 21:07:55.952 App[88568:2654974] critical:   at UIKit.UIApplication.Main (string[],intptr,intptr) [0x00005] in /Developer/MonoTouch/Source/monotouch/src/UIKit/     UIApplication.cs:62
 2015-05-20 21:07:55.952 App[88568:2654974] critical:   at UIKit.UIApplication.Main (string[],string,string) [0x00038] in /Developer/MonoTouch/Source/monotouch/src/UIKit/     UIApplication.cs:46
 2015-05-20 21:07:55.953 App[88568:2654974] critical:   at App.Mobile.Touch.Application.Main (string[]) [0x00008] in /Users/gbastos/Documents/Gabriel Docs/Development/App/     App.Mobile.Touch/Main.cs:17
 2015-05-20 21:07:55.953 App[88568:2654974] critical:   at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>

I have a ViewModel that has a weak subscribe in MainThread in a MvxMessenger. The function that is called after receiving a message is calling my database service. But it is possible that I get two different messages in the same time. (Maybe this is the error, already added a lock to the databaseService but didn't work)

This error only happens in iOS, but not in Android or WPF.

Does any one had some related error?

Here is my DatabaseService Get Thread code:

public Thread GetThread(string threadId)
    {
        lock (DatabaseLockObject)
        {
            try
            {
                var a = _connection.Get<Thread> (threadId);
                return a;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

My messenger subscription is:

_subscriptionToken = messenger.SubscribeOnMainThread <DatabaseMessenger>(UpdateThreadList);

And the UpdateThreadList method is:

    private void UpdateThreadList(DatabaseMessenger sender)
    {
            var message = (Message)sender.Value;
            var thread = _databaseService.GetThread(message.ThreadId);
            bool needUpdate = false;

            if(SharedSettings.AppSettings.GetValue(SharedSettings.Keys.CurrentThread.ToString(), "") != message.ThreadId
                && sender.Type != DatabaseMessenger.DatabaseMessageType.Delete  && message.Timestamp >= thread.SnippetTime && 
                message.SenderId != SharedSettings.AppSettings.GetValue(SharedSettings.Keys.UserInfoId.ToString(), "") && message.Id != _lastMessageId)
            {
                ++thread.UnreadMessagesCount;
                needUpdate = true;
                _lastMessageId = message.Id;
            }

            if(needUpdate)
            {
                _databaseService.Update(thread);
            }
        }
    }

Thanks in regards, Gabriel

Aucun commentaire:

Enregistrer un commentaire