1

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.

Emil
  • 6,411
  • 7
  • 62
  • 112
  • hmm not sure off hand... I have a sample i put together that uses mobile center and the latest akavache https://github.com/PureWeen/Akavache.Samples and it seems to load ok without any exceptions – Shane Neuville Nov 13 '17 at 02:39
  • which line is causing the crash? the stack trace looks like its happening in a Layout, not OnStart – Jason Nov 13 '17 at 02:39
  • @ShaneNeuville could it be because sample is using this line as jack_tux suggested below? why do I needed? _LazyBlob = new Lazy(() => new SQLitePersistentBlobCache(Path.Combine(fs.GetDefaultLocalMachineCacheDirectory(), "afile.db"), BlobCache.TaskpoolScheduler)); – Emil Nov 13 '17 at 11:21
  • @Jason message is really strange. I also dont understand why it shows me layout but it crashes on the line var User= await BlobCache.UserAccount.GetObject("User"); Everything works if I remove this line or if I execute this line in somewhere else like in any viewmodel. Only crashes if I do it in OnStart of the app.xaml.cs – Emil Nov 13 '17 at 11:24
  • @ShaneNeuville I just took a look carefully and sample is using MainPage.Appearing to get object from akavache which i said working fine. It doesnt query directly inside onstart method – Emil Nov 13 '17 at 11:31
  • If you await something else in the OnStart method do you get the same exception? The Exception you posted seems like an error internally with Xam Forms and not related to Akavache. Or what if instead of awaiting GetObject you just did a subscribe? Then does it crash? – Shane Neuville Nov 13 '17 at 15:29
  • @ShaneNeuville coming back to this issue with more information after long time, please see my edited question above. – Emil Jun 07 '18 at 16:32

1 Answers1

1

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);
jack_tux
  • 419
  • 3
  • 8
  • this is really strange. why it work on any other page without initiliazing with full path but doesnt work in OnStart method – Emil Nov 13 '17 at 11:23