Akavache or Sqlite is crashing When Calling in OnStart method

900 Views Asked by At

When I use the code below in my xamarin.forms project to check if User object exist in akavache cache, I am getting the exception below. the same code or any akavache query works somewhere else but crashes in onStart method only. I believe that I am initializing akavache in constructor already. I tried exact same code using mobile center to query locally (local sqlite) user data and I get the same exception. I think that this should be something to do with the sqlite as both akavache and mobile center uses similar sqlite libraries. Does anybody know why it doesnt work in OnStart method?

 public App()
        {
            Microsoft.Azure.Mobile.MobileCenter.Start("android=key" +
                   "uwp=key",
                   typeof(Microsoft.Azure.Mobile.Analytics.Analytics), typeof(Microsoft.Azure.Mobile.Crashes.Crashes));

            Akavache.BlobCache.ApplicationName = "myApp";
            Akavache.BlobCache.EnsureInitialized();

            ServiceLocator.Add<ICloudService, AzureCloudService>();

            InitializeComponent();

            }

  protected async override void OnStart()
        { 

            try
            {
               var User= await BlobCache.UserAccount.GetObject<User>("User");

                if (User != null)
                    Helpers.Settings.IsLoggedIn = true;
                else
                    Helpers.Settings.IsLoggedIn = false;
            }
            catch (Exception)
            {
                Helpers.Settings.IsLoggedIn = false;
            }
           ShowMainPageOrLoginPage();
        }

11-13 02:08:14.761 E/MobileCenterCrashes( 6308): Unhandled Exception: 11-13 02:08:14.761 E/MobileCenterCrashes( 6308): System.NullReferenceException: Object reference not set to an instance of an object. 11-13 02:08:14.761 E/MobileCenterCrashes( 6308): at Xamarin.Forms.Platform.Android.AppCompat.Platform.LayoutRootPage (Xamarin.Forms.Page page, System.Int32 width, System.Int32 height) [0x0000c] in D:\agent_work\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:291 11-13 02:08:14.761 E/MobileCenterCrashes( 6308): at Xamarin.Forms.Platform.Android.AppCompat.Platform.Xamarin.Forms.Platform.Android.IPlatformLayout.OnLayout (System.Boolean changed, System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x00003] in D:\agent_work\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:199 11-13 02:08:14.761 E/MobileCenterCrashes( 6308): at Xamarin.Forms.Platform.Android.PlatformRenderer.OnLayout (System.Boolean changed, System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x0000e] in D:\agent_work\1\s\Xamarin.Forms.Platform.Android\PlatformRenderer.cs:73 11-13 02:08:14.761 E/MobileCenterCrashes( 6308): at Android.Views.ViewGroup.n_OnLayout_ZIIII (System.IntPtr jnienv, System.IntPtr native__this, System.Boolean changed, System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x00008] in :0

EDIT: This issue is definetly caused by akavache. Message is really strange. it looks like that akavache has some relation with LayoutRootPage.

See my code above, I get User object from akavache cache and user object defines if I should show Login Page or Main Page. If I move ShowMainPageOrLoginPage();function above akavache call, it works just fine. So it seems that you cant make any query with akavache before the rootlayoutpage - Main page is set or loaded.

1

There are 1 best solutions below

1
jack_tux On

I had the same problem once before, for some reason if you initialize using the static method it doesn't work all the time.

Doesn't work

IBlobCache _cache = BlobCache.LocalMachine;

Does Work

IBlobCache _cache = new SQLitePersistentBlobCache(systemPath + "/CacheUtils.db");

If you want to find the systemPath you can use this in either your Android or iOS

systemPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);